From bf85926025b38cced363ca5e5ba8b7d4c909322e Mon Sep 17 00:00:00 2001 From: Caio SBA Date: Mon, 26 Dec 2011 20:35:32 -0300 Subject: [PATCH] If activity is about an article, the comment is added to the article; Comments can 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 | 13 ++++++++++++- app/models/article.rb | 7 +++++-- app/models/comment.rb | 6 ++++++ app/views/profile/_comment.rhtml | 2 +- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index ade39ca..ba00e6b 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -185,7 +185,8 @@ class ProfileController < PublicController def leave_comment_on_activity @comment = Comment.new(params[:comment]) @comment.author = user - @comment.source = ActionTracker::Record.find(params[:comment][:source_id]) + @activity = ActionTracker::Record.find(params[:comment][:source_id]) + @comment.source_type, @comment.source_id = (@activity.target_type == 'Article' ? ['Article', @activity.target_id] : [@activity.class.to_s, @activity.id]) @tab_action = params[:tab_action] @message = @comment.save ? _("Comment successfully added.") : _("You can't leave an empty comment.") @activities = @profile.activities.paginate(:per_page => 30, :page => params[:page]) if params[:not_load_scraps].nil? @@ -293,6 +294,16 @@ class ProfileController < PublicController end end + def remove_comment + #FIXME Check whether these permissions are enough + @comment = Comment.find(params[:comment_id]) + if (user == @comment.author || user == profile || user.has_permission?(:moderate_comments, profile)) + @comment.destroy + session[:notice] = _('Comment successfully deleted') + end + redirect_to :action => :index + end + protected def check_access_to_profile diff --git a/app/models/article.rb b/app/models/article.rb index 711734b..2f27375 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -135,8 +135,7 @@ class Article < ActiveRecord::Base after_update :update_creation_activity def update_creation_activity - action = ActionTracker::Record.find(:first, :conditions => {:target_type => 'Article', :target_id => self.id}) - action.touch + self.activity.touch if self.activity end # retrieves all articles belonging to the given +profile+ that are not @@ -563,6 +562,10 @@ class Article < ActiveRecord::Base _('Created at: ') end + def activity + ActionTracker::Record.find_by_target_type_and_target_id 'Article', self.id + end + private def sanitize_tag_list diff --git a/app/models/comment.rb b/app/models/comment.rb index 6686e26..65cde87 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -79,6 +79,12 @@ class Comment < ActiveRecord::Base if comment.source.kind_of?(Article) && comment.article.notify_comments? && !comment.article.profile.notification_emails.empty? Comment::Notifier.deliver_mail(comment) end + + comment.article.activity.increment!(:comments_count) if comment.source.kind_of?(Article) && comment.article.activity + end + + after_destroy do |comment| + comment.article.activity.decrement!(:comments_count) if comment.source.kind_of?(Article) && comment.article.activity end def replies diff --git a/app/views/profile/_comment.rhtml b/app/views/profile/_comment.rhtml index 0c712b9..01a45bf 100644 --- a/app/views/profile/_comment.rhtml +++ b/app/views/profile/_comment.rhtml @@ -25,7 +25,7 @@ <% if logged_in? && (user == profile || user == Person.find(comment.author_id) || user.has_permission?(:moderate_comments, profile)) %> <% button_bar(:style => 'float: right; margin-top: 0px;') do %> - <%= 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?')) %> + <%= 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?')) %> <% end %> <% end %> -- libgit2 0.21.2