From f31c657e98f20ac22457d6de4feb9d000e00aeab Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Fri, 16 Dec 2011 18:56:09 -0200 Subject: [PATCH] AI1826 --- app/controllers/public/profile_controller.rb | 18 +++++++++++++++--- app/models/action_tracker_notification.rb | 3 +++ app/models/article.rb | 12 +++++++----- app/models/comment.rb | 12 ++++++++---- app/models/person.rb | 4 ++++ app/models/profile.rb | 4 ++++ app/models/scrap.rb | 5 +++-- app/views/profile/_add_member_in_community.rhtml | 1 + app/views/profile/_comment.rhtml | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ app/views/profile/_create_article.rhtml | 11 +++++++++++ app/views/profile/_default_activity.rhtml | 10 ++++++++++ app/views/profile/_join_community.rhtml | 1 + app/views/profile/_leave_comment_on_activity.rhtml | 4 ++++ app/views/profile/_leave_scrap.rhtml | 4 ++-- app/views/profile/_new_friendship.rhtml | 1 + app/views/profile/_profile_activities.rhtml | 42 ++++++++++++++++++++++++++---------------- app/views/profile/_profile_activities_scraps.rhtml | 8 ++++++++ app/views/profile/_profile_activity.rhtml | 2 +- app/views/profile/_profile_scrap.rhtml | 10 ++++++---- app/views/profile/_profile_wall.rhtml | 20 ++++++++++++++++++-- app/views/profile/_update_article.rhtml | 1 + app/views/profile/_upload_image.rhtml | 1 + config/initializers/action_tracker.rb | 8 +------- db/migrate/20111211204445_add_polymorphism_on_comment.rb | 12 ++++++++++++ db/migrate/20111211233957_add_comment_count_to_action_tracker.rb | 9 +++++++++ db/schema.rb | 23 ++++++++++++++++------- public/designs/themes/base/style.css | 32 ++++++++++++++++---------------- public/stylesheets/application.css | 53 ++++++++++++++++++++++++++++++++++++----------------- test/factories.rb | 2 +- test/functional/profile_controller_test.rb | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------- test/unit/action_tracker_notification_test.rb | 8 ++++++++ test/unit/article_test.rb | 41 +++++++++++++++++++++++++++++++++++++++++ test/unit/comment_test.rb | 43 ++++++++++++------------------------------- test/unit/community_test.rb | 1 + test/unit/enterprise_test.rb | 2 ++ test/unit/person_test.rb | 30 ++++++++++++++++++++++++++++++ test/unit/profile_test.rb | 6 ++++++ vendor/plugins/active_record_counter_cache_on_polymorphic_association/init.rb | 32 ++++++++++++++++++++++++++++++++ 38 files changed, 480 insertions(+), 138 deletions(-) create mode 100644 app/views/profile/_add_member_in_community.rhtml create mode 100644 app/views/profile/_comment.rhtml create mode 100644 app/views/profile/_create_article.rhtml create mode 100644 app/views/profile/_default_activity.rhtml create mode 100644 app/views/profile/_join_community.rhtml create mode 100644 app/views/profile/_leave_comment_on_activity.rhtml create mode 100644 app/views/profile/_new_friendship.rhtml create mode 100644 app/views/profile/_profile_activities_scraps.rhtml create mode 100644 app/views/profile/_update_article.rhtml create mode 100644 app/views/profile/_upload_image.rhtml create mode 100644 db/migrate/20111211204445_add_polymorphism_on_comment.rb create mode 100644 db/migrate/20111211233957_add_comment_count_to_action_tracker.rb create mode 100644 vendor/plugins/active_record_counter_cache_on_polymorphic_association/init.rb diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index fc8ab3f..b7153d9 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -14,6 +14,7 @@ class ProfileController < PublicController if logged_in? && current_person.follows?(@profile) @network_activities = @profile.tracked_notifications.paginate(:per_page => 30, :page => params[:page]) if @network_activities.empty? @wall_items = @profile.scraps_received.not_replies.paginate(:per_page => 30, :page => params[:page]) + @activities = @profile.activities.paginate(:per_page => 30, :page => params[:page]) end @tags = profile.article_tags unless profile.display_info_to?(user) @@ -180,18 +181,29 @@ class ProfileController < PublicController @scrap.receiver= receiver @tab_action = params[:tab_action] @message = @scrap.save ? _("Message successfully sent.") : _("You can't leave an empty message.") - @scraps = @profile.scraps_received.not_replies.paginate(:per_page => 30, :page => params[:page]) if params[:not_load_scraps].nil? + @activities = @profile.activities.paginate(:per_page => 30, :page => params[:page]) if params[:not_load_scraps].nil? render :partial => 'leave_scrap' end + def leave_comment_on_activity + @comment = Comment.new(params[:comment]) + @comment.author = user #'if logged_in? + @comment.source = ActionTracker::Record.find(params[:comment][:source_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? + render :partial => 'leave_comment_on_activity' + end + def view_more_scraps @scraps = @profile.scraps_received.not_replies.paginate(:per_page => 30, :page => params[:page]) render :partial => 'profile_scraps', :locals => {:scraps => @scraps} end def view_more_activities - @activities = @profile.tracked_actions.paginate(:per_page => 30, :page => params[:page]) - render :partial => 'profile_activities', :locals => {:activities => @activities} +# @activities = @profile.tracked_actions.paginate(:per_page => 30, :page => params[:page]) + @activities = @profile.activities.paginate(:per_page => 30, :page => params[:page]) + render :partial => 'profile_activities_scraps', :locals => {:activities => @activities} end def view_more_network_activities diff --git a/app/models/action_tracker_notification.rb b/app/models/action_tracker_notification.rb index 5a6e916..717d0a0 100644 --- a/app/models/action_tracker_notification.rb +++ b/app/models/action_tracker_notification.rb @@ -3,9 +3,12 @@ class ActionTrackerNotification < ActiveRecord::Base belongs_to :profile belongs_to :action_tracker, :class_name => 'ActionTracker::Record', :foreign_key => 'action_tracker_id' + has_many :comments, :through => :action_tracker, :class_name => 'Comment', :foreign_key => 'source_id' + validates_presence_of :profile_id, :action_tracker_id validates_uniqueness_of :action_tracker_id, :scope => :profile_id end ActionTracker::Record.has_many :action_tracker_notifications, :class_name => 'ActionTrackerNotification', :foreign_key => 'action_tracker_id', :dependent => :destroy +ActionTracker::Record.has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :order => 'created_at asc' diff --git a/app/models/article.rb b/app/models/article.rb index 59e9e6d..6be91a6 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -2,9 +2,7 @@ require 'hpricot' class Article < ActiveRecord::Base - track_actions :create_article, :after_create, :keep_params => [:name, :url], :if => Proc.new { |a| a.is_trackable? && !a.image? }, :custom_target => :action_tracker_target - track_actions :update_article, :before_update, :keep_params => [:name, :url], :if => Proc.new { |a| a.is_trackable? && (a.body_changed? || a.name_changed?) }, :custom_target => :action_tracker_target - track_actions :remove_article, :before_destroy, :keep_params => [:name], :if => Proc.new { |a| a.is_trackable? }, :custom_target => :action_tracker_target + track_actions :create_article, :after_create, :keep_params => [:name, :url, :lead], :if => Proc.new { |a| a.is_trackable? && !a.image? }, :custom_target => :action_tracker_target # xss_terminate plugin can't sanitize array fields before_save :sanitize_tag_list @@ -17,7 +15,7 @@ class Article < ActiveRecord::Base belongs_to :last_changed_by, :class_name => 'Person', :foreign_key => 'last_changed_by_id' - has_many :comments, :dependent => :destroy, :order => 'created_at asc' + has_many :comments, :dependent => :destroy, :order => 'created_at asc', :as => :source has_many :article_categorizations, :conditions => [ 'articles_categories.virtual = ?', false ] has_many :categories, :through => :article_categorizations @@ -134,7 +132,11 @@ class Article < ActiveRecord::Base before_update do |article| article.advertise = true end - + + after_update do |article| + #update article's activity + end + # retrieves all articles belonging to the given +profile+ that are not # sub-articles of any other article. named_scope :top_level_for, lambda { |profile| diff --git a/app/models/comment.rb b/app/models/comment.rb index 244ed25..91ea076 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,9 +1,13 @@ class Comment < ActiveRecord::Base - track_actions :leave_comment, :after_create, :keep_params => ["article.title", "article.url", "title", "url", "body"], :custom_target => :action_tracker_target +# track_actions :leave_comment, :after_create, :keep_params => ["article.title", "article.url", "title", "url", "body"], :custom_target => :action_tracker_target validates_presence_of :body - belongs_to :article, :counter_cache => true + + belongs_to :source, :foreign_key => :source_id, :counter_cache => true, :polymorphic => true + alias :article :source + alias :article= :source= + belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id' has_many :children, :class_name => 'Comment', :foreign_key => 'reply_of_id', :dependent => :destroy belongs_to :reply_of, :class_name => 'Comment', :foreign_key => 'reply_of_id' @@ -70,11 +74,11 @@ class Comment < ActiveRecord::Base after_save :notify_article after_destroy :notify_article def notify_article - article.comments_updated + article.comments_updated if article.kind_of?(Article) end after_create do |comment| - if comment.article.notify_comments? && !comment.article.profile.notification_emails.empty? + if comment.source.kind_of?(Article) && comment.article.notify_comments? && !comment.article.profile.notification_emails.empty? Comment::Notifier.deliver_mail(comment) end end diff --git a/app/models/person.rb b/app/models/person.rb index 3ddeadc..b8c10b5 100644 --- a/app/models/person.rb +++ b/app/models/person.rb @@ -421,6 +421,10 @@ class Person < Profile user.save! end + def activities + Scrap.find_by_sql("SELECT id, updated_at, '#{Scrap.to_s}' AS klass FROM #{Scrap.table_name} WHERE scraps.receiver_id = #{self.id} AND scraps.scrap_id IS NULL UNION SELECT id, updated_at, '#{ActionTracker::Record.to_s}' AS klass FROM #{ActionTracker::Record.table_name} WHERE action_tracker.user_id = #{self.id} ORDER BY updated_at DESC") + end + protected def followed_by?(profile) diff --git a/app/models/profile.rb b/app/models/profile.rb index ac5591d..40edc18 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -822,6 +822,10 @@ private :generate_url, :url_options name end + # Override in your subclasses + def activities + [] + end protected def followed_by?(person) diff --git a/app/models/scrap.rb b/app/models/scrap.rb index 50f497e..fb0a5db 100644 --- a/app/models/scrap.rb +++ b/app/models/scrap.rb @@ -11,8 +11,9 @@ class Scrap < ActiveRecord::Base named_scope :not_replies, :conditions => {:scrap_id => nil} - track_actions :leave_scrap, :after_create, :keep_params => ['sender.name', 'content', 'receiver.name', 'receiver.url'], :if => Proc.new{|s| s.receiver != s.sender}, :custom_target => :action_tracker_target - track_actions :leave_scrap_to_self, :after_create, :keep_params => ['sender.name', 'content'], :if => Proc.new{|s| s.receiver == s.sender} +######### COMO OS AMIGOS VÃO SABER Q O AMIGO RECEBEU COMENTÁRIO? ACHO QUE TEM QUE TER AÇÃO +# track_actions :leave_scrap, :after_create, :keep_params => ['sender.name', 'content', 'receiver.name', 'receiver.url'], :if => Proc.new{|s| s.receiver != s.sender}, :custom_target => :action_tracker_target +# track_actions :leave_scrap_to_self, :after_create, :keep_params => ['sender.name', 'content'], :if => Proc.new{|s| s.receiver == s.sender} after_create do |scrap| scrap.root.update_attribute('updated_at', DateTime.now) unless scrap.root.nil? diff --git a/app/views/profile/_add_member_in_community.rhtml b/app/views/profile/_add_member_in_community.rhtml new file mode 100644 index 0000000..118a275 --- /dev/null +++ b/app/views/profile/_add_member_in_community.rhtml @@ -0,0 +1 @@ +<%= render :partial => 'default_activity', :locals => {:activity => activity} %> diff --git a/app/views/profile/_comment.rhtml b/app/views/profile/_comment.rhtml new file mode 100644 index 0000000..3277900 --- /dev/null +++ b/app/views/profile/_comment.rhtml @@ -0,0 +1,61 @@ +
  • +
    + +
    + + <%# if comment.author %> + <%= link_to image_tag(profile_icon(comment.author, :minor)) + + content_tag('span', comment.author_name, :class => 'comment-info'), + comment.author.url, + :class => 'comment-picture', + :title => comment.author_name + %> + <%# end %> + +
    +

    <%= comment.title %>

    +
    +

    + <%= txt2html comment.body %> +

    +
    + <%= show_time(comment.created_at) %> +
    +
    + + <%# if logged_in? && (user == profile || user == comment.author || user.has_permission?(:moderate_comments, profile)) %> + <% button_bar(:style => 'float: right; margin-top: 0px;') do %> + <%= icon_button(:delete, _('Remove this comment and all its replies'), { :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?')) %> + <% end %> + <%# end %> + +
    + <% if @comment && @comment.errors.any? && @comment.reply_of_id.to_i == comment.id %> + <%= error_messages_for :comment %> + + <% end %> + <%= report_abuse(comment.author, :comment_link, comment) if comment.author %> + <%= link_to_function _('Reply'), + "var f = add_comment_reply_form(this, %s); f.find('input[name=comment[title]], textarea').val(''); return false" % comment.id, + :class => 'comment-footer comment-footer-link comment-footer-hide', + :id => 'comment-reply-to-' + comment.id.to_s + %> +
    + +
    + + <% unless comment.replies.blank? %> +
      + <% comment.replies.each do |reply| %> + <%= render :partial => 'comment', :locals => { :comment => reply } %> + <% end %> +
    + <% end %> + +
    +
  • diff --git a/app/views/profile/_create_article.rhtml b/app/views/profile/_create_article.rhtml new file mode 100644 index 0000000..09b31ce --- /dev/null +++ b/app/views/profile/_create_article.rhtml @@ -0,0 +1,11 @@ +
    + <%= link_to(profile_image(activity.user, :minor), activity.user.url) %> +
    +
    +

    <%= link_to activity.user.name, activity.user.url %> <%= describe activity %>

    +

    <%= activity.params['lead'] %>

    +

    <%= time_ago_as_sentence(activity.created_at) + ' ' + _('ago') %>

    +
    + <%= link_to_remote(content_tag(:span, _('Remove')), :url =>{:action => 'remove_activity', :activity_id => activity.id}, :update => "profile-activity-item-#{activity.id}") if logged_in? && current_person == @profile %> +
    +
    diff --git a/app/views/profile/_default_activity.rhtml b/app/views/profile/_default_activity.rhtml new file mode 100644 index 0000000..c3c487d --- /dev/null +++ b/app/views/profile/_default_activity.rhtml @@ -0,0 +1,10 @@ +
    + <%= link_to(profile_image(activity.user, :minor), activity.user.url) %> +
    +
    +

    <%= link_to activity.user.name, activity.user.url %> <%= describe activity %>

    +

    <%= time_ago_as_sentence(activity.created_at) + ' ' + _('ago') %>

    +
    + <%= link_to_remote(content_tag(:span, _('Remove')), :url =>{:action => 'remove_activity', :activity_id => activity.id}, :update => "profile-activity-item-#{activity.id}") if logged_in? && current_person == @profile %> +
    +
    diff --git a/app/views/profile/_join_community.rhtml b/app/views/profile/_join_community.rhtml new file mode 100644 index 0000000..118a275 --- /dev/null +++ b/app/views/profile/_join_community.rhtml @@ -0,0 +1 @@ +<%= render :partial => 'default_activity', :locals => {:activity => activity} %> diff --git a/app/views/profile/_leave_comment_on_activity.rhtml b/app/views/profile/_leave_comment_on_activity.rhtml new file mode 100644 index 0000000..4d9ac6e --- /dev/null +++ b/app/views/profile/_leave_comment_on_activity.rhtml @@ -0,0 +1,4 @@ +<%= @message %> +<% unless @activities.nil? %> + <%= render :partial => 'profile_activities_scraps', :locals => {:activities => @activities} %> +<% end %> diff --git a/app/views/profile/_leave_scrap.rhtml b/app/views/profile/_leave_scrap.rhtml index 67dc049..4d9ac6e 100644 --- a/app/views/profile/_leave_scrap.rhtml +++ b/app/views/profile/_leave_scrap.rhtml @@ -1,4 +1,4 @@ <%= @message %> -<% unless @scraps.nil? %> - <%= render :partial => 'profile_scraps', :locals => {:scraps => @scraps} %> +<% unless @activities.nil? %> + <%= render :partial => 'profile_activities_scraps', :locals => {:activities => @activities} %> <% end %> diff --git a/app/views/profile/_new_friendship.rhtml b/app/views/profile/_new_friendship.rhtml new file mode 100644 index 0000000..118a275 --- /dev/null +++ b/app/views/profile/_new_friendship.rhtml @@ -0,0 +1 @@ +<%= render :partial => 'default_activity', :locals => {:activity => activity} %> diff --git a/app/views/profile/_profile_activities.rhtml b/app/views/profile/_profile_activities.rhtml index 3f2e74f..1d169d1 100644 --- a/app/views/profile/_profile_activities.rhtml +++ b/app/views/profile/_profile_activities.rhtml @@ -1,18 +1,28 @@ -<% activities.each do |activity| %> -
  • -
    - <%= link_to(profile_image(activity.user, :minor), activity.user.url) %> +
  • + <%= render :partial => activity.verb, :locals => { :activity => activity }%> +
    + + <%# if logged_in? && current_person.follows?(activity.sender) && activity.root.nil? %> + <%= link_to_function _('Comment'), "hide_and_show(['#profile-wall-reply-response-#{activity.id}'],['#profile-wall-reply-#{activity.id}', '#profile-wall-reply-form-#{activity.id}']);$('reply_content_#{activity.id}').value='';$('activity_id_#{activity.id}').value='#{activity.id}';return false", :class => "profile-send-reply" %> + <%# end %> + + + +
  • -<% end %> -<% if activities.current_page < activities.total_pages %> -
    - <%= button_to_remote :add, _('View more'), :url => {:action => 'view_more_activities', :page => (activities.current_page + 1)}, :update => "profile_activities_page_#{activities.current_page}" %> +
    -<% end %> + + diff --git a/app/views/profile/_profile_activities_scraps.rhtml b/app/views/profile/_profile_activities_scraps.rhtml new file mode 100644 index 0000000..cbd527a --- /dev/null +++ b/app/views/profile/_profile_activities_scraps.rhtml @@ -0,0 +1,8 @@ +<% activities.each do |a| %> + <% activity = a.klass.constantize.find(a.id) %> + <% if activity.kind_of?(ActionTracker::Record) && @profile.person? %> + <%= render :partial => 'profile_activities', :locals => {:activity => activity} %> + <% else %> + <%= render :partial => 'profile_scrap', :locals => {:scrap => activity } %> + <% end %> +<% end %> diff --git a/app/views/profile/_profile_activity.rhtml b/app/views/profile/_profile_activity.rhtml index 772e39b..c780a4f 100644 --- a/app/views/profile/_profile_activity.rhtml +++ b/app/views/profile/_profile_activity.rhtml @@ -1,6 +1,6 @@

    <%= _("%s's activity") % @profile.name %>

    diff --git a/app/views/profile/_profile_scrap.rhtml b/app/views/profile/_profile_scrap.rhtml index 390d3d5..e08c3ac 100644 --- a/app/views/profile/_profile_scrap.rhtml +++ b/app/views/profile/_profile_scrap.rhtml @@ -7,13 +7,15 @@ <% comment_balloon :class => 'profile-wall-description' do %>

    <%= link_to scrap.sender.name, scrap.sender.url %>

    -

    <%= time_ago_as_sentence(scrap.created_at) + ' ' + _('ago') %>

    <%= txt2html scrap.content %>

    - <%= button_to_remote(:delete, content_tag(:span, _('Remove')), :url =>{:action => 'remove_scrap', :scrap_id => scrap.id}, :update => "profile-wall-item-#{scrap.id}") if logged_in? && user.can_control_scrap?(scrap) %> +

    <%= time_ago_as_sentence(scrap.created_at) + ' ' + _('ago') %>

    +
    <% if logged_in? && current_person.follows?(scrap.sender) && scrap.root.nil? %> -

    <%= link_to_function _('Reply'), "hide_and_show(['#profile-wall-reply-response-#{scrap.id}'],['#profile-wall-reply-#{scrap.id}', '#profile-wall-reply-form-#{scrap.id}']);$('reply_content_#{scrap.id}').value='';$('scrap_id_#{scrap.id}').value='#{scrap.id}';return false", :class => "profile-send-reply icon-reply" %>

    + <%= link_to_function _('Comment'), "hide_and_show(['#profile-wall-reply-response-#{scrap.id}'],['#profile-wall-reply-#{scrap.id}', '#profile-wall-reply-form-#{scrap.id}']);$('reply_content_#{scrap.id}').value='';$('scrap_id_#{scrap.id}').value='#{scrap.id}';return false", :class => "profile-send-reply" %> <% end %> + <%= link_to_remote(content_tag(:span, _('Remove')), :url =>{:action => 'remove_scrap', :scrap_id => scrap.id}, :update => "profile-wall-item-#{scrap.id}") if logged_in? && user.can_control_scrap?(scrap) %> <% end %> +