Merge remote-tracking branch 'origin/master' into saltation
[gitorious:taladars-gitorious-saltation.git] / app / helpers / messages_helper.rb
1 # encoding: utf-8
2 #--
3 #   Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
4 #
5 #   This program is free software: you can redistribute it and/or modify
6 #   it under the terms of the GNU Affero General Public License as published by
7 #   the Free Software Foundation, either version 3 of the License, or
8 #   (at your option) any later version.
9 #
10 #   This program is distributed in the hope that it will be useful,
11 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #   GNU Affero General Public License for more details.
14 #
15 #   You should have received a copy of the GNU Affero General Public License
16 #   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #++
18 module MessagesHelper
19   def sender_and_recipient_display(message)
20     sender_and_recipient_for(message).collect(&:capitalize).join(",")
21   end
22
23   def sender_and_recipient_for(message)
24     if message.recipient == current_user
25       sender_name = message.sender.nil? ? "[removed]" : message.sender_name
26       [link_to(h(sender_name), message.sender), "me"]
27     else
28       ["me", link_to(h(message.recipient.title), message.recipient)]
29     end
30   end
31
32   def other_party(message,user)
33     message.recipient == user ? message.sender : message.recipient
34   end
35
36   def message_title(message)
37     sender, recipient = sender_and_recipient_for(message)
38
39     case message.notifiable
40     when MergeRequest
41       msg_link = link_to('merge request', [message.notifiable.target_repository.project,
42                                            message.notifiable.target_repository,
43                                            message.notifiable])
44       "From <strong>#{sender}</strong> to <strong>#{recipient}</strong>, about a #{msg_link}"
45     when MergeRequestVersion
46       msg_link = link_to('merge request', [message.notifiable.merge_request.target_repository.project,
47                                            message.notifiable.merge_request.target_repository,
48                                            message.notifiable.merge_request])
49       "From <strong>#{sender}</strong> to <strong>#{recipient}</strong>, about a version of #{msg_link}"
50     when Membership
51       %Q{<strong>#{sender}</strong> added <strong>#{recipient}</strong> to the } +
52         %Q{team #{link_to("#{message.notifiable.group.name}", message.notifiable.group)}}
53     when Committership
54       committership = message.notifiable
55       user_link = link_to(committership.committer.title, [committership.repository.project,
56                                                           committership.repository,
57                                                           :committerships])
58       %Q{<strong>#{sender}</strong> added #{user_link} as committer in } +
59         %Q{<strong>#{committership.repository.name}</strong>}
60     else
61       "#{link_to('message', message)} from <strong>#{sender}</strong> to <strong>#{recipient}</strong>"
62     end
63   end
64
65   def sender_avatar(message)
66     if message.replies_enabled?
67       avatar_from_email(message.sender.email, :size => 32)
68     else
69       image_tag("default_face.gif", :size => "32x32")
70     end
71   end
72 end