diff --git a/app/models/article.rb b/app/models/article.rb
index 3849fff..8ca5a44 100644
--- a/app/models/article.rb
+++ b/app/models/article.rb
@@ -2,10 +2,9 @@ require 'hpricot'
class Article < ActiveRecord::Base
- track_actions :create_article, :after_create, :keep_params => [:name, :url], :if => Proc.new { |a| a.published? && !a.profile.is_a?(Community) && !a.image? && a.notifiable? }
- track_actions :update_article, :before_update, :keep_params => [:name, :url], :if => Proc.new { |a| a.published? && (a.body_changed? || a.name_changed?) && a.notifiable? }
- track_actions :remove_article, :before_destroy, :keep_params => [:name], :if => Proc.new { |a| a.published? && a.notifiable? }
- track_actions :publish_article_in_community, :after_create, :keep_params => ["name", "url", "profile.url", "profile.name"], :if => Proc.new { |a| a.published? && a.profile.is_a?(Community) && !a.image? && a.notifiable? }
+ 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
# xss_terminate plugin can't sanitize array fields
before_save :sanitize_tag_list
@@ -43,6 +42,10 @@ class Article < ActiveRecord::Base
validates_format_of :external_link, :with => URL_FORMAT, :if => lambda { |article| !article.external_link.blank? }
+ def is_trackable?
+ self.published? && self.notifiable? && self.advertise?
+ end
+
def external_link=(link)
if !link.blank? && link !~ /^[a-z]+:\/\//i
link = 'http://' + link
@@ -50,6 +53,9 @@ class Article < ActiveRecord::Base
self[:external_link] = link
end
+ def action_tracker_target
+ self.profile
+ end
def self.human_attribute_name(attrib)
case attrib.to_sym
diff --git a/app/models/comment.rb b/app/models/comment.rb
index b8a92ca..0bd61d7 100644
--- a/app/models/comment.rb
+++ b/app/models/comment.rb
@@ -1,6 +1,6 @@
class Comment < ActiveRecord::Base
- track_actions :leave_comment, :after_create, :keep_params => ["article.title", "article.url", "title", "url", "body"]
+ track_actions :leave_comment, :after_create, :keep_params => ["article.title", "article.url", "title", "url", "body"], :custom_target => :action_tracker_target
validates_presence_of :title, :body
belongs_to :article, :counter_cache => true
@@ -21,6 +21,10 @@ class Comment < ActiveRecord::Base
xss_terminate :only => [ :body, :title, :name ], :on => 'validation'
+ def action_tracker_target
+ self.article.profile
+ end
+
def author_name
if author
author.short_name
diff --git a/app/models/friendship.rb b/app/models/friendship.rb
index 57b2e21..8df2844 100644
--- a/app/models/friendship.rb
+++ b/app/models/friendship.rb
@@ -1,5 +1,5 @@
class Friendship < ActiveRecord::Base
- track_actions :new_friendship, :after_create, :keep_params => ["friend.name", "friend.url", "friend.profile_custom_icon"], :unless => Proc.new { |f| f.friend.is_a_friend?(f.person) }
+ track_actions :new_friendship, :after_create, :keep_params => ["friend.name", "friend.url", "friend.profile_custom_icon"], :custom_user => :person
belongs_to :person, :foreign_key => :person_id
belongs_to :friend, :class_name => 'Person', :foreign_key => 'friend_id'
diff --git a/app/models/person.rb b/app/models/person.rb
index d6edf46..ed1702b 100644
--- a/app/models/person.rb
+++ b/app/models/person.rb
@@ -327,12 +327,7 @@ class Person < Profile
end
def self.notify_activity(tracked_action)
- profile = tracked_action.dispatcher.profile if tracked_action.dispatcher.is_a?(Article)
- profile = tracked_action.dispatcher.resource if tracked_action.dispatcher.is_a?(RoleAssignment)
- profile = tracked_action.dispatcher.article.profile if tracked_action.dispatcher.is_a?(Comment)
- profile_id = profile.nil? ? nil : profile.id
- Delayed::Job.enqueue NotifyActivityToProfilesJob.new(tracked_action.id, profile_id)
- ActionTrackerNotification.create(:action_tracker => tracked_action, :profile => tracked_action.user)
+ Delayed::Job.enqueue NotifyActivityToProfilesJob.new(tracked_action.id)
end
def is_member_of?(profile)
diff --git a/app/models/published_article.rb b/app/models/published_article.rb
index a7a3653..98f09e8 100644
--- a/app/models/published_article.rb
+++ b/app/models/published_article.rb
@@ -37,4 +37,8 @@ class PublishedArticle < Article
reference_article && reference_article.abstract
end
+ def notifiable?
+ true
+ end
+
end
diff --git a/app/views/profile/_profile_network_activities.rhtml b/app/views/profile/_profile_network_activities.rhtml
index 88002eb..ed32049 100644
--- a/app/views/profile/_profile_network_activities.rhtml
+++ b/app/views/profile/_profile_network_activities.rhtml
@@ -9,6 +9,7 @@
diff --git a/config/initializers/action_tracker.rb b/config/initializers/action_tracker.rb
index 15de3cb..6c3bf2d 100644
--- a/config/initializers/action_tracker.rb
+++ b/config/initializers/action_tracker.rb
@@ -19,11 +19,6 @@ ActionTrackerConfig.verbs = {
:type => :groupable
},
- :publish_article_in_community => {
- :description => lambda { n_('published 1 article in communities: %{title}', 'published %{num} articles in communities: %{title}', get_name.size) % { :num => get_name.size, :title => '{{ta.collect_group_with_index(:name){ |n,i| link_to(truncate(n), ta.get_url[i]) + " (%s " + link_to(ta.get_profile_name[i], ta.get_profile_url[i]) + ")" }.to_sentence(:connector => "%s")}}' % [_("in"), _("and")] } },
- :type => :groupable
- },
-
:new_friendship => {
:description => lambda { n_('has made 1 new friend:
%{name}', 'has made %{num} new friends:
%{name}', get_friend_name.size) % { :num => get_friend_name.size, :name => '{{ta.collect_group_with_index(:friend_name){ |n,i| link_to(content_tag(:img, nil, :src => (ta.get_friend_profile_custom_icon[i] || default_or_themed_icon("/images/icons-app/person-icon.png"))), ta.get_friend_url[i], :title => n)}.join}}' } },
:type => :groupable
@@ -34,6 +29,14 @@ ActionTrackerConfig.verbs = {
:type => :groupable
},
+ :add_member_in_community => {
+ :description => lambda { _('has joined the community.') },
+ },
+
+ :remove_member_in_community => {
+ :description => lambda { _('has left the community.') },
+ },
+
:leave_community => {
:description => lambda { n_('has left 1 community:
%{name}', 'has left %{num} communities:
%{name}', get_resource_name.size) % { :num => get_resource_name.size, :name => '{{ta.collect_group_with_index(:resource_name){ |n,i| link_to(content_tag(:img, nil, :src => (ta.get_resource_profile_custom_icon[i] || default_or_themed_icon("/images/icons-app/community-icon.png"))), ta.get_resource_url[i], :title => n)}.join}}' } },
:type => :groupable
diff --git a/db/migrate/20100920182433_change_action_tracker_record.rb b/db/migrate/20100920182433_change_action_tracker_record.rb
new file mode 100644
index 0000000..4acc597
--- /dev/null
+++ b/db/migrate/20100920182433_change_action_tracker_record.rb
@@ -0,0 +1,13 @@
+class ChangeActionTrackerRecord < ActiveRecord::Migration
+ def self.up
+ rename_column(:action_tracker, :dispatcher_type, :target_type)
+ rename_column(:action_tracker, :dispatcher_id, :target_id)
+ ActionTracker::Record.update_all("verb='create_article'", {:verb => 'publish_article_in_community'})
+ end
+
+ def self.down
+ raise "this migration can't be reverted"
+ rename_column(:action_tracker, :target_type, :dispatcher_type)
+ rename_column(:action_tracker, :target_id, :dispatcher_id)
+ end
+end
diff --git a/lib/notify_activity_to_profiles_job.rb b/lib/notify_activity_to_profiles_job.rb
index 003eae6..96b8802 100644
--- a/lib/notify_activity_to_profiles_job.rb
+++ b/lib/notify_activity_to_profiles_job.rb
@@ -1,16 +1,32 @@
-class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id, :target_profile_id)
+class NotifyActivityToProfilesJob < Struct.new(:tracked_action_id)
+ NOTIFY_ONLY_COMMUNITY = [
+ 'add_member_in_community',
+ 'remove_member_in_community',
+ ]
+
+ NOT_NOTIFY_COMMUNITY = [
+ 'join_community',
+ 'leave_community',
+ ]
def perform
- profile = Profile.find(target_profile_id) unless target_profile_id.nil?
tracked_action = ActionTracker::Record.find(tracked_action_id)
+ target = tracked_action.target
+ if target.is_a?(Community) && NOTIFY_ONLY_COMMUNITY.include?(tracked_action.verb)
+ ActionTrackerNotification.create(:action_tracker => tracked_action, :profile => target)
+ return
+ end
+
+ ActionTrackerNotification.create(:action_tracker => tracked_action, :profile => tracked_action.user)
tracked_action.user.each_friend do |friend|
ActionTrackerNotification.create(:action_tracker => tracked_action, :profile => friend)
end
- if profile.is_a?(Community)
- profile.each_member do |member|
+
+ if target.is_a?(Community)
+ target.each_member do |member|
next if member == tracked_action.user
ActionTrackerNotification.create(:action_tracker => tracked_action, :profile => member)
end
- ActionTrackerNotification.create(:action_tracker => tracked_action, :profile => profile)
+ ActionTrackerNotification.create(:action_tracker => tracked_action, :profile => target) unless NOT_NOTIFY_COMMUNITY.include?(tracked_action.verb)
end
end
end
diff --git a/test/factories.rb b/test/factories.rb
index 61ecb4c..c295825 100644
--- a/test/factories.rb
+++ b/test/factories.rb
@@ -224,7 +224,7 @@ module Noosfero::Factory
alias :defaults_for_textile_article :defaults_for_article
alias :defaults_for_tiny_mce_article :defaults_for_article
alias :defaults_for_rss_feed :defaults_for_article
- alias :defaults_for_textile_article :defaults_for_article
+ alias :defaults_for_published_article :defaults_for_article
alias :defaults_for_folder :defaults_for_article
###############################################
@@ -381,7 +381,7 @@ module Noosfero::Factory
###############################################
def defaults_for_action_tracker_record(params = {})
- { :created_at => DateTime.now, :verb => :leave_comment, :user_type => 'Profile', :user_id => 1 }.merge(params)
+ { :created_at => DateTime.now, :verb => 'add_member_in_community', :user_type => 'Profile', :user_id => 1 }.merge(params)
end
###############################################
diff --git a/test/functional/profile_controller_test.rb b/test/functional/profile_controller_test.rb
index 4695ed7..ee1ac7c 100644
--- a/test/functional/profile_controller_test.rb
+++ b/test/functional/profile_controller_test.rb
@@ -769,7 +769,8 @@ class ProfileControllerTest < Test::Unit::TestCase
should 'the network activity be paginated' do
p1= Person.first
- 40.times{Scrap.create!(defaults_for_scrap(:sender => p1, :receiver => p1))}
+ at = fast_create(ActionTracker::Record)
+ 40.times{fast_create(ActionTrackerNotification, :action_tracker_id => at.id, :profile_id => p1.id)}
@controller.stubs(:logged_in?).returns(true)
user = mock()
diff --git a/test/unit/article_test.rb b/test/unit/article_test.rb
index aababad..7d6bc0d 100644
--- a/test/unit/article_test.rb
+++ b/test/unit/article_test.rb
@@ -1020,4 +1020,40 @@ class ArticleTest < Test::Unit::TestCase
assert_equal 0, ActionTracker::Record.count
end
+ should "the action_tracker_target method be defined" do
+ assert Article.method_defined?(:action_tracker_target)
+ end
+
+ should "the action_tracker_target method return the article profile" do
+ profile = fast_create(Person)
+ article = fast_create(Article, :profile_id => profile.id)
+ assert_equal profile, article.action_tracker_target
+
+ profile = fast_create(Community)
+ article = fast_create(Article, :profile_id => profile.id)
+ assert_equal profile, article.action_tracker_target
+ end
+
+ should "have defined the is_trackable method defined" do
+ assert Article.method_defined?(:is_trackable?)
+ end
+
+ should "the common trackable conditions return the correct value" do
+ a = Article.new
+ a.published = a.advertise = true
+ assert_equal true, a.published?
+ assert_equal false, a.notifiable?
+ assert_equal true, a.advertise?
+ assert_equal false, a.is_trackable?
+
+ a.published=false
+ assert_equal false, a.published?
+ assert_equal false, a.is_trackable?
+
+ a.published=true
+ a.advertise=false
+ assert_equal false, a.advertise?
+ assert_equal false, a.is_trackable?
+ end
+
end
diff --git a/test/unit/comment_test.rb b/test/unit/comment_test.rb
index 5e90879..86f6d6a 100644
--- a/test/unit/comment_test.rb
+++ b/test/unit/comment_test.rb
@@ -226,4 +226,16 @@ class CommentTest < Test::Unit::TestCase
assert_equal comment.url, ta.get_url
end
+ should 'have the action_tracker_target defined' do
+ assert Comment.method_defined?(:action_tracker_target)
+ end
+
+ should "have the action_tracker_target be the articles's profile" do
+ owner = create_user('testuser').person
+ article = owner.articles.create!(:name => 'test', :body => '...')
+ comment = article.comments.create!(:article => article, :name => 'foo', :title => 'bar', :body => 'my comment', :email => 'cracker@test.org')
+ ta = ActionTracker::Record.last
+ assert_equal owner, ta.target
+ end
+
end
diff --git a/test/unit/community_test.rb b/test/unit/community_test.rb
index 18382af..2d79655 100644
--- a/test/unit/community_test.rb
+++ b/test/unit/community_test.rb
@@ -242,9 +242,9 @@ class CommunityTest < Test::Unit::TestCase
RoleAssignment.delete_all
ActionTrackerNotification.delete_all
- RoleAssignment.any_instance.stubs(:role_id).returns(3)
- assert_difference(ActionTrackerNotification, :count, 3) do
+ assert_difference(ActionTrackerNotification, :count, 5) do
community.add_member(p1)
+ process_delayed_job_queue
community.add_member(p3)
assert p1.is_member_of?(community)
assert !p2.is_member_of?(community)
diff --git a/test/unit/friendship_test.rb b/test/unit/friendship_test.rb
index 5917523..da90c22 100644
--- a/test/unit/friendship_test.rb
+++ b/test/unit/friendship_test.rb
@@ -23,23 +23,27 @@ class FriendshipTest < Test::Unit::TestCase
end
should 'create tracked action' do
- f = Friendship.create! :person => create_user('a').person, :friend => create_user('b').person
+ a, b, c = create_user('a').person, create_user('b').person, create_user('c').person
+ f = Friendship.create! :person => a, :friend => b
ta = ActionTracker::Record.last
- person = Person.first
- assert_equal person.name, ta.user.name
+ assert_equal a, ta.user
assert_equal 'b', ta.get_friend_name[0]
- f = Friendship.create! :person => create_user('a').person, :friend => create_user('c').person
+ f = Friendship.create! :person => a, :friend => c
ta = ActionTracker::Record.last
- assert_equal person.name, ta.user.name
+ assert_equal a, ta.user
assert_equal 'c', ta.get_friend_name[1]
end
- should 'create tracked action only if they are not friends yet' do
+ should 'create tracked action for both people' do
a, b = create_user('a').person, create_user('b').person
f = Friendship.create! :person => a, :friend => b
- assert_equal ['b'], ActionTracker::Record.last.get_friend_name
+ ta = ActionTracker::Record.last
+ assert_equal a, ta.user
+ assert_equal ['b'], ta.get_friend_name
f = Friendship.create! :person => b, :friend => a
- assert_equal ['b'], ActionTracker::Record.last.get_friend_name
+ ta = ActionTracker::Record.last
+ assert_equal b, ta.user
+ assert_equal ['a'], ta.get_friend_name
end
end
diff --git a/test/unit/notify_activity_to_profiles_job_test.rb b/test/unit/notify_activity_to_profiles_job_test.rb
index d1f6a3d..b1ebf4d 100644
--- a/test/unit/notify_activity_to_profiles_job_test.rb
+++ b/test/unit/notify_activity_to_profiles_job_test.rb
@@ -2,24 +2,168 @@ require File.dirname(__FILE__) + '/../test_helper'
class NotifyActivityToProfilesJobTest < ActiveSupport::TestCase
- should 'create the ActionTrackerNotification' do
+ should 'notify just the community in tracker with add_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)
+ action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Profile', :target_id => community.id, :verb => 'add_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, community.id)
+ job = NotifyActivityToProfilesJob.new(action_tracker.id)
job.perform
process_delayed_job_queue
- [community, p1, p2, m1, m2].each do |profile|
+ 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 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)
+ action_tracker = fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => person.id, :target_type => 'Profile', :verb => 'create_article')
+ 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(Friendship, :person_id => p1.id, :friend_id => m1.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 3, ActionTrackerNotification.count
+ [person, p1, p2].each do |profile|
+ notification = ActionTrackerNotification.find_by_profile_id profile.id
+ assert_equal action_tracker, notification.action_tracker
+ end
+ end
+
+ should 'not notify the communities members' 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 => 'create_article')
+ assert !NotifyActivityToProfilesJob::NOTIFY_ONLY_COMMUNITY.include?(action_tracker.verb)
+ m1, m2 = fast_create(Person), fast_create(Person), fast_create(Person), fast_create(Person)
+ 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 4, ActionTrackerNotification.count
+ [person, community, m1, m2].each do |profile|
+ notification = ActionTrackerNotification.find_by_profile_id profile.id
+ assert_equal action_tracker, notification.action_tracker
+ end
+ end
+
+ should 'notify users its friends, the community and its members' 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 => 'create_article')
+ 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 6, ActionTrackerNotification.count
+ [person, community, 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 'not notify the community tracking join_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 => 'join_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 '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']
+ 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']
+ assert_equal [], not_notify_community_verbs - NotifyActivityToProfilesJob::NOT_NOTIFY_COMMUNITY
+ assert_equal [], NotifyActivityToProfilesJob::NOT_NOTIFY_COMMUNITY - not_notify_community_verbs
+ end
+
end
diff --git a/test/unit/person_test.rb b/test/unit/person_test.rb
index 1bafa16..a3e31ec 100644
--- a/test/unit/person_test.rb
+++ b/test/unit/person_test.rb
@@ -829,7 +829,7 @@ class PersonTest < Test::Unit::TestCase
end
end
- should "the tracked action notify friends with one delayed job process followed by others jobs created after run the firt job" do
+ should "the tracked action notify friends with one delayed job process" do
p1 = Person.first
p2 = fast_create(Person)
p3 = fast_create(Person)
@@ -847,8 +847,7 @@ class PersonTest < Test::Unit::TestCase
assert_difference(Delayed::Job, :count, 1) do
Person.notify_activity(action_tracker)
end
-
- assert_difference(ActionTrackerNotification, :count, 2) do
+ assert_difference(ActionTrackerNotification, :count, 3) do
process_delayed_job_queue
end
end
@@ -866,13 +865,9 @@ class PersonTest < Test::Unit::TestCase
assert !p2.is_member_of?(community)
process_delayed_job_queue
- action_tracker = fast_create(ActionTracker::Record)
- article = mock()
- action_tracker.stubs(:dispatcher).returns(article)
- article.stubs(:is_a?).with(Article).returns(true)
- article.stubs(:is_a?).with(RoleAssignment).returns(false)
- article.stubs(:is_a?).with(Comment).returns(false)
- article.stubs(:profile).returns(community)
+ action_tracker = fast_create(ActionTracker::Record, :verb => 'create_article')
+ action_tracker.target = community
+ action_tracker.save!
ActionTrackerNotification.delete_all
assert_difference(ActionTrackerNotification, :count, 3) do
Person.notify_activity(action_tracker)
@@ -900,7 +895,7 @@ class PersonTest < Test::Unit::TestCase
action_tracker = fast_create(ActionTracker::Record)
article = mock()
- action_tracker.stubs(:dispatcher).returns(article)
+ action_tracker.stubs(:target).returns(article)
article.stubs(:is_a?).with(Article).returns(true)
article.stubs(:is_a?).with(RoleAssignment).returns(false)
article.stubs(:is_a?).with(Comment).returns(false)
@@ -960,9 +955,47 @@ class PersonTest < Test::Unit::TestCase
p = create_user('test_user').person
c = fast_create(Community, :name => "Foo")
c.add_member(p)
- assert_equal ["Foo"], ActionTracker::Record.last.get_resource_name
+ assert_equal ["Foo"], ActionTracker::Record.last(:conditions => {:verb => 'join_community'}).get_resource_name
c.reload.add_moderator(p.reload)
- assert_equal ["Foo"], ActionTracker::Record.last.get_resource_name
+ assert_equal ["Foo"], ActionTracker::Record.last(:conditions => {:verb => 'join_community'}).get_resource_name
+ end
+
+ should 'the tracker target be Community when a person joins a community' do
+ ActionTracker::Record.delete_all
+ p = create_user('test_user').person
+ c = fast_create(Community, :name => "Foo")
+ c.add_member(p)
+ assert_kind_of Community, ActionTracker::Record.last(:conditions => {:verb => 'join_community'}).target
+ end
+
+ should 'the community be notified specifically when a person joins a community' do
+ ActionTracker::Record.delete_all
+ p = create_user('test_user').person
+ c = fast_create(Community, :name => "Foo")
+ c.add_member(p)
+ assert_not_nil ActionTracker::Record.last(:conditions => {:verb => 'add_member_in_community'})
+ end
+
+ should 'the community specific notification created when a member joins community could not be propagated to members' do
+ ActionTracker::Record.delete_all
+ p1 = create_user('test_user').person
+ p2 = create_user('test_user').person
+ p3 = create_user('test_user').person
+ c = fast_create(Community, :name => "Foo")
+ c.add_member(p1)
+ process_delayed_job_queue
+ c.add_member(p3)
+ process_delayed_job_queue
+ assert_equal 4, ActionTracker::Record.count
+ assert_equal 5, ActionTrackerNotification.count
+ has_add_member_notification = false
+ ActionTrackerNotification.all.map do |notification|
+ if notification.action_tracker.verb == 'add_member_in_community'
+ has_add_member_notification = true
+ assert_equal c, notification.profile
+ end
+ end
+ assert has_add_member_notification
end
should 'track only one action when a person leaves a community' do
@@ -972,7 +1005,58 @@ class PersonTest < Test::Unit::TestCase
c.add_moderator(p)
ActionTracker::Record.delete_all
c.remove_member(p)
- assert_equal ["Foo"], ActionTracker::Record.last.get_resource_name
+ assert_equal ["Foo"], ActionTracker::Record.last(:conditions => {:verb => 'leave_community'}).get_resource_name
+ end
+
+ should 'the tracker target be Community when a person leaves a community' do
+ ActionTracker::Record.delete_all
+ p = create_user('test_user').person
+ c = fast_create(Community, :name => "Foo")
+ c.add_member(p)
+ c.add_moderator(p)
+ ActionTracker::Record.delete_all
+ c.remove_member(p)
+ assert_kind_of Community, ActionTracker::Record.last(:conditions => {:verb => 'leave_community'}).target
+ end
+
+ should 'the community be notified specifically when a person leaves a community' do
+ ActionTracker::Record.delete_all
+ p = create_user('test_user').person
+ c = fast_create(Community, :name => "Foo")
+ c.add_member(p)
+ c.add_moderator(p)
+ ActionTracker::Record.delete_all
+ c.remove_member(p)
+ assert_not_nil ActionTracker::Record.last(:conditions => {:verb => 'remove_member_in_community'})
+ end
+
+ should 'the community specific notification created when a member leaves community could not be propagated to members' do
+ ActionTracker::Record.delete_all
+ p1 = Person.first
+ p2 = create_user('test_user').person
+ p3 = create_user('test_user').person
+ c = fast_create(Community, :name => "Foo")
+ process_delayed_job_queue
+ Delayed::Job.delete_all
+ c.add_member(p1)
+ c.add_member(p3)
+ c.add_moderator(p1)
+ c.add_moderator(p3)
+ ActionTracker::Record.delete_all
+ c.remove_member(p1)
+ process_delayed_job_queue
+ c.remove_member(p3)
+ process_delayed_job_queue
+ assert_equal 4, ActionTracker::Record.count
+ assert_equal 5, ActionTrackerNotification.count
+ has_remove_member_notification = false
+ ActionTrackerNotification.all.map do |notification|
+ if notification.action_tracker.verb == 'remove_member_in_community'
+ has_remove_member_notification = true
+ assert_equal c, notification.profile
+ end
+ end
+ assert has_remove_member_notification
end
end
diff --git a/test/unit/published_article_test.rb b/test/unit/published_article_test.rb
index c5972fa..8ab9d57 100644
--- a/test/unit/published_article_test.rb
+++ b/test/unit/published_article_test.rb
@@ -137,4 +137,136 @@ class PublishedArticleTest < ActiveSupport::TestCase
assert_equal p.parent, new_parent
end
+ should 'notifiable be true' do
+ a = fast_create(PublishedArticle)
+ assert a.notifiable?
+ end
+
+ should 'notify activity on create' do
+ ActionTracker::Record.delete_all
+ a = fast_create(Article)
+ PublishedArticle.create! :reference_article => a, :name => 'test', :profile_id => fast_create(Profile).id, :published => true
+ assert_equal 1, ActionTracker::Record.count
+ end
+
+ should 'notify with different trackers activity create with different targets' do
+ ActionTracker::Record.delete_all
+ profile = fast_create(Profile)
+ a = fast_create(Article)
+ PublishedArticle.create! :reference_article => a, :name => 'bar', :profile_id => profile.id, :published => true
+ a = fast_create(Article)
+ PublishedArticle.create! :reference_article => a, :name => 'another bar', :profile_id => profile.id, :published => true
+ assert_equal 1, ActionTracker::Record.count
+ a = fast_create(Article)
+ PublishedArticle.create! :reference_article => a, :name => 'another bar', :profile_id => fast_create(Profile).id, :published => true
+ assert_equal 2, ActionTracker::Record.count
+ end
+
+ should 'notify activity on update' do
+ ActionTracker::Record.delete_all
+ a = fast_create(Article)
+ a = PublishedArticle.create! :reference_article => a, :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
+ end
+
+ should 'notify with different trackers activity update with different targets' do
+ ActionTracker::Record.delete_all
+ a = fast_create(Article)
+ a1 = PublishedArticle.create! :reference_article => a, :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
+ a = fast_create(Article)
+ a2 = PublishedArticle.create! :reference_article => a, :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
+ end
+
+ should 'notify activity on destroy' do
+ ActionTracker::Record.delete_all
+ a = fast_create(Article)
+ a = PublishedArticle.create! :reference_article => a, :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
+ assert_equal 1, ActionTracker::Record.count
+ a.destroy
+ assert_equal 2, ActionTracker::Record.count
+ end
+
+ should 'notify different activities when destroy articles with diferrents targets' do
+ ActionTracker::Record.delete_all
+ a = fast_create(Article)
+ a1 = PublishedArticle.create! :reference_article => a, :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
+ a = fast_create(Article)
+ a2 = PublishedArticle.create! :reference_article => a, :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
+ end
+
+ should "the tracker action target be defined as Community by custom_target method 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)
+ a = fast_create(Article)
+ article = PublishedArticle.create! :reference_article => a, :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
+ end
+
+ should "the tracker action target be defined as person by custom_target method on articles'creation in profile" do
+ ActionTracker::Record.delete_all
+ person = Person.first
+ a = fast_create(Article)
+ article = PublishedArticle.create! :reference_article => a, :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
+ end
+
+ should 'not notify activity if the article is not advertise' do
+ ActionTracker::Record.delete_all
+ article = fast_create(Article)
+ a = PublishedArticle.create! :reference_article => article, :name => 'bar', :profile_id => fast_create(Profile).id, :published => true, :advertise => false
+ assert_equal true, a.published?
+ assert_equal true, a.notifiable?
+ assert_equal false, a.image?
+ assert_equal false, a.profile.is_a?(Community)
+ assert_equal 0, ActionTracker::Record.count
+ end
+
+ should "have defined the is_trackable method defined" do
+ assert PublishedArticle.method_defined?(:is_trackable?)
+ end
+
+ should "the common trackable conditions return the correct value" do
+ a = PublishedArticle.new
+ a.published = a.advertise = true
+ assert_equal true, a.published?
+ assert_equal true, a.notifiable?
+ assert_equal true, a.advertise?
+ assert_equal true, a.is_trackable?
+
+ a.published=false
+ assert_equal false, a.published?
+ assert_equal false, a.is_trackable?
+
+ a.published=true
+ a.advertise=false
+ assert_equal false, a.advertise?
+ assert_equal false, a.is_trackable?
+ end
+
end
diff --git a/test/unit/textile_article_test.rb b/test/unit/textile_article_test.rb
index 61ecf03..1c9629f 100644
--- a/test/unit/textile_article_test.rb
+++ b/test/unit/textile_article_test.rb
@@ -38,6 +38,16 @@ class TextileArticleTest < Test::Unit::TestCase
assert_equal 1, ActionTracker::Record.count
end
+ should 'notify with different trackers activity create with different targets' 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
+ end
+
should 'notify activity on update' do
ActionTracker::Record.delete_all
a = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
@@ -47,6 +57,19 @@ class TextileArticleTest < Test::Unit::TestCase
assert_equal 2, ActionTracker::Record.count
end
+ should 'notify with different trackers activity update with different targets' 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
+ end
+
should 'notify activity on destroy' do
ActionTracker::Record.delete_all
a = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
@@ -55,4 +78,72 @@ class TextileArticleTest < Test::Unit::TestCase
assert_equal 2, ActionTracker::Record.count
end
+ should 'notify different activities when destroy articles with diferrents targets' 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
+ end
+
+ should "the tracker action target be defined as Community by custom_target method 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
+ end
+
+ should "the tracker action target be defined as person by custom_target method 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
+ end
+
+ should 'not notify activity if the article is not advertise' do
+ ActionTracker::Record.delete_all
+ a = TextileArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true, :advertise => false
+ assert_equal true, a.published?
+ assert_equal true, a.notifiable?
+ assert_equal false, a.image?
+ assert_equal false, a.profile.is_a?(Community)
+ assert_equal 0, ActionTracker::Record.count
+ end
+
+ should "have defined the is_trackable method defined" do
+ assert TextileArticle.method_defined?(:is_trackable?)
+ end
+
+ should "the common trackable conditions return the correct value" do
+ a = TextileArticle.new
+ a.published = a.advertise = true
+ assert_equal true, a.published?
+ assert_equal true, a.notifiable?
+ assert_equal true, a.advertise?
+ assert_equal true, a.is_trackable?
+
+ a.published=false
+ assert_equal false, a.published?
+ assert_equal false, a.is_trackable?
+
+ a.published=true
+ a.advertise=false
+ assert_equal false, a.advertise?
+ assert_equal false, a.is_trackable?
+ end
+
end
diff --git a/test/unit/tiny_mce_article_test.rb b/test/unit/tiny_mce_article_test.rb
index 859450b..c4f3a84 100644
--- a/test/unit/tiny_mce_article_test.rb
+++ b/test/unit/tiny_mce_article_test.rb
@@ -129,6 +129,16 @@ class TinyMceArticleTest < Test::Unit::TestCase
assert_equal 1, ActionTracker::Record.count
end
+ should 'notify with different trackers activity create with different targets' do
+ ActionTracker::Record.delete_all
+ profile = fast_create(Profile)
+ TinyMceArticle.create! :name => 'bar', :profile_id => profile.id, :published => true
+ TinyMceArticle.create! :name => 'another bar', :profile_id => profile.id, :published => true
+ assert_equal 1, ActionTracker::Record.count
+ TinyMceArticle.create! :name => 'another bar', :profile_id => fast_create(Profile).id, :published => true
+ assert_equal 2, ActionTracker::Record.count
+ end
+
should 'notify activity on update' do
ActionTracker::Record.delete_all
a = TinyMceArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
@@ -138,6 +148,19 @@ class TinyMceArticleTest < Test::Unit::TestCase
assert_equal 2, ActionTracker::Record.count
end
+ should 'notify with different trackers activity update with different targets' do
+ ActionTracker::Record.delete_all
+ a1 = TinyMceArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
+ a2 = TinyMceArticle.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
+ end
+
should 'notify activity on destroy' do
ActionTracker::Record.delete_all
a = TinyMceArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
@@ -146,4 +169,73 @@ class TinyMceArticleTest < Test::Unit::TestCase
assert_equal 2, ActionTracker::Record.count
end
+ should 'notify different activities when destroy articles with diferrents targets' do
+ ActionTracker::Record.delete_all
+ a1 = TinyMceArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true
+ a2 = TinyMceArticle.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
+ end
+
+ should "the tracker action target be defined as Community by custom_target method 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 = TinyMceArticle.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
+ end
+
+ should "the tracker action target be defined as person by custom_target method on articles'creation in profile" do
+ ActionTracker::Record.delete_all
+ person = Person.first
+ article = TinyMceArticle.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
+ end
+
+ should 'not notify activity if the article is not advertise' do
+ ActionTracker::Record.delete_all
+ a = TinyMceArticle.create! :name => 'bar', :profile_id => fast_create(Profile).id, :published => true, :advertise => false
+ assert_equal true, a.published?
+ assert_equal true, a.notifiable?
+ assert_equal false, a.image?
+ assert_equal false, a.profile.is_a?(Community)
+ assert_equal 0, ActionTracker::Record.count
+ end
+
+ should "have defined the is_trackable method defined" do
+ assert TinyMceArticle.method_defined?(:is_trackable?)
+ end
+
+ should "the common trackable conditions return the correct value" do
+ a = TinyMceArticle.new
+ a.published = a.advertise = true
+ assert_equal true, a.published?
+ assert_equal true, a.notifiable?
+ assert_equal true, a.advertise?
+ assert_equal true, a.is_trackable?
+
+ a.published=false
+ assert_equal false, a.published?
+ assert_equal false, a.is_trackable?
+
+ a.published=true
+ a.advertise=false
+ assert_equal false, a.advertise?
+ assert_equal false, a.is_trackable?
+ end
+
+
end
diff --git a/vendor/plugins/access_control/lib/role_assignment.rb b/vendor/plugins/access_control/lib/role_assignment.rb
index 3a2f5cf..34ac5f1 100644
--- a/vendor/plugins/access_control/lib/role_assignment.rb
+++ b/vendor/plugins/access_control/lib/role_assignment.rb
@@ -5,10 +5,14 @@ class RoleAssignment < ActiveRecord::Base
validates_presence_of :role, :accessor
- track_actions :join_community, :after_create, :keep_params => ["resource.name", "resource.url", "resource.profile_custom_icon"], :if => Proc.new { |x| x.resource.is_a?(Community) && x.accessor.role_assignments.count(:conditions => { :resource_id => x.resource.id, :resource_type => 'Profile' }) == 1 }
+ track_actions :join_community, :after_create, :keep_params => ["resource.name", "resource.url", "resource.profile_custom_icon"], :if => Proc.new { |x| x.resource.is_a?(Community) && x.accessor.role_assignments.count(:conditions => { :resource_id => x.resource.id, :resource_type => 'Profile' }) == 1 }, :custom_user => :accessor, :custom_target => :resource
- track_actions :leave_community, :before_destroy, :keep_params => ["resource.name", "resource.url", "resource.profile_custom_icon"], :if => Proc.new { |x| x.resource.is_a?(Community) && x.accessor.role_assignments.count(:conditions => { :resource_id => x.resource.id, :resource_type => 'Profile' }) == 1 }
+ track_actions :add_member_in_community, :after_create, :if => Proc.new { |x| x.resource.is_a?(Community) && x.accessor.role_assignments.count(:conditions => { :resource_id => x.resource.id, :resource_type => 'Profile' }) == 1 }, :custom_user => :accessor, :custom_target => :resource
+ track_actions :leave_community, :before_destroy, :keep_params => ["resource.name", "resource.url", "resource.profile_custom_icon"], :if => Proc.new { |x| x.resource.is_a?(Community) && x.accessor.role_assignments.count(:conditions => { :resource_id => x.resource.id, :resource_type => 'Profile' }) == 1 }, :custom_user => :accessor, :custom_target => :resource
+
+ track_actions :remove_member_in_community, :before_destroy, :if => Proc.new { |x| x.resource.is_a?(Community) && x.accessor.role_assignments.count(:conditions => { :resource_id => x.resource.id, :resource_type => 'Profile' }) == 1 }, :custom_target => :resource, :custom_user => :accessor
+
def has_permission?(perm, res)
return false unless role.has_permission?(perm.to_s) && (resource || is_global)
return true if is_global
diff --git a/vendor/plugins/action_tracker/generators/action_tracker/templates/migration.rb b/vendor/plugins/action_tracker/generators/action_tracker/templates/migration.rb
index e383f6d..e211ab3 100644
--- a/vendor/plugins/action_tracker/generators/action_tracker/templates/migration.rb
+++ b/vendor/plugins/action_tracker/generators/action_tracker/templates/migration.rb
@@ -2,7 +2,7 @@ class CreateActionTracker < ActiveRecord::Migration
def self.up
create_table :action_tracker do |t|
t.belongs_to :user, :polymorphic => true
- t.belongs_to :dispatcher, :polymorphic => true
+ t.belongs_to :target, :polymorphic => true
t.text :params
t.string :verb
t.timestamps
@@ -10,7 +10,7 @@ class CreateActionTracker < ActiveRecord::Migration
change_table :action_tracker do |t|
t.index [:user_id, :user_type]
- t.index [:dispatcher_id, :dispatcher_type]
+ t.index [:target_id, :target_type]
t.index :verb
end
end
diff --git a/vendor/plugins/action_tracker/lib/action_tracker.rb b/vendor/plugins/action_tracker/lib/action_tracker.rb
index cc6da3d..083f771 100644
--- a/vendor/plugins/action_tracker/lib/action_tracker.rb
+++ b/vendor/plugins/action_tracker/lib/action_tracker.rb
@@ -67,7 +67,9 @@ module ActionTracker
def track_actions(verb, callback, options = {}, &block)
keep_params = options.delete(:keep_params) || options.delete('keep_params') || :all
post_proc = options.delete(:post_processing) || options.delete('post_processing') || Proc.new{}
- send(callback, Proc.new { |tracked| tracked.save_action_for_verb(verb.to_s, keep_params, post_proc) }, options)
+ custom_user = options.delete(:custom_user) || options.delete('custom_user') || nil
+ custom_target = options.delete(:custom_target) || options.delete('custom_target') || nil
+ send(callback, Proc.new { |tracked| tracked.save_action_for_verb(verb.to_s, keep_params, post_proc, custom_user, custom_target) }, options)
send :include, InstanceMethods
end
@@ -86,8 +88,10 @@ module ActionTracker
time.to_f
end
- def save_action_for_verb(verb, keep_params = :all, post_proc = Proc.new{})
- user = ActionTracker::Record.current_user_from_model
+ def save_action_for_verb(verb, keep_params = :all, post_proc = Proc.new{}, custom_user = nil, custom_target = nil)
+ user = self.send(custom_user) unless custom_user.blank?
+ user ||= ActionTracker::Record.current_user_from_model
+ target = self.send(custom_target) unless custom_target.blank?
return nil if user.nil?
if keep_params.is_a? Array
stored_params = {}
@@ -103,13 +107,13 @@ module ActionTracker
end
tracked_action = case ActionTrackerConfig.verb_type(verb)
when :groupable
- Record.add_or_create :verb => verb, :params => stored_params
+ Record.add_or_create :verb => verb, :params => stored_params, :user => user, :target => target
when :updatable
- Record.update_or_create :verb => verb, :params => stored_params
+ Record.update_or_create :verb => verb, :params => stored_params, :user => user, :target => target
when :single
- Record.new :verb => verb, :params => stored_params
+ Record.new :verb => verb, :params => stored_params, :user => user
end
- tracked_action.dispatcher = self
+ tracked_action.target = target || self
user.tracked_actions << tracked_action
post_proc.call tracked_action.reload
end
diff --git a/vendor/plugins/action_tracker/lib/action_tracker_model.rb b/vendor/plugins/action_tracker/lib/action_tracker_model.rb
index 90c93dc..8565b97 100644
--- a/vendor/plugins/action_tracker/lib/action_tracker_model.rb
+++ b/vendor/plugins/action_tracker/lib/action_tracker_model.rb
@@ -4,7 +4,7 @@ module ActionTracker
set_table_name 'action_tracker'
belongs_to :user, :polymorphic => true
- belongs_to :dispatcher, :polymorphic => true
+ belongs_to :target, :polymorphic => true
serialize :params, Hash
@@ -25,7 +25,9 @@ module ActionTracker
def self.update_or_create(params)
u = params[:user] || current_user_from_model
return if u.nil?
- l = last :conditions => { :user_id => u.id, :user_type => u.class.base_class.to_s, :verb => params[:verb].to_s }
+ target_hash = params[:target].nil? ? {} : {:target_type => params[:target].class.base_class.to_s, :target_id => params[:target].id}
+ conditions = { :user_id => u.id, :user_type => u.class.base_class.to_s, :verb => params[:verb].to_s }.merge(target_hash)
+ l = last :conditions => conditions
( !l.nil? and Time.now - l.updated_at < ActionTrackerConfig.timeout ) ? l.update_attributes(params.merge({ :updated_at => Time.now })) : l = new(params)
l
end
@@ -33,7 +35,8 @@ module ActionTracker
def self.add_or_create(params)
u = params[:user] || current_user_from_model
return if u.nil?
- l = last :conditions => { :user_id => u.id, :user_type => u.class.base_class.to_s, :verb => params[:verb].to_s }
+ target_hash = params[:target].nil? ? {} : {:target_type => params[:target].class.base_class.to_s, :target_id => params[:target].id}
+ l = last :conditions => { :user_id => u.id, :user_type => u.class.base_class.to_s, :verb => params[:verb].to_s }.merge(target_hash)
if !l.nil? and Time.now - l.updated_at < ActionTrackerConfig.timeout
params[:params].clone.each { |key, value| params[:params][key] = l.params[key].clone.push(value) }
l.update_attributes params
diff --git a/vendor/plugins/action_tracker/test/action_tracker_model_test.rb b/vendor/plugins/action_tracker/test/action_tracker_model_test.rb
index 7a79941..2edcfa7 100644
--- a/vendor/plugins/action_tracker/test/action_tracker_model_test.rb
+++ b/vendor/plugins/action_tracker/test/action_tracker_model_test.rb
@@ -11,7 +11,7 @@ ActiveRecord::Schema.define do
end
create_table :action_tracker do |t|
t.belongs_to :user, :polymorphic => true
- t.belongs_to :dispatcher, :polymorphic => true
+ t.belongs_to :target, :polymorphic => true
t.text :params
t.string :verb
t.timestamps
@@ -29,7 +29,7 @@ class ActionTrackerModelTest < ActiveSupport::TestCase
ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something" } }
@mymodel = SomeModel.create!
@othermodel = SomeModel.create!
- @tracked_action = ActionTracker::Record.create! :verb => :some_verb, :params => { :user => "foo" }, :user => @mymodel, :dispatcher => @othermodel
+ @tracked_action = ActionTracker::Record.create! :verb => :some_verb, :params => { :user => "foo" }, :user => @mymodel, :target => @othermodel
end
def test_has_relationship
@@ -47,10 +47,10 @@ class ActionTrackerModelTest < ActiveSupport::TestCase
assert_equal [@tracked_action], @mymodel.tracked_actions
end
- def test_has_a_polymorphic_relation_with_dispatcher
- assert_equal @othermodel.id, @tracked_action.dispatcher_id
- assert_equal "SomeModel", @tracked_action.dispatcher_type
- assert_equal @othermodel, @tracked_action.dispatcher
+ def test_has_a_polymorphic_relation_with_target
+ assert_equal @othermodel.id, @tracked_action.target_id
+ assert_equal "SomeModel", @tracked_action.target_type
+ assert_equal @othermodel, @tracked_action.target
end
def test_should_stringify_verb_before_validation
@@ -90,7 +90,7 @@ class ActionTrackerModelTest < ActiveSupport::TestCase
assert_equal @mymodel, ActionTracker::Record.last.user
end
- def test_update_or_create_create_if_no_timeout
+ def test_update_or_create_create_if_timeout
ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
ActionTrackerConfig.timeout = 5.minutes
ActionTracker::Record.delete_all
@@ -111,7 +111,7 @@ class ActionTrackerModelTest < ActiveSupport::TestCase
assert_equal t, ta.reload.updated_at
end
- def test_update_or_create_update_if_timeout
+ def test_update_or_create_update_if_no_timeout
ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
ActionTrackerConfig.timeout = 7.minutes
ActionTracker::Record.delete_all
@@ -134,7 +134,37 @@ class ActionTrackerModelTest < ActiveSupport::TestCase
assert_equal 3, ta.reload.get_foo
end
- def test_add_or_create_create_if_no_timeout
+ def test_should_update_or_create_method_create_a_new_tracker_with_different_dispacthers
+ ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
+ ActionTracker::Record.delete_all
+ ta = nil
+ assert_difference "ActionTracker::Record.count" do
+ ta = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel, :target => @mymodel, :params => { :foo => 2 }
+ ta.save; ta.reload
+ end
+ assert_kind_of ActionTracker::Record, ta
+ t = ta.reload.updated_at
+ sleep(1)
+ assert_no_difference "ActionTracker::Record.count" do
+ ta2 = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel, :target => @mymodel, :params => { :foo => 3 }
+ ta2.save; ta2.reload
+ assert_kind_of ActionTracker::Record, ta2
+ end
+ assert_equal 3, ta.reload.get_foo
+ assert_not_equal t, ta.reload.updated_at
+
+ assert_kind_of ActionTracker::Record, ta
+ t = ta.reload.updated_at
+ assert_difference "ActionTracker::Record.count" do
+ ta2 = ActionTracker::Record.update_or_create :verb => :some, :user => @mymodel, :target => @othermodel, :params => { :foo => 4 }
+ ta2.save; ta2.reload
+ assert_kind_of ActionTracker::Record, ta2
+ end
+ assert_equal t, ta.reload.updated_at
+ assert_equal 3, ta.reload.get_foo
+ end
+
+ def test_add_or_create_create_if_timeout
ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :groupable } }
ActionTrackerConfig.timeout = 5.minutes
ActionTracker::Record.delete_all
@@ -157,7 +187,7 @@ class ActionTrackerModelTest < ActiveSupport::TestCase
assert_equal ["test"], ActionTracker::Record.last.params[:foo]
end
- def test_add_or_create_update_if_timeout
+ def test_add_or_create_update_if_no_timeout
ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
ActionTrackerConfig.timeout = 7.minutes
ActionTracker::Record.delete_all
@@ -188,6 +218,30 @@ class ActionTrackerModelTest < ActiveSupport::TestCase
assert_equal [2, 1, 1], ActionTracker::Record.last.params[:bar]
end
+ def test_add_or_create_create_if_no_timeout_and_different_target
+ ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
+ ActionTrackerConfig.timeout = 7.minutes
+ ActionTracker::Record.delete_all
+ ta = nil
+ assert_difference "ActionTracker::Record.count" do
+ ta = ActionTracker::Record.add_or_create :verb => :some, :user => @mymodel, :target => @mymodel, :params => { :foo => "test 1", :bar => 2 }
+ ta.save; ta.reload
+ end
+ assert_kind_of ActionTracker::Record, ta
+ assert_equal ["test 1"], ta.params[:foo]
+ assert_equal [2], ta.params[:bar]
+ ta.updated_at = Time.now.ago(6.minutes)
+ ta.send :update_without_callbacks
+ t = ta.reload.updated_at
+ assert_difference "ActionTracker::Record.count" do
+ ta2 = ActionTracker::Record.add_or_create :verb => :some, :user => @mymodel, :target => @othermodel, :params => { :foo => "test 2", :bar => 1 }
+ ta2.save; ta2.reload
+ assert_kind_of ActionTracker::Record, ta2
+ end
+ assert_equal ["test 1"], ta.params[:foo]
+ assert_equal [2], ta.params[:bar]
+ end
+
def test_time_spent
ActionTracker::Record.delete_all
ActionTrackerConfig.verbs = { :some => { :description => "Something", :type => :updatable } }
diff --git a/vendor/plugins/action_tracker/test/action_tracker_test.rb b/vendor/plugins/action_tracker/test/action_tracker_test.rb
index aae44ba..4f666a4 100644
--- a/vendor/plugins/action_tracker/test/action_tracker_test.rb
+++ b/vendor/plugins/action_tracker/test/action_tracker_test.rb
@@ -15,7 +15,7 @@ ActiveRecord::Schema.define do
end
create_table :action_tracker do |t|
t.belongs_to :user, :polymorphic => true
- t.belongs_to :dispatcher, :polymorphic => true
+ t.belongs_to :target, :polymorphic => true
t.text :params
t.string :verb
t.timestamps
@@ -29,6 +29,7 @@ end
class OtherModel < ActiveRecord::Base
set_table_name :other_table
+ acts_as_trackable
end
class ThingsController < ActionController::Base
@@ -38,7 +39,7 @@ class ThingsController < ActionController::Base
render :text => "test"
end
- def test
+ def test
render :text => "test"
end
@@ -490,7 +491,81 @@ class ActionTrackerTest < ActiveSupport::TestCase
get :test
end
end
- assert_equal "test", OtherModel.last.other_column
+ assert_equal "test", OtherModel.last.other_column
+ end
+
+ def test_track_actions_custom_user_as_symbol
+ ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
+ model = create_model do
+ track_actions :test, :after_create, :custom_user => :test_custom_user
+ def current_user
+ SomeModel.create!
+ end
+ def test_custom_user
+ OtherModel.create!
+ end
+ end
+ @controller = create_controller_for_model(model, :another_column => 2)
+ assert_difference('ActionTracker::Record.count') { get :test }
+ assert_kind_of OtherModel, ActionTracker::Record.last.user
+ end
+
+ def test_track_actions_custom_user_as_string
+ ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
+ model = create_model do
+ track_actions :test, :after_create, "custom_user" => :test_custom_user
+ def current_user
+ SomeModel.create!
+ end
+ def test_custom_user
+ OtherModel.create!
+ end
+ end
+ @controller = create_controller_for_model(model, :another_column => 2)
+ assert_difference('ActionTracker::Record.count') { get :test }
+ assert_kind_of OtherModel, ActionTracker::Record.last.user
+ end
+
+ def test_track_actions_custom_user_is_nil_by_default
+ ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
+ model = create_model do
+ track_actions :test, :after_create
+ def current_user
+ SomeModel.create!
+ end
+ def test_custom_user
+ OtherModel.create!
+ end
+ end
+ @controller = create_controller_for_model(model, :another_column => 2)
+ assert_difference('ActionTracker::Record.count') { get :test }
+ assert_kind_of SomeModel, ActionTracker::Record.last.user
+ end
+
+ def test_track_actions_custom_target_as_symbol
+ ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
+ model = create_model do
+ track_actions :test, :after_create, :custom_target => :test_custom_target
+ def test_custom_target
+ SomeModel.create!
+ end
+ end
+ @controller = create_controller_for_model(model, :another_column => 2)
+ assert_difference('ActionTracker::Record.count') { get :test }
+ assert_kind_of SomeModel, ActionTracker::Record.last.target
+ end
+
+ def test_track_actions_custom_target_as_string
+ ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
+ model = create_model do
+ track_actions :test, :after_create, "custom_target" => :test_custom_target
+ def test_custom_target
+ SomeModel.create!
+ end
+ end
+ @controller = create_controller_for_model(model, :another_column => 2)
+ assert_difference('ActionTracker::Record.count') { get :test }
+ assert_kind_of SomeModel, ActionTracker::Record.last.target
end
def test_acts_as_trackable_with_options
@@ -515,7 +590,7 @@ class ActionTrackerTest < ActiveSupport::TestCase
end
end
- def test_track_actions_save_dispatcher
+ def test_track_actions_save_target
ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
model = create_model do
track_actions :test, :after_create
@@ -524,7 +599,7 @@ class ActionTrackerTest < ActiveSupport::TestCase
assert_difference 'ActionTracker::Record.count' do
get :test
end
- assert_kind_of model.base_class, ActionTracker::Record.last.dispatcher
+ assert_kind_of model.base_class, ActionTracker::Record.last.target
end
private
diff --git a/vendor/plugins/user_stamp/lib/user_stamp.rb b/vendor/plugins/user_stamp/lib/user_stamp.rb
index 4558605..647e045 100644
--- a/vendor/plugins/user_stamp/lib/user_stamp.rb
+++ b/vendor/plugins/user_stamp/lib/user_stamp.rb
@@ -30,12 +30,14 @@ class UserStampSweeper < ActionController::Caching::Sweeper
def before_validation(record)
return unless current_user
- if record.respond_to?(UserStamp.creator_assignment_method) && record.new_record?
- record.send(UserStamp.creator_assignment_method, current_user)
+ attribute, method = UserStamp.creator_attribute, UserStamp.creator_assignment_method
+ if record.respond_to?(method) && record.new_record?
+ record.send(method, current_user) unless record.send("#{attribute}_id_changed?") || record.send("#{attribute}_type_changed?")
end
- if record.respond_to?(UserStamp.updater_assignment_method)
- record.send(UserStamp.updater_assignment_method, current_user)
+ attribute, method = UserStamp.updater_attribute, UserStamp.updater_assignment_method
+ if record.respond_to?(method)
+ record.send(method, current_user) if record.send(attribute).blank?
end
end
diff --git a/vendor/plugins/user_stamp/spec/user_stamp_sweeper_spec.rb b/vendor/plugins/user_stamp/spec/user_stamp_sweeper_spec.rb
index 45cbf24..b2e7ca0 100644
--- a/vendor/plugins/user_stamp/spec/user_stamp_sweeper_spec.rb
+++ b/vendor/plugins/user_stamp/spec/user_stamp_sweeper_spec.rb
@@ -2,58 +2,59 @@ require File.dirname(__FILE__) + '/spec_helper'
class PostsController
def self.current_user
- User.new(220)
+ @@user
end
end
describe UserStampSweeper, "#before_validation" do
before do
- UserStamp.creator_attribute = :creator_id
- UserStamp.updater_attribute = :updater_id
+ @@user = User.new(220)
+ UserStamp.creator_attribute = :creator
+ UserStamp.updater_attribute = :updater
UserStamp.current_user_method = :current_user
@sweeper = UserStampSweeper.instance
@sweeper.stub!(:controller).and_return(PostsController)
end
describe "(with new record)" do
- it "should set creator_id if attribute exists" do
- record = mock('Record', :creator_id= => nil, :updater_id= => nil, :new_record? => true)
- record.should_receive(:creator_id=).with(220).once
+ it "should set creator if attribute exists" do
+ record = mock('Record', :creator= => nil, :updater= => nil, :new_record? => true, :updater => nil, :creator_id_changed? => false, :creator_type_changed? => false, :updater_id_changed? => false, :updater_type_changed? => false)
+ record.should_receive(:creator=).with(@@user).once
@sweeper.before_validation(record)
end
- it "should NOT set creator_id if attribute does not exist" do
- record = mock('Record', :new_record? => true, :updater_id= => nil, :respond_to? => false)
- record.should_receive(:respond_to?).with("creator_id=").and_return(false)
- record.should_not_receive(:creator_id=)
+ it "should NOT set creator if attribute does not exist" do
+ record = mock('Record', :new_record? => true, :updater= => nil, :respond_to? => false)
+ record.should_receive(:respond_to?).with("creator=").and_return(false)
+ record.should_not_receive(:creator=)
@sweeper.before_validation(record)
end
end
describe "(with non new record)" do
- it "should NOT set creator_id if attribute exists" do
- record = mock('Record', :creator_id= => nil, :updater_id= => nil, :new_record? => false)
- record.should_not_receive(:creator_id=)
+ it "should NOT set creator if attribute exists" do
+ record = mock('Record', :creator= => nil, :updater= => nil, :updater => nil, :new_record? => false, :creator_id_changed? => false, :creator_type_changed? => false, :updater_id_changed? => false, :updater_type_changed? => false)
+ record.should_not_receive(:creator=)
@sweeper.before_validation(record)
end
- it "should NOT set creator_id if attribute does not exist" do
- record = mock('Record', :updater_id= => nil, :new_record? => false)
- record.should_not_receive(:creator_id=)
+ it "should NOT set creator if attribute does not exist" do
+ record = mock('Record', :updater= => nil, :updater => nil, :new_record? => false, :creator_id_changed? => false, :creator_type_changed? => false, :updater_id_changed? => false, :updater_type_changed? => false)
+ record.should_not_receive(:creator=)
@sweeper.before_validation(record)
end
end
- it "should set updater_id if attribute exists" do
- record = mock('Record', :creator_id= => nil, :updater_id= => nil, :new_record? => :false)
- record.should_receive(:updater_id=)
+ it "should set updater if attribute exists" do
+ record = mock('Record', :creator= => nil, :updater= => nil, :new_record? => false, :updater => nil)
+ record.should_receive(:updater=)
@sweeper.before_validation(record)
end
- it "should NOT set updater_id if attribute does not exist" do
- record = mock('Record', :creator_id= => nil, :updater_id= => nil, :new_record? => :false, :respond_to? => false)
- record.should_receive(:respond_to?).with("updater_id=").and_return(false)
- record.should_not_receive(:updater_id=)
+ it "should NOT set updater if attribute does not exist" do
+ record = mock('Record', :creator= => nil, :updater= => nil, :new_record? => :false, :respond_to? => false)
+ record.should_receive(:respond_to?).with("updater=").and_return(false)
+ record.should_not_receive(:updater=)
@sweeper.before_validation(record)
end
end
@@ -69,8 +70,8 @@ describe UserStampSweeper, "#before_validation (with custom attribute names)" do
describe "(with new record)" do
it "should set created_by if attribute exists" do
- record = mock('Record', :created_by= => nil, :updated_by= => nil, :new_record? => true)
- record.should_receive(:created_by=).with(220).once
+ record = mock('Record', :created_by= => nil, :updated_by => nil, :updated_by= => nil, :new_record? => true, :created_by_id_changed? => false, :created_by_type_changed? => false, :updated_by_id_changed? => false, :updated_by_type_changed? => false)
+ record.should_receive(:created_by=).with(@@user).once
@sweeper.before_validation(record)
end
@@ -84,20 +85,20 @@ describe UserStampSweeper, "#before_validation (with custom attribute names)" do
describe "(with non new record)" do
it "should NOT set created_by if attribute exists" do
- record = mock('Record', :created_by= => nil, :updated_by= => nil, :new_record? => false)
+ record = mock('Record', :created_by= => nil, :updated_by => nil, :updated_by= => nil, :new_record? => false, :updated_by_id_changed? => false, :updated_by_type_changed? => false)
record.should_not_receive(:created_by=)
@sweeper.before_validation(record)
end
it "should NOT set created_by if attribute does not exist" do
- record = mock('Record', :updated_by= => nil, :new_record? => false)
+ record = mock('Record', :updated_by= => nil, :updated_by => nil, :new_record? => false, :updated_by_id_changed? => false, :updated_by_type_changed? => false)
record.should_not_receive(:created_by=)
@sweeper.before_validation(record)
end
end
it "should set updated_by if attribute exists" do
- record = mock('Record', :created_by= => nil, :updated_by= => nil, :new_record? => :false)
+ record = mock('Record', :created_by= => nil, :updated_by= => nil, :updated_by => nil, :new_record? => :false, :created_by_id_changed? => false, :created_by_type_changed? => false, :updated_by_id_changed? => false, :updated_by_type_changed? => false)
record.should_receive(:updated_by=)
@sweeper.before_validation(record)
end
@@ -108,12 +109,44 @@ describe UserStampSweeper, "#before_validation (with custom attribute names)" do
record.should_not_receive(:updated_by=)
@sweeper.before_validation(record)
end
+
+ it "should NOT set created_by if attribute changed" do
+ record = mock('Record', :created_by= => nil, :updated_by= => nil, :new_record? => true, :created_by_id_changed? => true, :created_by_type_changed? => true)
+ record.should_receive(:respond_to?).with("updated_by=").and_return(false)
+ record.should_receive(:respond_to?).with("created_by=").and_return(true)
+ record.should_not_receive(:created_by=)
+ @sweeper.before_validation(record)
+ end
+
+ it "should NOT set updated_by if attribute is not nil" do
+ record = mock('Record', :created_by= => nil, :updated_by= => nil, :updated_by => 1, :new_record? => false)
+ record.should_receive(:respond_to?).with("updated_by=").and_return(true)
+ record.should_receive(:respond_to?).with("created_by=").and_return(false)
+ record.should_not_receive(:updated_by=)
+ @sweeper.before_validation(record)
+ end
+
+ it "should set created_by if attribute has not changed" do
+ record = mock('Record', :created_by= => nil, :updated_by= => nil, :new_record? => true, :created_by_id_changed? => false, :created_by_type_changed? => false)
+ record.should_receive(:respond_to?).with("updated_by=").and_return(false)
+ record.should_receive(:respond_to?).with("created_by=").and_return(true)
+ record.should_receive(:created_by=)
+ @sweeper.before_validation(record)
+ end
+
+ it "should set updated_by if attribute is nil" do
+ record = mock('Record', :created_by= => nil, :updated_by= => nil, :updated_by => nil, :new_record? => false)
+ record.should_receive(:respond_to?).with("updated_by=").and_return(true)
+ record.should_receive(:respond_to?).with("created_by=").and_return(false)
+ record.should_receive(:updated_by=)
+ @sweeper.before_validation(record)
+ end
end
describe UserStampSweeper, "#current_user" do
before do
- UserStamp.creator_attribute = :creator_id
- UserStamp.updater_attribute = :updater_id
+ UserStamp.creator_attribute = :creator
+ UserStamp.updater_attribute = :updater
UserStamp.current_user_method = :current_user
@sweeper = UserStampSweeper.instance
end
@@ -137,8 +170,8 @@ end
describe UserStampSweeper, "#current_user (with custom current_user_method)" do
before do
- UserStamp.creator_attribute = :creator_id
- UserStamp.updater_attribute = :updater_id
+ UserStamp.creator_attribute = :creator
+ UserStamp.updater_attribute = :updater
UserStamp.current_user_method = :my_user
@sweeper = UserStampSweeper.instance
end
@@ -158,4 +191,4 @@ describe UserStampSweeper, "#current_user (with custom current_user_method)" do
controller.should_not_receive(:my_user)
@sweeper.send(:current_user)
end
-end
\ No newline at end of file
+end
--
libgit2 0.21.2