diff --git a/app/models/scrap.rb b/app/models/scrap.rb index 9967374..50f497e 100644 --- a/app/models/scrap.rb +++ b/app/models/scrap.rb @@ -11,7 +11,7 @@ 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} + 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| diff --git a/lib/notify_activity_to_profiles_job.rb b/lib/notify_activity_to_profiles_job.rb index 39dd451..39cdb05 100644 --- a/lib/notify_activity_to_profiles_job.rb +++ b/lib/notify_activity_to_profiles_job.rb @@ -19,7 +19,7 @@ class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id) ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select f.friend_id, #{tracked_action.id} from friendships as f where person_id=#{tracked_action.user.id} and f.friend_id not in (select atn.profile_id from action_tracker_notifications as atn where atn.action_tracker_id = #{tracked_action.id})") - if target.is_a?(Community) + if target.is_a?(Community) || (target.is_a?(Article) && target.profile.is_a?(Community)) ActionTrackerNotification.connection.execute("insert into action_tracker_notifications(profile_id, action_tracker_id) select distinct profiles.id, #{tracked_action.id} from role_assignments, profiles where profiles.type = 'Person' and profiles.id = role_assignments.accessor_id and profiles.id != #{tracked_action.user.id} and role_assignments.resource_type = 'Profile' and role_assignments.resource_id = #{target.id}") ActionTrackerNotification.create(:profile_id => target.id, :action_tracker_id => tracked_action.id) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb) diff --git a/test/unit/action_tracker_notification_test.rb b/test/unit/action_tracker_notification_test.rb index 5784fc7..fa1a0bf 100644 --- a/test/unit/action_tracker_notification_test.rb +++ b/test/unit/action_tracker_notification_test.rb @@ -77,11 +77,27 @@ class ActionTrackerNotificationTest < ActiveSupport::TestCase end should "have comments through action_tracker" do - action = fast_create(ActionTracker::Record) - notification = fast_create(ActionTrackerNotification, :action_tracker_id => action.id, :profile_id => 1) + person = fast_create(Person) + community = fast_create(Community) + community.add_member(person) + activity = ActionTracker::Record.last + process_delayed_job_queue + notification = ActionTrackerNotification.last + + comment = create(Comment, :source => activity, :author => person) + + assert_equal activity.comments, notification.comments + end + + should "have comments through article action_tracker" do + person = fast_create(Person) + article = create(TextileArticle, :profile_id => person.id) + process_delayed_job_queue + notification = ActionTrackerNotification.last + + comment = create(Comment, :source => article, :author => person) - comment = fast_create(Comment, :source_id => action.id) - assert_equal action.comments, notification.comments + assert_equal article.activity.comments, notification.comments end end diff --git a/test/unit/approve_article_test.rb b/test/unit/approve_article_test.rb index 13f6ff1..a57fb88 100644 --- a/test/unit/approve_article_test.rb +++ b/test/unit/approve_article_test.rb @@ -250,7 +250,7 @@ class ApproveArticleTest < ActiveSupport::TestCase assert_equal 1, ActionTracker::Record.count end - should 'notify with different trackers activity create with different targets' do + should 'not group trackers activity of article\'s creation' do ActionTracker::Record.delete_all article = fast_create(TextileArticle) @@ -260,28 +260,29 @@ class ApproveArticleTest < ActiveSupport::TestCase article = fast_create(TextileArticle) a = ApproveArticle.create!(:name => 'another bar', :article => article, :target => community, :requestor => profile) a.finish - assert_equal 1, ActionTracker::Record.count article = fast_create(TextileArticle) other_community = fast_create(Community) a = ApproveArticle.create!(:name => 'another bar', :article => article, :target => other_community, :requestor => profile) a.finish - assert_equal 2, ActionTracker::Record.count + assert_equal 3, ActionTracker::Record.count end - should 'notify activity on update' do + should 'update activity on update of an article' do ActionTracker::Record.delete_all a = ApproveArticle.create!(:name => 'bar', :article => article, :target => community, :requestor => profile) a.finish - assert_equal 1, ActionTracker::Record.count - - published = article.class.last - published.name = 'foo' - published.save! - assert_equal 2, ActionTracker::Record.count + published = community.articles.find_by_name(a.name) + time = published.activity.updated_at + Time.stubs(:now).returns(time + 1.day) + assert_no_difference ActionTracker::Record, :count do + published.name = 'foo' + published.save! + end + assert_equal time + 1.day, published.activity.updated_at end - should 'notify with different trackers activity update with different targets' do + should 'not create trackers activity when updating articles' do ActionTracker::Record.delete_all article1 = fast_create(TextileArticle) a = ApproveArticle.create!(:name => 'bar', :article => article1, :target => community, :requestor => profile) @@ -293,16 +294,16 @@ class ApproveArticleTest < ActiveSupport::TestCase a.finish assert_equal 2, ActionTracker::Record.count - published = article1.class.last - published.name = 'foo';published.save! - assert_equal 3, ActionTracker::Record.count - - published = article2.class.last - published.name = 'another foo';published.save! - assert_equal 4, ActionTracker::Record.count + assert_no_difference ActionTracker::Record, :count do + published = article1.class.last + published.name = 'foo';published.save! + + published = article2.class.last + published.name = 'another foo';published.save! + end end - should "the tracker action target be defined as Community by custom_target method on articles'creation in communities" do + should "the tracker action target be defined as the article on articles'creation in communities" do ActionTracker::Record.delete_all person = fast_create(Person) community.add_member(person) @@ -310,17 +311,21 @@ class ApproveArticleTest < ActiveSupport::TestCase a = ApproveArticle.create!(:article => article, :target => community, :requestor => profile) a.finish - assert_equal Community, ActionTracker::Record.last.target.class + approved_article = community.articles.find_by_name(article.name) + + assert_equal approved_article, ActionTracker::Record.last.target end - should "the tracker action target be defined as person by custom_target method on articles'creation in profile" do + should "the tracker action target be defined as the article on articles'creation in profile" do ActionTracker::Record.delete_all person = fast_create(Person) a = ApproveArticle.create!(:article => article, :target => person, :requestor => profile) a.finish - assert_equal Person, ActionTracker::Record.last.target.class + approved_article = person.articles.find_by_name(article.name) + + assert_equal approved_article, ActionTracker::Record.last.target end should "have the same is_trackable method as original article" do diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb index 7fe35a3..376fcfc 100644 --- a/test/unit/article_test.rb +++ b/test/unit/article_test.rb @@ -934,66 +934,31 @@ class ArticleTest < ActiveSupport::TestCase end should 'track action when a published article is created outside a community' do - article = TinyMceArticle.create! :name => 'Tracked Article', :profile_id => profile.id - assert article.published? - assert_kind_of Person, article.profile - ta = ActionTracker::Record.last - assert_equal 'Tracked Article', ta.get_name.last - assert_equal article.url, ta.get_url.last - assert_kind_of Person, ta.user - ta.created_at = Time.now.ago(26.hours); ta.save! - article = TinyMceArticle.create! :name => 'Another Tracked Article', :profile_id => profile.id - ta = ActionTracker::Record.last - assert_equal ['Another Tracked Article'], ta.get_name - assert_equal [article.url], ta.get_url + article = create(TinyMceArticle, :profile_id => profile.id) + ta = article.activity + assert_equal article.name, ta.get_name + assert_equal article.url, ta.get_url end should 'track action when a published article is created in a community' do community = fast_create(Community) - p1 = ActionTracker::Record.current_user_from_model + p1 = fast_create(Person) p2 = fast_create(Person) p3 = fast_create(Person) community.add_member(p1) community.add_member(p2) - assert p1.is_member_of?(community) - assert p2.is_member_of?(community) - assert !p3.is_member_of?(community) Article.destroy_all ActionTracker::Record.destroy_all - article = TinyMceArticle.create! :name => 'Tracked Article', :profile_id => community.id - assert article.published? - assert_kind_of Community, article.profile - ta = ActionTracker::Record.last - assert_equal 'Tracked Article', ta.get_name.last - assert_equal article.url, ta.get_url.last - assert_kind_of Person, ta.user + UserStampSweeper.any_instance.expects(:current_user).returns(p1).at_least_once + article = create(TinyMceArticle, :profile_id => community.id) + process_delayed_job_queue - assert_equal 3, ActionTrackerNotification.count + assert_equal 3, ActionTrackerNotification.all ActionTrackerNotification.all.map{|a|a.profile}.map do |profile| assert [p1,p2,community].include?(profile) end end - should 'track action when a published article is updated' do - a = TinyMceArticle.create! :name => 'a', :profile_id => profile.id - a.update_attributes! :name => 'b' - ta = ActionTracker::Record.last - assert_equal ['b'], ta.get_name - assert_equal [a.reload.url], ta.get_url - a.update_attributes! :name => 'c' - ta = ActionTracker::Record.last - assert_equal ['b','c'], ta.get_name - assert_equal [a.url,a.reload.url], ta.get_url - a.update_attributes! :body => 'test' - ta = ActionTracker::Record.last - assert_equal ['b','c','c'], ta.get_name - assert_equal [a.url,a.reload.url,a.reload.url], ta.get_url - a.update_attributes! :hits => 50 - ta = ActionTracker::Record.last - assert_equal ['b','c','c'], ta.get_name - assert_equal [a.url,a.reload.url,a.reload.url], ta.get_url - end - should 'track action when a published article is removed' do a = TinyMceArticle.create! :name => 'a', :profile_id => profile.id a.destroy @@ -1022,13 +987,13 @@ class ArticleTest < ActiveSupport::TestCase should 'update action when comment is created' do article = create(TinyMceArticle, :profile => profile) - action = ActionTracker::Record.last + action = article.activity time = action.updated_at Time.stubs(:now).returns(time + 1.day) - article.comments << Comment.create(:name => 'Guest', :email => 'guest@example.com', :title => 'test comment', :body => 'hello!') - assert_not_equal time, ActionTracker::Record.last.updated_at + comment = create(Comment, :source => article, :author => profile) + assert_equal time + 1.day, article.activity.updated_at end should 'notifiable is false by default' do @@ -1162,36 +1127,17 @@ class ArticleTest < ActiveSupport::TestCase assert_equal 6, ActionTrackerNotification.count end - should 'not create more than one notification track action to friends when update more than one artile' do - p1 = Person.first || fast_create(Person) + should 'create notifications to friends when creating an article' do friend = fast_create(Person) - p1.add_friend(friend) + profile.add_friend(friend) 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.last - 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 - process_delayed_job_queue - assert_equal 2, ActionTrackerNotification.count + UserStampSweeper.any_instance.expects(:current_user).returns(profile).at_least_once + article = create(TinyMceArticle, :profile_id => profile.id) - article = TinyMceArticle.create! :name => 'Tracked Article 2', :profile_id => p1.id - assert article.published? - assert_kind_of Person, article.profile - assert_equal 1, ActionTracker::Record.count - ta = ActionTracker::Record.last - 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 process_delayed_job_queue - assert_equal 2, ActionTrackerNotification.count + assert_equal friend, ActionTrackerNotification.last.profile end should 'create the notification to the friend when one friend has the notification and the other no' do @@ -1582,7 +1528,7 @@ class ArticleTest < ActiveSupport::TestCase should 'survive to a invalid src attribute while looking for images in body' do article = Article.new(:body => "An article with invalid src in img tag ", :profile => @profile) assert_nothing_raised URI::InvalidURIError do - assert_equal ['http://localhost/path%20with%20spaces.png'], article.body_images_paths + assert_equal ["http://#{profile.environment.default_hostname}/path%20with%20spaces.png"], article.body_images_paths end end diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb index 349d9e5..d7ff6b2 100644 --- a/test/unit/comment_test.rb +++ b/test/unit/comment_test.rb @@ -74,11 +74,13 @@ class CommentTest < ActiveSupport::TestCase assert_equal cc + 1, Article.find(art.id).comments_count end - should 'update counter cache in activity' do - action = fast_create(ActionTracker::Record) - cc = action.comments_count - comment = fast_create(Comment, :source_id => action.id) + should 'update counter cache in article activity' do + owner = create_user('testuser').person + article = create(TextileArticle, :profile_id => owner.id) + action = article.activity + cc = action.comments_count + comment = create(Comment, :source => action, :author_id => owner.id) assert_equal cc + 1, ActionTracker::Record.find(action.id).comments_count end diff --git a/test/unit/community_test.rb b/test/unit/community_test.rb index ac8175d..798b658 100644 --- a/test/unit/community_test.rb +++ b/test/unit/community_test.rb @@ -345,7 +345,7 @@ class CommunityTest < ActiveSupport::TestCase scrap = Scrap.create!(defaults_for_scrap(:sender => person, :receiver => community, :content => 'A scrap')) activity = ActionTracker::Record.last - assert_equal [scrap], community.activities.map { |a| a.klass.constantize.find(a.id) } + assert_equal [activity,scrap], community.activities.map { |a| a.klass.constantize.find(a.id) } end should 'return tracked_actions of community as activities' do diff --git a/test/unit/notify_activity_to_profiles_job_test.rb b/test/unit/notify_activity_to_profiles_job_test.rb index 24de4a4..aacaa14 100644 --- a/test/unit/notify_activity_to_profiles_job_test.rb +++ b/test/unit/notify_activity_to_profiles_job_test.rb @@ -24,28 +24,6 @@ class NotifyActivityToProfilesJobTest < ActiveSupport::TestCase end end - should 'notify just the community in tracker with remove_member_in_community verb' do - person = fast_create(Person) - community = fast_create(Community) - action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Profile', :target_id => community.id, :verb => 'remove_member_in_community') - assert NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY.include?(action_tracker.verb) - p1, p2, m1, m2 = fast_create(Person), fast_create(Person), fast_create(Person), fast_create(Person) - fast_create(Friendship, :person_id => person.id, :friend_id => p1.id) - fast_create(Friendship, :person_id => person.id, :friend_id => p2.id) - fast_create(RoleAssignment, :accessor_id => m1.id, :role_id => 3, :resource_id => community.id) - fast_create(RoleAssignment, :accessor_id => m2.id, :role_id => 3, :resource_id => community.id) - ActionTrackerNotification.delete_all - job = NotifyActivityToProfilesJob.new(action_tracker.id) - job.perform - process_delayed_job_queue - - assert_equal 1, ActionTrackerNotification.count - [community].each do |profile| - notification = ActionTrackerNotification.find_by_profile_id profile.id - assert_equal action_tracker, notification.action_tracker - end - end - should 'notify just the users and his friends tracking user actions' do person = fast_create(Person) community = fast_create(Community) @@ -132,36 +110,14 @@ class NotifyActivityToProfilesJobTest < ActiveSupport::TestCase end end - should 'not notify the community tracking leave_community verb' do - person = fast_create(Person) - community = fast_create(Community) - action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Profile', :target_id => community.id, :verb => 'leave_community') - assert !NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY.include?(action_tracker.verb) - p1, p2, m1, m2 = fast_create(Person), fast_create(Person), fast_create(Person), fast_create(Person) - fast_create(Friendship, :person_id => person.id, :friend_id => p1.id) - fast_create(Friendship, :person_id => person.id, :friend_id => p2.id) - fast_create(RoleAssignment, :accessor_id => m1.id, :role_id => 3, :resource_id => community.id) - fast_create(RoleAssignment, :accessor_id => m2.id, :role_id => 3, :resource_id => community.id) - ActionTrackerNotification.delete_all - job = NotifyActivityToProfilesJob.new(action_tracker.id) - job.perform - process_delayed_job_queue - - assert_equal 5, ActionTrackerNotification.count - [person, p1, p2, m1, m2].each do |profile| - notification = ActionTrackerNotification.find_by_profile_id profile.id - assert_equal action_tracker, notification.action_tracker - end - end - should "the NOTIFY_ONLY_COMMUNITY constant has all the verbs tested" do - notify_community_verbs = ['add_member_in_community', 'remove_member_in_community'] + notify_community_verbs = ['add_member_in_community'] assert_equal [], notify_community_verbs - NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY assert_equal [], NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY - notify_community_verbs end should "the NOT_NOTIFY_COMMUNITY constant has all the verbs tested" do - not_notify_community_verbs = ['join_community', 'leave_community'] + not_notify_community_verbs = ['join_community'] assert_equal [], not_notify_community_verbs - NotifyActivityToProfilesJob::NOT_NOTIFY_COMMUNITY assert_equal [], NotifyActivityToProfilesJob::NOT_NOTIFY_COMMUNITY - not_notify_community_verbs end diff --git a/test/unit/textile_article_test.rb b/test/unit/textile_article_test.rb index a927789..92b43ad 100644 --- a/test/unit/textile_article_test.rb +++ b/test/unit/textile_article_test.rb @@ -38,80 +38,72 @@ class TextileArticleTest < ActiveSupport::TestCase assert_equal 1, ActionTracker::Record.count end - should 'notify with different trackers activity create with different targets' do + should 'not group trackers activity of article\'s creation' do ActionTracker::Record.delete_all profile = fast_create(Profile) TextileArticle.create! :name => 'bar', :profile_id => profile.id, :published => true TextileArticle.create! :name => 'another bar', :profile_id => profile.id, :published => true - assert_equal 1, ActionTracker::Record.count TextileArticle.create! :name => 'another bar', :profile_id => fast_create(Profile).id, :published => true - assert_equal 2, ActionTracker::Record.count + assert_equal 3, ActionTracker::Record.count end - should 'notify activity on update' do + should 'update activity on update of an article' do ActionTracker::Record.delete_all - a = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true - assert_equal 1, ActionTracker::Record.count - a.name = 'foo' - a.save! - assert_equal 2, ActionTracker::Record.count + profile = fast_create(Profile) + article = create(TextileArticle, :profile_id => profile.id) + time = article.activity.updated_at + Time.stubs(:now).returns(time + 1.day) + assert_no_difference ActionTracker::Record, :count do + article.name = 'foo' + article.save! + end + assert_equal time + 1.day, article.activity.updated_at end - should 'notify with different trackers activity update with different targets' do + should 'not create trackers activity when updating articles' do ActionTracker::Record.delete_all a1 = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true a2 = TextileArticle.create! :name => 'another bar', :profile_id => fast_create(Profile).id, :published => true - assert_equal 2, ActionTracker::Record.count - a1.name = 'foo' - a1.save! - assert_equal 3, ActionTracker::Record.count - a2.name = 'another foo' - a2.save! - assert_equal 4, ActionTracker::Record.count + assert_no_difference ActionTracker::Record, :count do + a1.name = 'foo';a1.save! + a2.name = 'another foo';a2.save! + end end - should 'notify activity on destroy' do + should 'not notify activity on destroy' do ActionTracker::Record.delete_all a = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true - assert_equal 1, ActionTracker::Record.count - a.destroy - assert_equal 2, ActionTracker::Record.count + assert_no_difference ActionTracker::Record, :count do + a.destroy + end end - should 'notify different activities when destroy articles with diferrents targets' do + should 'not notify when an article is destroyed' do ActionTracker::Record.delete_all a1 = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true a2 = TextileArticle.create! :name => 'another bar', :profile_id => fast_create(Profile).id, :published => true assert_equal 2, ActionTracker::Record.count - a1.destroy - assert_equal 3, ActionTracker::Record.count - a2.destroy - assert_equal 4, ActionTracker::Record.count + assert_no_difference ActionTracker::Record, :count do + a1.destroy + a2.destroy + end end - should "the tracker action target be defined as Community by custom_target method on articles'creation in communities" do + should "the tracker action target be defined as the article on articles'creation in communities" do ActionTracker::Record.delete_all community = fast_create(Community) p1 = Person.first community.add_member(p1) assert p1.is_member_of?(community) article = TextileArticle.create! :name => 'test', :profile_id => community.id - assert_equal true, article.published? - assert_equal true, article.notifiable? - assert_equal false, article.image? - assert_equal Community, article.profile.class - assert_equal Community, ActionTracker::Record.last.target.class + assert_equal article, ActionTracker::Record.last.target end - should "the tracker action target be defined as person by custom_target method on articles'creation in profile" do + should "the tracker action target be defined as the article on articles'creation in profile" do ActionTracker::Record.delete_all person = Person.first article = TextileArticle.create! :name => 'test', :profile_id => person.id - assert_equal true, article.published? - assert_equal true, article.notifiable? - assert_equal false, article.image? - assert_equal Person, article.profile.class - assert_equal person, ActionTracker::Record.last.target + assert_equal article, ActionTracker::Record.last.target end should 'not notify activity if the article is not advertise' do diff --git a/test/unit/tiny_mce_article_test.rb b/test/unit/tiny_mce_article_test.rb index f2f48a7..2ea21bb 100644 --- a/test/unit/tiny_mce_article_test.rb +++ b/test/unit/tiny_mce_article_test.rb @@ -147,7 +147,7 @@ class TinyMceArticleTest < ActiveSupport::TestCase should 'update activity on update of an article' do ActionTracker::Record.delete_all profile = fast_create(Profile) - article = create(TextileArticle, :profile_id => profile.id) + article = create(TinyMceArticle, :profile_id => profile.id) time = article.activity.updated_at Time.stubs(:now).returns(time + 1.day) assert_no_difference ActionTracker::Record, :count do diff --git a/vendor/plugins/action_tracker_has_comments/init.rb b/vendor/plugins/action_tracker_has_comments/init.rb index 3b5ac8d..a41310e 100644 --- a/vendor/plugins/action_tracker_has_comments/init.rb +++ b/vendor/plugins/action_tracker_has_comments/init.rb @@ -2,7 +2,7 @@ ActionTracker::Record.module_eval do - has_many :comments, :class_name => 'Comment', :dependent => :destroy, :finder_sql => 'SELECT * FROM comments WHERE #{conditions_for_comments} ORDER BY created_at ASC' + has_many :comments, :class_name => 'Comment', :foreign_key => 'source_id', :dependent => :destroy, :finder_sql => 'SELECT * FROM comments WHERE #{conditions_for_comments} ORDER BY created_at ASC' def conditions_for_comments type, id = (self.target_type == 'Article' ? ['Article', self.target_id] : [self.class.to_s, self.id]) -- libgit2 0.21.2