cirandas.net

ref: master

app/models/invite_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
class InviteFriend < Invitation

  settings_items :group_for_person, :group_for_friend
  before_create :check_for_invitation_existence

  def perform
    person.add_friend(friend, group_for_person)
    friend.add_friend(person, group_for_friend)
  end

  def title
    _("Friend invitation")
  end

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

  def accept_details
    true
  end

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

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

  def permission
    :manage_friends
  end

  # Default message send to friend when user use invite a friend feature
  def self.mail_template
    [ _('Hello <friend>,'),
      _('<user> is inviting you to participate on <environment>.'),
      _('To accept the invitation, please follow this link:'),
      '<url>',
      "--\n<environment>",
    ].join("\n\n")
  end

  private
  def check_for_invitation_existence
    if friend
      friend.tasks.pending.of("InviteFriend").where(requestor_id: person.id, target_id: friend.id).blank?
    end
  end

end