cirandas.net

ref: master

app/models/add_friend.rb


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class AddFriend < Task

  settings_items :group_for_person, :group_for_friend

  validates_presence_of :requestor_id, :target_id

  validates_uniqueness_of :target_id, scope: [ :requestor_id, :status ], if: proc{ |t| t.status == Task::Status::ACTIVE }

  validates_length_of :group_for_person, :group_for_friend, :maximum => 150, :allow_nil => true

  alias :person :requestor
  alias :person= :requestor=

  alias :friend :target
  alias :friend= :target=

  validates :requestor, :kind_of => { :kind => Person }
  validates :target, :kind_of => { :kind => Person }

  after_create do |task|
    TaskMailer.invitation_notification(task).deliver unless task.friend
    remove_from_suggestion_list(task)
  end

  def perform
    target.add_friend(requestor, group_for_friend)
    requestor.add_friend(target, group_for_person)
  end

  def permission
    :manage_friends
  end

  def target_notification_description
    _('%{requestor} wants to be your friend.') % {:requestor => requestor.name}
  end

  def target_notification_message
    target_notification_description + "\n\n" +
    _('You need to login to %{system} in order to accept %{requestor} as your friend.') % { :system => target.environment.name, :requestor => requestor.name }
  end

  def title
    _("New friend")
  end

  def information
    {:message => _('%{requestor} wants to be your friend.') }
  end

  def accept_details
    true
  end

  def icon
    {:type => :profile_image, :profile => requestor, :url => requestor.url}
  end

  def remove_from_suggestion_list(task)
    suggestion = task.requestor.suggested_profiles.find_by suggestion_id: task.target.id
    suggestion.disable if suggestion
  end
end