cirandas.net

ref: master

app/models/invite_member.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
64
65
66
67
68
69
70
71
72
class InviteMember < Invitation

  settings_items :community_id, :type => :integer
  validates_presence_of :community_id
  before_create :check_for_invitation_existence

  def community
    Community.find(community_id)
  end

  def community=(newcommunity)
    community_id = newcommunity.id
  end

  def perform
    community.add_member(friend, true)
  end

  def title
    _("Community invitation")
  end

  def linked_subject
    {:text => community.name, :url => community.public_profile_url}
  end

  def information
    {:message => _('%{requestor} invited you to join %{linked_subject}.').html_safe}
  end

  def url
    community.url
  end

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

  def target_notification_description
    (_('%{requestor} invited you to join %{community}.') % {:requestor => requestor.name, :community => community.name}).html_safe
  end

  def target_notification_message
    if friend
      _('%{requestor} is inviting you to join "%{community}" on %{system}.') % { :system => target.environment.name, :requestor => requestor.name, :community => community.name }
    else
      super
    end
  end

  def expanded_message
    super.gsub /<community>/, community.name
  end

  # Default message send to friend when user use invite a friend feature
  def self.mail_template
    [ _('Hello <friend>,'),
      _('<user> is inviting you to join "<community>" 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("InviteMember").where(requestor_id: person.id).select{ |t| t.data[:community_id] == community_id }.blank?
    end
  end

end