From 003903846d4fc92edf102055b6e2458c47000412 Mon Sep 17 00:00:00 2001 From: Daniela Soares Feitosa Date: Fri, 9 Mar 2012 18:07:37 -0300 Subject: [PATCH] AI1826 --- app/models/article.rb | 6 ++++++ app/models/comment.rb | 9 ++++++--- db/migrate/20111228202739_remove_useless_tracked_actions.rb | 1 - db/migrate/20120228202739_adapt_create_articles_activity.rb | 24 ++++++++++++++++++++++++ test/unit/article_test.rb | 56 +++++++++++++++++++------------------------------------- test/unit/comment_test.rb | 44 ++++++++++++++++++++++++++++++++++++++------ 6 files changed, 93 insertions(+), 47 deletions(-) create mode 100644 db/migrate/20120228202739_adapt_create_articles_activity.rb diff --git a/app/models/article.rb b/app/models/article.rb index 1365f4a..abf6554 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -561,6 +561,12 @@ class Article < ActiveRecord::Base ActionTracker::Record.find_by_target_type_and_target_id 'Article', self.id end + def create_activity + if is_trackable? && !image? + save_action_for_verb 'create_article', [:name, :url, :lead, :first_image], Proc.new{}, :author + end + end + def first_image img = Hpricot(self.lead.to_s).search('img[@src]').first || Hpricot(self.body.to_s).search('img').first img.nil? ? '' : img.attributes['src'] diff --git a/app/models/comment.rb b/app/models/comment.rb index fc376a4..50af142 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -80,9 +80,12 @@ class Comment < ActiveRecord::Base Comment::Notifier.deliver_mail(comment) end - if comment.source.kind_of?(Article) && comment.article.activity - comment.article.activity.increment!(:comments_count) - comment.article.activity.update_attribute(:visible, true) + if comment.source.kind_of?(Article) + comment.article.create_activity if comment.article.activity.nil? + if comment.article.activity + comment.article.activity.increment!(:comments_count) + comment.article.activity.update_attribute(:visible, true) + end end end diff --git a/db/migrate/20111228202739_remove_useless_tracked_actions.rb b/db/migrate/20111228202739_remove_useless_tracked_actions.rb index 4c1d090..5da8b0e 100644 --- a/db/migrate/20111228202739_remove_useless_tracked_actions.rb +++ b/db/migrate/20111228202739_remove_useless_tracked_actions.rb @@ -7,7 +7,6 @@ class RemoveUselessTrackedActions < ActiveRecord::Migration if (activity.updated_at.to_time < Time.now.months_ago(3)) || verbs.include?(activity.verb) activity.destroy end - # select_all("SELECT id, verb, updated_at FROM action_tracker WHERE verb IN ('create_article', 'update_article', 'remove_article', 'leave_comment', 'leave_community', 'remove_member_in_community')").each do |tracker| end end end diff --git a/db/migrate/20120228202739_adapt_create_articles_activity.rb b/db/migrate/20120228202739_adapt_create_articles_activity.rb new file mode 100644 index 0000000..0c77469 --- /dev/null +++ b/db/migrate/20120228202739_adapt_create_articles_activity.rb @@ -0,0 +1,24 @@ +class AdaptCreateArticlesActivity < ActiveRecord::Migration + + # Removing 'create_article' activities that grouped articles. + # Creating new activities only to recent articles (not grouping) + def self.up + select_all("SELECT id FROM action_tracker WHERE verb = 'create_article'").each do |tracker| + activity = ActionTracker::Record.find_by_id(tracker['id']) + if activity + activity.destroy + end + end + + select_all("SELECT id FROM articles").each do |art| + article = Article.find(art['id']) + if article && article.created_at >= 8.days.ago && article.author && article.author.kind_of?(Person) + article.create_activity + end + end + end + + def self.down + say "this migration can't be reverted" + end +end diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 6c1cc6d..252de5c 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -976,17 +976,6 @@ assert_equal 'bla', profile.articles.map(&:comments_count) assert_not_equal time, ActionTracker::Record.last.updated_at end - should 'update action when comment is created' do - article = create(TinyMceArticle, :profile => profile) - action = article.activity - time = action.updated_at - - Time.stubs(:now).returns(time + 1.day) - - comment = create(Comment, :source => article, :author => profile) - assert_equal time + 1.day, article.activity.updated_at - end - should 'notifiable is false by default' do a = fast_create(Article) assert !a.notifiable? @@ -1013,6 +1002,15 @@ assert_equal 'bla', profile.articles.map(&:comments_count) assert_equal 0, ActionTracker::Record.count end + should 'create activity' do + a = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true + a.activity.destroy + assert_nil a.activity + + a.create_activity + assert_not_nil a.activity + end + should "the action_tracker_target method be defined" do assert Article.method_defined?(:action_tracker_target) end @@ -1100,39 +1098,23 @@ assert_equal 'bla', profile.articles.map(&:comments_count) end should 'create the notification to the friend when one friend has the notification and the other no' do - p1 = Person.first || fast_create(Person) - f1 = fast_create(Person) - p1.add_friend(f1) Article.destroy_all ActionTracker::Record.destroy_all ActionTrackerNotification.destroy_all - article = TinyMceArticle.create! :name => 'Tracked Article 1', :profile_id => p1.id - assert article.published? - assert_kind_of Person, article.profile - assert_equal 1, ActionTracker::Record.count - ta = ActionTracker::Record.first - assert_equal 'Tracked Article 1', ta.get_name.last - assert_equal article.url, ta.get_url.last - assert p1, ta.user - assert p1, ta.target + + f1 = fast_create(Person) + profile.add_friend(f1) + article = TinyMceArticle.create! :name => 'Tracked Article 1', :profile_id => profile.id + assert_equal 1, ActionTracker::Record.find_all_by_verb('create_article').count process_delayed_job_queue - assert_equal 2, ActionTrackerNotification.count + assert_equal 2, ActionTrackerNotification.find_all_by_action_tracker_id(article.activity.id).count f2 = fast_create(Person) - p1.add_friend(f2) - process_delayed_job_queue - assert_equal 5, ActionTrackerNotification.count - article = TinyMceArticle.create! :name => 'Tracked Article 2', :profile_id => p1.id - assert article.published? - assert_kind_of Person, article.profile - assert_equal 2, ActionTracker::Record.count - ta = ActionTracker::Record.first - assert_equal 'Tracked Article 2', ta.get_name.last - assert_equal article.url, ta.get_url.last - assert_equal p1, ta.user - assert_equal p1, ta.target + profile.add_friend(f2) + article2 = TinyMceArticle.create! :name => 'Tracked Article 2', :profile_id => profile.id + assert_equal 2, ActionTracker::Record.find_all_by_verb('create_article').count process_delayed_job_queue - assert_equal 6, ActionTrackerNotification.count + assert_equal 3, ActionTrackerNotification.find_all_by_action_tracker_id(article2.activity.id).count end should 'found articles with published date between a range' do diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 3e33172..f70dad3 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -65,12 +65,10 @@ class CommentTest < ActiveSupport::TestCase should 'update counter cache in article' do owner = create_user('testuser').person - art = owner.articles.build(:name => 'ytest'); art.save! - + art = create(TextileArticle, :profile_id => owner.id) cc = art.comments_count - art.comments.build(:title => 'test comment', :body => 'anything', :author => owner).save! - art = Article.find(art.id) + comment = create(Comment, :source => art, :author_id => owner.id) assert_equal cc + 1, Article.find(art.id).comments_count end @@ -84,6 +82,18 @@ class CommentTest < ActiveSupport::TestCase assert_equal cc + 1, ActionTracker::Record.find(action.id).comments_count end + should 'update counter cache in general activity when add a comment' do + person = fast_create(Person) + community = fast_create(Community) + + activity = ActionTracker::Record.create :user => person, :target => community, :verb => 'add_member_in_community' + + cc = activity.comments_count + + comment = create(Comment, :source => activity, :author_id => person.id) + assert_equal cc + 1, ActionTracker::Record.find(activity.id).comments_count + end + should 'provide author name for authenticated authors' do owner = create_user('testuser').person assert_equal 'testuser', Comment.new(:author => owner).author_name @@ -329,7 +339,29 @@ class CommentTest < ActiveSupport::TestCase assert c.rejected? end - should 'update article activity when add a comment' should 'update activity when add a comment' - should 'create a new activity when add a comment and the activity was removed' + + should 'update article activity when add a comment' do + profile = create_user('testuser').person + article = create(TinyMceArticle, :profile => profile) + action = article.activity + time = action.updated_at + + Time.stubs(:now).returns(time + 1.day) + + comment = create(Comment, :source => article, :author => profile) + assert_equal time + 1.day, article.activity.updated_at + end + + should 'create a new activity when add a comment and the activity was removed' do + profile = create_user('testuser').person + article = create(TinyMceArticle, :profile => profile) + article.activity.destroy + + assert_nil article.activity + + comment = create(Comment, :source => article, :author => profile) + assert_not_nil article.activity + end + end -- libgit2 0.21.2