Commit 003903846d4fc92edf102055b6e2458c47000412

Authored by Daniela Feitosa
1 parent e3f53121

AI1826

Removed old create_article activities
Created new actvities for articles created on the last 8 days
Create a new activity for article if activity was removed
Fixed tests
app/models/article.rb
@@ -561,6 +561,12 @@ class Article < ActiveRecord::Base @@ -561,6 +561,12 @@ class Article < ActiveRecord::Base
561 ActionTracker::Record.find_by_target_type_and_target_id 'Article', self.id 561 ActionTracker::Record.find_by_target_type_and_target_id 'Article', self.id
562 end 562 end
563 563
  564 + def create_activity
  565 + if is_trackable? && !image?
  566 + save_action_for_verb 'create_article', [:name, :url, :lead, :first_image], Proc.new{}, :author
  567 + end
  568 + end
  569 +
564 def first_image 570 def first_image
565 img = Hpricot(self.lead.to_s).search('img[@src]').first || Hpricot(self.body.to_s).search('img').first 571 img = Hpricot(self.lead.to_s).search('img[@src]').first || Hpricot(self.body.to_s).search('img').first
566 img.nil? ? '' : img.attributes['src'] 572 img.nil? ? '' : img.attributes['src']
app/models/comment.rb
@@ -80,9 +80,12 @@ class Comment < ActiveRecord::Base @@ -80,9 +80,12 @@ class Comment < ActiveRecord::Base
80 Comment::Notifier.deliver_mail(comment) 80 Comment::Notifier.deliver_mail(comment)
81 end 81 end
82 82
83 - if comment.source.kind_of?(Article) && comment.article.activity  
84 - comment.article.activity.increment!(:comments_count)  
85 - comment.article.activity.update_attribute(:visible, true) 83 + if comment.source.kind_of?(Article)
  84 + comment.article.create_activity if comment.article.activity.nil?
  85 + if comment.article.activity
  86 + comment.article.activity.increment!(:comments_count)
  87 + comment.article.activity.update_attribute(:visible, true)
  88 + end
86 end 89 end
87 end 90 end
88 91
db/migrate/20111228202739_remove_useless_tracked_actions.rb
@@ -7,7 +7,6 @@ class RemoveUselessTrackedActions < ActiveRecord::Migration @@ -7,7 +7,6 @@ class RemoveUselessTrackedActions < ActiveRecord::Migration
7 if (activity.updated_at.to_time < Time.now.months_ago(3)) || verbs.include?(activity.verb) 7 if (activity.updated_at.to_time < Time.now.months_ago(3)) || verbs.include?(activity.verb)
8 activity.destroy 8 activity.destroy
9 end 9 end
10 - # 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|  
11 end 10 end
12 end 11 end
13 end 12 end
db/migrate/20120228202739_adapt_create_articles_activity.rb 0 → 100644
@@ -0,0 +1,24 @@ @@ -0,0 +1,24 @@
  1 +class AdaptCreateArticlesActivity < ActiveRecord::Migration
  2 +
  3 + # Removing 'create_article' activities that grouped articles.
  4 + # Creating new activities only to recent articles (not grouping)
  5 + def self.up
  6 + select_all("SELECT id FROM action_tracker WHERE verb = 'create_article'").each do |tracker|
  7 + activity = ActionTracker::Record.find_by_id(tracker['id'])
  8 + if activity
  9 + activity.destroy
  10 + end
  11 + end
  12 +
  13 + select_all("SELECT id FROM articles").each do |art|
  14 + article = Article.find(art['id'])
  15 + if article && article.created_at >= 8.days.ago && article.author && article.author.kind_of?(Person)
  16 + article.create_activity
  17 + end
  18 + end
  19 + end
  20 +
  21 + def self.down
  22 + say "this migration can't be reverted"
  23 + end
  24 +end
test/unit/article_test.rb
@@ -976,17 +976,6 @@ assert_equal &#39;bla&#39;, profile.articles.map(&amp;:comments_count) @@ -976,17 +976,6 @@ assert_equal &#39;bla&#39;, profile.articles.map(&amp;:comments_count)
976 assert_not_equal time, ActionTracker::Record.last.updated_at 976 assert_not_equal time, ActionTracker::Record.last.updated_at
977 end 977 end
978 978
979 - should 'update action when comment is created' do  
980 - article = create(TinyMceArticle, :profile => profile)  
981 - action = article.activity  
982 - time = action.updated_at  
983 -  
984 - Time.stubs(:now).returns(time + 1.day)  
985 -  
986 - comment = create(Comment, :source => article, :author => profile)  
987 - assert_equal time + 1.day, article.activity.updated_at  
988 - end  
989 -  
990 should 'notifiable is false by default' do 979 should 'notifiable is false by default' do
991 a = fast_create(Article) 980 a = fast_create(Article)
992 assert !a.notifiable? 981 assert !a.notifiable?
@@ -1013,6 +1002,15 @@ assert_equal &#39;bla&#39;, profile.articles.map(&amp;:comments_count) @@ -1013,6 +1002,15 @@ assert_equal &#39;bla&#39;, profile.articles.map(&amp;:comments_count)
1013 assert_equal 0, ActionTracker::Record.count 1002 assert_equal 0, ActionTracker::Record.count
1014 end 1003 end
1015 1004
  1005 + should 'create activity' do
  1006 + a = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
  1007 + a.activity.destroy
  1008 + assert_nil a.activity
  1009 +
  1010 + a.create_activity
  1011 + assert_not_nil a.activity
  1012 + end
  1013 +
1016 should "the action_tracker_target method be defined" do 1014 should "the action_tracker_target method be defined" do
1017 assert Article.method_defined?(:action_tracker_target) 1015 assert Article.method_defined?(:action_tracker_target)
1018 end 1016 end
@@ -1100,39 +1098,23 @@ assert_equal &#39;bla&#39;, profile.articles.map(&amp;:comments_count) @@ -1100,39 +1098,23 @@ assert_equal &#39;bla&#39;, profile.articles.map(&amp;:comments_count)
1100 end 1098 end
1101 1099
1102 should 'create the notification to the friend when one friend has the notification and the other no' do 1100 should 'create the notification to the friend when one friend has the notification and the other no' do
1103 - p1 = Person.first || fast_create(Person)  
1104 - f1 = fast_create(Person)  
1105 - p1.add_friend(f1)  
1106 Article.destroy_all 1101 Article.destroy_all
1107 ActionTracker::Record.destroy_all 1102 ActionTracker::Record.destroy_all
1108 ActionTrackerNotification.destroy_all 1103 ActionTrackerNotification.destroy_all
1109 - article = TinyMceArticle.create! :name => 'Tracked Article 1', :profile_id => p1.id  
1110 - assert article.published?  
1111 - assert_kind_of Person, article.profile  
1112 - assert_equal 1, ActionTracker::Record.count  
1113 - ta = ActionTracker::Record.first  
1114 - assert_equal 'Tracked Article 1', ta.get_name.last  
1115 - assert_equal article.url, ta.get_url.last  
1116 - assert p1, ta.user  
1117 - assert p1, ta.target 1104 +
  1105 + f1 = fast_create(Person)
  1106 + profile.add_friend(f1)
  1107 + article = TinyMceArticle.create! :name => 'Tracked Article 1', :profile_id => profile.id
  1108 + assert_equal 1, ActionTracker::Record.find_all_by_verb('create_article').count
1118 process_delayed_job_queue 1109 process_delayed_job_queue
1119 - assert_equal 2, ActionTrackerNotification.count 1110 + assert_equal 2, ActionTrackerNotification.find_all_by_action_tracker_id(article.activity.id).count
1120 1111
1121 f2 = fast_create(Person) 1112 f2 = fast_create(Person)
1122 - p1.add_friend(f2)  
1123 - process_delayed_job_queue  
1124 - assert_equal 5, ActionTrackerNotification.count  
1125 - article = TinyMceArticle.create! :name => 'Tracked Article 2', :profile_id => p1.id  
1126 - assert article.published?  
1127 - assert_kind_of Person, article.profile  
1128 - assert_equal 2, ActionTracker::Record.count  
1129 - ta = ActionTracker::Record.first  
1130 - assert_equal 'Tracked Article 2', ta.get_name.last  
1131 - assert_equal article.url, ta.get_url.last  
1132 - assert_equal p1, ta.user  
1133 - assert_equal p1, ta.target 1113 + profile.add_friend(f2)
  1114 + article2 = TinyMceArticle.create! :name => 'Tracked Article 2', :profile_id => profile.id
  1115 + assert_equal 2, ActionTracker::Record.find_all_by_verb('create_article').count
1134 process_delayed_job_queue 1116 process_delayed_job_queue
1135 - assert_equal 6, ActionTrackerNotification.count 1117 + assert_equal 3, ActionTrackerNotification.find_all_by_action_tracker_id(article2.activity.id).count
1136 end 1118 end
1137 1119
1138 should 'found articles with published date between a range' do 1120 should 'found articles with published date between a range' do
test/unit/comment_test.rb
@@ -65,12 +65,10 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -65,12 +65,10 @@ class CommentTest &lt; ActiveSupport::TestCase
65 65
66 should 'update counter cache in article' do 66 should 'update counter cache in article' do
67 owner = create_user('testuser').person 67 owner = create_user('testuser').person
68 - art = owner.articles.build(:name => 'ytest'); art.save!  
69 - 68 + art = create(TextileArticle, :profile_id => owner.id)
70 cc = art.comments_count 69 cc = art.comments_count
71 - art.comments.build(:title => 'test comment', :body => 'anything', :author => owner).save!  
72 - art = Article.find(art.id)  
73 70
  71 + comment = create(Comment, :source => art, :author_id => owner.id)
74 assert_equal cc + 1, Article.find(art.id).comments_count 72 assert_equal cc + 1, Article.find(art.id).comments_count
75 end 73 end
76 74
@@ -84,6 +82,18 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -84,6 +82,18 @@ class CommentTest &lt; ActiveSupport::TestCase
84 assert_equal cc + 1, ActionTracker::Record.find(action.id).comments_count 82 assert_equal cc + 1, ActionTracker::Record.find(action.id).comments_count
85 end 83 end
86 84
  85 + should 'update counter cache in general activity when add a comment' do
  86 + person = fast_create(Person)
  87 + community = fast_create(Community)
  88 +
  89 + activity = ActionTracker::Record.create :user => person, :target => community, :verb => 'add_member_in_community'
  90 +
  91 + cc = activity.comments_count
  92 +
  93 + comment = create(Comment, :source => activity, :author_id => person.id)
  94 + assert_equal cc + 1, ActionTracker::Record.find(activity.id).comments_count
  95 + end
  96 +
87 should 'provide author name for authenticated authors' do 97 should 'provide author name for authenticated authors' do
88 owner = create_user('testuser').person 98 owner = create_user('testuser').person
89 assert_equal 'testuser', Comment.new(:author => owner).author_name 99 assert_equal 'testuser', Comment.new(:author => owner).author_name
@@ -329,7 +339,29 @@ class CommentTest &lt; ActiveSupport::TestCase @@ -329,7 +339,29 @@ class CommentTest &lt; ActiveSupport::TestCase
329 assert c.rejected? 339 assert c.rejected?
330 end 340 end
331 341
332 - should 'update article activity when add a comment'  
333 should 'update activity when add a comment' 342 should 'update activity when add a comment'
334 - should 'create a new activity when add a comment and the activity was removed' 343 +
  344 + should 'update article activity when add a comment' do
  345 + profile = create_user('testuser').person
  346 + article = create(TinyMceArticle, :profile => profile)
  347 + action = article.activity
  348 + time = action.updated_at
  349 +
  350 + Time.stubs(:now).returns(time + 1.day)
  351 +
  352 + comment = create(Comment, :source => article, :author => profile)
  353 + assert_equal time + 1.day, article.activity.updated_at
  354 + end
  355 +
  356 + should 'create a new activity when add a comment and the activity was removed' do
  357 + profile = create_user('testuser').person
  358 + article = create(TinyMceArticle, :profile => profile)
  359 + article.activity.destroy
  360 +
  361 + assert_nil article.activity
  362 +
  363 + comment = create(Comment, :source => article, :author => profile)
  364 + assert_not_nil article.activity
  365 + end
  366 +
335 end 367 end