public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
insoshi / app / helpers / activities_helper.rb
100644 169 lines (158 sloc) 5.234 kb
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
module ActivitiesHelper
 
  # Given an activity, return a message for the feed for the activity's class.
  def feed_message(activity)
    person = activity.person
    case activity_type(activity)
    when "BlogPost"
      post = activity.item
      blog = post.blog
      view_blog = blog_link("View #{h person.name}'s blog", blog)
      %(#{person_link(person)} made a blog post titled
#{post_link(blog, post)}.<br /> #{view_blog}.)
    when "Comment"
      parent = activity.item.commentable
      parent_type = parent.class.to_s
      case parent_type
      when "BlogPost"
        post = activity.item.commentable
        blog = post.blog
        %(#{person_link(person)} made a comment to
#{someones(blog.person, person)}
blog post #{post_link(blog, post)}.)
      when "Person"
        %(#{person_link(activity.item.commenter)} commented on
#{wall(activity)}.)
      end
    when "Connection"
      %(#{person_link(activity.item.person)} and
#{person_link(activity.item.contact)}
have connected.)
    when "ForumPost"
      post = activity.item
      %(#{person_link(person)} made a post on the forum topic
#{topic_link(post.topic)}.)
    when "Topic"
      %(#{person_link(person)} created the new discussion topic
#{topic_link(activity.item)}.)
    when "Photo"
      %(#{person_link(person)} changed their profile picture.)
    when "Person"
      %(#{person_link(person)} changed their description.)
    else
      # TODO: make this a more graceful falure (?).
      raise "Invalid activity type #{activity_type(activity).inspect}"
    end
  end
  
  def minifeed_message(activity)
    person = activity.person
    case activity_type(activity)
    when "BlogPost"
      post = activity.item
      blog = post.blog
      %(#{person_link(person)} made a
#{post_link("new blog post", blog, post)}.)
    when "Comment"
      parent = activity.item.commentable
      parent_type = parent.class.to_s
      case parent_type
      when "BlogPost"
        post = activity.item.commentable
        blog = post.blog
        %(#{person_link(person)} made a comment to
#{someones(blog.person, person)}
blog post #{post_link(blog, post)}.)
        %(#{person_link(person)} made a comment on
#{someones(blog.person, person)}
#{post_link("blog post", post.blog, post)}.)
      when "Person"
        %(#{person_link(activity.item.commenter)} commented on
#{wall(activity)}.)
      end
    when "Connection"
      %(#{person_link(person)} and #{person_link(activity.item.contact)}
have connected.)
    when "ForumPost"
      topic = activity.item.topic
      # TODO: deep link this to the post
      %(#{person_link(person)} made a #{topic_link("forum post", topic)}.)
    when "Topic"
      %(#{person_link(person)} created a
#{topic_link("new discussion topic", activity.item)}.)
    when "Photo"
      %(#{person_link(person)} changed their profile picture.)
    when "Person"
      %(#{person_link(person)} changed their description.)
    else
      raise "Invalid activity type #{activity_type(activity).inspect}"
    end
  end
  
  # Given an activity, return the right icon.
  def feed_icon(activity)
    img = case activity_type(activity)
            when "BlogPost"
              "blog.gif"
            when "Comment"
              parent_type = activity.item.commentable.class.to_s
              case parent_type
              when "BlogPost"
                "comment.gif"
              when "Person"
                "signal.gif"
              end
            when "Connection"
              "switch.gif"
            when "ForumPost"
              "new.gif"
            when "Topic"
              "add.gif"
            when "Photo"
              "camera.gif"
            when "Person"
                "edit.gif"
            else
              # TODO: make this a more graceful falure (?).
              raise "Invalid activity type #{activity_type(activity).inspect}"
            end
    image_tag("icons/#{img}", :class => "icon")
  end
  
  def someones(person, commenter, link = true)
    # raise commenter.inspect
    if link
      person == commenter ? "their own" : "#{person_link(person)}'s"
    else
      person == commenter ? "their own" : "#{h person.name}'s"
    end
  end
  
  def blog_link(text, blog)
    link_to(text, blog_path(blog))
  end
  
  def post_link(text, blog, post = nil)
    if post.nil?
      post = blog
      blog = text
      text = post.title
    end
    link_to(text, blog_post_path(blog, post))
  end
  
  def topic_link(text, topic = nil)
    if topic.nil?
      topic = text
      text = topic.name
    end
    link_to(text, forum_topic_path(topic.forum, topic))
  end
 
  # Return a link to the wall.
  def wall(activity)
    commenter = activity.person
    person = activity.item.commentable
    link_to("#{someones(person, commenter, false)} wall",
            person_path(person, :anchor => "wall"))
  end
  
  private
  
    # Return the type of activity.
    # We switch on the class.to_s because the class itself is quite long
    # (due to ActiveRecord).
    def activity_type(activity)
      activity.item.class.to_s
    end
end