Commit bf85926025b38cced363ca5e5ba8b7d4c909322e

Authored by Caio Almeida
Committed by Daniela Feitosa
1 parent 83923dfc

If activity is about an article, the comment is added to the article; Comments c…

…an be removed; Added an method to get the activity related to the article; Alter the counter cache of the activity when a comment is added or removed to the article
app/controllers/public/profile_controller.rb
@@ -185,7 +185,8 @@ class ProfileController < PublicController @@ -185,7 +185,8 @@ class ProfileController < PublicController
185 def leave_comment_on_activity 185 def leave_comment_on_activity
186 @comment = Comment.new(params[:comment]) 186 @comment = Comment.new(params[:comment])
187 @comment.author = user 187 @comment.author = user
188 - @comment.source = ActionTracker::Record.find(params[:comment][:source_id]) 188 + @activity = ActionTracker::Record.find(params[:comment][:source_id])
  189 + @comment.source_type, @comment.source_id = (@activity.target_type == 'Article' ? ['Article', @activity.target_id] : [@activity.class.to_s, @activity.id])
189 @tab_action = params[:tab_action] 190 @tab_action = params[:tab_action]
190 @message = @comment.save ? _("Comment successfully added.") : _("You can't leave an empty comment.") 191 @message = @comment.save ? _("Comment successfully added.") : _("You can't leave an empty comment.")
191 @activities = @profile.activities.paginate(:per_page => 30, :page => params[:page]) if params[:not_load_scraps].nil? 192 @activities = @profile.activities.paginate(:per_page => 30, :page => params[:page]) if params[:not_load_scraps].nil?
@@ -293,6 +294,16 @@ class ProfileController < PublicController @@ -293,6 +294,16 @@ class ProfileController < PublicController
293 end 294 end
294 end 295 end
295 296
  297 + def remove_comment
  298 + #FIXME Check whether these permissions are enough
  299 + @comment = Comment.find(params[:comment_id])
  300 + if (user == @comment.author || user == profile || user.has_permission?(:moderate_comments, profile))
  301 + @comment.destroy
  302 + session[:notice] = _('Comment successfully deleted')
  303 + end
  304 + redirect_to :action => :index
  305 + end
  306 +
296 protected 307 protected
297 308
298 def check_access_to_profile 309 def check_access_to_profile
app/models/article.rb
@@ -135,8 +135,7 @@ class Article < ActiveRecord::Base @@ -135,8 +135,7 @@ class Article < ActiveRecord::Base
135 135
136 after_update :update_creation_activity 136 after_update :update_creation_activity
137 def update_creation_activity 137 def update_creation_activity
138 - action = ActionTracker::Record.find(:first, :conditions => {:target_type => 'Article', :target_id => self.id})  
139 - action.touch 138 + self.activity.touch if self.activity
140 end 139 end
141 140
142 # retrieves all articles belonging to the given +profile+ that are not 141 # retrieves all articles belonging to the given +profile+ that are not
@@ -563,6 +562,10 @@ class Article < ActiveRecord::Base @@ -563,6 +562,10 @@ class Article < ActiveRecord::Base
563 _('Created at: ') 562 _('Created at: ')
564 end 563 end
565 564
  565 + def activity
  566 + ActionTracker::Record.find_by_target_type_and_target_id 'Article', self.id
  567 + end
  568 +
566 private 569 private
567 570
568 def sanitize_tag_list 571 def sanitize_tag_list
app/models/comment.rb
@@ -79,6 +79,12 @@ class Comment < ActiveRecord::Base @@ -79,6 +79,12 @@ class Comment < ActiveRecord::Base
79 if comment.source.kind_of?(Article) && comment.article.notify_comments? && !comment.article.profile.notification_emails.empty? 79 if comment.source.kind_of?(Article) && comment.article.notify_comments? && !comment.article.profile.notification_emails.empty?
80 Comment::Notifier.deliver_mail(comment) 80 Comment::Notifier.deliver_mail(comment)
81 end 81 end
  82 +
  83 + comment.article.activity.increment!(:comments_count) if comment.source.kind_of?(Article) && comment.article.activity
  84 + end
  85 +
  86 + after_destroy do |comment|
  87 + comment.article.activity.decrement!(:comments_count) if comment.source.kind_of?(Article) && comment.article.activity
82 end 88 end
83 89
84 def replies 90 def replies
app/views/profile/_comment.rhtml
@@ -25,7 +25,7 @@ @@ -25,7 +25,7 @@
25 25
26 <% if logged_in? && (user == profile || user == Person.find(comment.author_id) || user.has_permission?(:moderate_comments, profile)) %> 26 <% if logged_in? && (user == profile || user == Person.find(comment.author_id) || user.has_permission?(:moderate_comments, profile)) %>
27 <% button_bar(:style => 'float: right; margin-top: 0px;') do %> 27 <% button_bar(:style => 'float: right; margin-top: 0px;') do %>
28 - <%= icon_button(:delete, _('Remove'), { :profile => params[:profile], :remove_comment => comment.id, :view => params[:view] }, :method => :post, :confirm => _('Are you sure you want to remove this comment and all its replies?')) %> 28 + <%= icon_button(:delete, _('Remove'), { :action => :remove_comment, :comment_id => comment.id }, :method => :get, :confirm => _('Are you sure you want to remove this comment and all its replies?')) %>
29 <% end %> 29 <% end %>
30 <% end %> 30 <% end %>
31 31