Commit 2827fff7c76aa180de0323ab2b99339fe4e71cf9

Authored by Braulio Bhavamitra
2 parents 608ddc9d b7f7e9fc

Merge branch 'global-user' into 'master'

Global thread-safe current user for models use

See merge request !362
app/models/user.rb
... ... @@ -15,6 +15,14 @@ class User < ActiveRecord::Base
15 15 :email => {:label => _('Email'), :weight => 5},
16 16 }
17 17  
  18 + # see http://stackoverflow.com/a/2513456/670229
  19 + def self.current
  20 + Thread.current[:current_user]
  21 + end
  22 + def self.current=(user)
  23 + Thread.current[:current_user] = user
  24 + end
  25 +
18 26 def self.[](login)
19 27 self.find_by_login(login)
20 28 end
... ...
config/initializers/action_tracker.rb
... ... @@ -32,6 +32,8 @@ ActionTrackerConfig.verbs = {
32 32 },
33 33 }
34 34  
35   -ActionTrackerConfig.current_user_method = :current_person
  35 +ActionTrackerConfig.current_user = proc do
  36 + User.current.person rescue nil
  37 +end
36 38  
37 39 ActionTrackerConfig.timeout = 24.hours
... ...
lib/authenticated_system.rb
1 1 module AuthenticatedSystem
  2 +
2 3 protected
  4 +
  5 + # See impl. from http://stackoverflow.com/a/2513456/670229
  6 + def self.included? base
  7 + base.around_filter do
  8 + begin
  9 + User.current = current_user
  10 + yield
  11 + ensure
  12 + # to address the thread variable leak issues in Puma/Thin webserver
  13 + User.current = nil
  14 + end
  15 + end
  16 + end
  17 +
3 18 # Returns true or false if the user is logged in.
4 19 # Preloads @current_user with the user model if they're logged in.
5 20 def logged_in?
... ... @@ -8,7 +23,9 @@ module AuthenticatedSystem
8 23  
9 24 # Accesses the current user from the session.
10 25 def current_user
11   - @current_user ||= (session[:user] && User.find_by_id(session[:user])) || nil
  26 + @current_user ||= begin
  27 + User.current = (session[:user] && User.find_by_id(session[:user])) || nil
  28 + end
12 29 end
13 30  
14 31 # Store the given user in the session.
... ... @@ -19,7 +36,7 @@ module AuthenticatedSystem
19 36 session[:user] = new_user.id
20 37 new_user.register_login
21 38 end
22   - @current_user = new_user
  39 + @current_user = User.current = new_user
23 40 end
24 41  
25 42 # Check if the user is authorized.
... ...
test/action_tracker_test_helper.rb
1   -class UserStampSweeper < ActionController::Caching::Sweeper
2   - private
3   - def current_user
4   - Person.first
5   - end
  1 +class User
  2 + def self.current
  3 + Thread.current[:current_user] || User.first
  4 + end
6 5 end
7 6  
8 7 module ActionTracker
... ...
test/functional/profile_controller_test.rb
... ... @@ -703,13 +703,13 @@ class ProfileControllerTest &lt; ActionController::TestCase
703 703 p1= create_user.person
704 704 p2= create_user.person
705 705  
706   - UserStampSweeper.any_instance.stubs(:current_user).returns(p1)
  706 + User.current = p1.user
707 707 scrap1 = create(Scrap, defaults_for_scrap(:sender => p1, :receiver => p2))
708 708  
709   - UserStampSweeper.any_instance.stubs(:current_user).returns(p2)
  709 + User.current = p2.user
710 710 scrap2 = create(Scrap, defaults_for_scrap(:sender => p2, :receiver => p1))
711 711  
712   - UserStampSweeper.any_instance.stubs(:current_user).returns(p1)
  712 + User.current = p1.user
713 713 create(TinyMceArticle, :profile => p1, :name => 'An article about free software')
714 714 a1 = ActionTracker::Record.last
715 715  
... ... @@ -738,10 +738,10 @@ class ProfileControllerTest &lt; ActionController::TestCase
738 738 scrap1 = create(Scrap, defaults_for_scrap(:sender => p2, :receiver => p3))
739 739 scrap2 = create(Scrap, defaults_for_scrap(:sender => p2, :receiver => profile))
740 740  
741   - UserStampSweeper.any_instance.stubs(:current_user).returns(p3)
  741 + User.current = p3.user
742 742 article1 = TinyMceArticle.create!(:profile => p3, :name => 'An article about free software')
743 743  
744   - UserStampSweeper.any_instance.stubs(:current_user).returns(p2)
  744 + User.current = p2.user
745 745 article2 = TinyMceArticle.create!(:profile => p2, :name => 'Another article about free software')
746 746  
747 747 login_as(profile.identifier)
... ... @@ -761,15 +761,15 @@ class ProfileControllerTest &lt; ActionController::TestCase
761 761  
762 762 ActionTracker::Record.delete_all
763 763  
764   - UserStampSweeper.any_instance.stubs(:current_user).returns(p1)
  764 + User.current = p1.user
765 765 create(Scrap,defaults_for_scrap(:sender => p1, :receiver => p1))
766 766 a1 = ActionTracker::Record.last
767 767  
768   - UserStampSweeper.any_instance.stubs(:current_user).returns(p2)
  768 + User.current = p2.user
769 769 create(Scrap, defaults_for_scrap(:sender => p2, :receiver => p3))
770 770 a2 = ActionTracker::Record.last
771 771  
772   - UserStampSweeper.any_instance.stubs(:current_user).returns(p3)
  772 + User.current = p3.user
773 773 create(Scrap, defaults_for_scrap(:sender => p3, :receiver => p1))
774 774 a3 = ActionTracker::Record.last
775 775  
... ... @@ -791,15 +791,15 @@ class ProfileControllerTest &lt; ActionController::TestCase
791 791  
792 792 ActionTracker::Record.delete_all
793 793  
794   - UserStampSweeper.any_instance.stubs(:current_user).returns(p1)
  794 + User.current = p1.user
795 795 create(Scrap, defaults_for_scrap(:sender => p1, :receiver => p1))
796 796 a1 = ActionTracker::Record.last
797 797  
798   - UserStampSweeper.any_instance.stubs(:current_user).returns(p2)
  798 + User.current = p2.user
799 799 create(Scrap, defaults_for_scrap(:sender => p2, :receiver => p3))
800 800 a2 = ActionTracker::Record.last
801 801  
802   - UserStampSweeper.any_instance.stubs(:current_user).returns(p3)
  802 + User.current = p3.user
803 803 create(Scrap, defaults_for_scrap(:sender => p3, :receiver => p1))
804 804 a3 = ActionTracker::Record.last
805 805  
... ... @@ -833,10 +833,10 @@ class ProfileControllerTest &lt; ActionController::TestCase
833 833 ActionTracker::Record.destroy_all
834 834 create(Scrap, defaults_for_scrap(:sender => p1, :receiver => p1))
835 835 a1 = ActionTracker::Record.last
836   - UserStampSweeper.any_instance.stubs(:current_user).returns(p2)
  836 + User.current = p2.user
837 837 create(Scrap, defaults_for_scrap(:sender => p2, :receiver => p3))
838 838 a2 = ActionTracker::Record.last
839   - UserStampSweeper.any_instance.stubs(:current_user).returns(p3)
  839 + User.current = p3.user
840 840 create(Scrap, defaults_for_scrap(:sender => p3, :receiver => p1))
841 841 a3 = ActionTracker::Record.last
842 842  
... ... @@ -868,7 +868,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
868 868 ActionTracker::Record.destroy_all
869 869 create(Article, :name => 'a', :profile_id => community.id)
870 870 create(Article, :name => 'b', :profile_id => community.id)
871   - UserStampSweeper.any_instance.stubs(:current_user).returns(p2)
  871 + User.current = p2.user
872 872 create(Article, :name => 'c', :profile_id => community.id)
873 873 process_delayed_job_queue
874 874  
... ... @@ -895,10 +895,10 @@ class ProfileControllerTest &lt; ActionController::TestCase
895 895 ActionTracker::Record.destroy_all
896 896 create(Scrap, defaults_for_scrap(:sender => p1, :receiver => p1))
897 897 a1 = ActionTracker::Record.last
898   - UserStampSweeper.any_instance.stubs(:current_user).returns(p2)
  898 + User.current = p2.user
899 899 create(Scrap, defaults_for_scrap(:sender => p2, :receiver => p3))
900 900 a2 = ActionTracker::Record.last
901   - UserStampSweeper.any_instance.stubs(:current_user).returns(p3)
  901 + User.current = p3.user
902 902 create(Scrap, defaults_for_scrap(:sender => p3, :receiver => p1))
903 903 a3 = ActionTracker::Record.last
904 904  
... ... @@ -1317,7 +1317,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
1317 1317 another_person = fast_create(Person)
1318 1318 create(Scrap, defaults_for_scrap(:sender => another_person, :receiver => profile, :content => 'A scrap'))
1319 1319  
1320   - UserStampSweeper.any_instance.stubs(:current_user).returns(profile)
  1320 + User.current = profile.user
1321 1321 ActionTracker::Record.destroy_all
1322 1322 TinyMceArticle.create!(:profile => profile, :name => 'An article about free software')
1323 1323  
... ... @@ -1332,7 +1332,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
1332 1332 another_person = fast_create(Person)
1333 1333 scrap = create(Scrap, defaults_for_scrap(:sender => another_person, :receiver => profile, :content => 'A scrap'))
1334 1334  
1335   - UserStampSweeper.any_instance.stubs(:current_user).returns(profile)
  1335 + User.current = profile.user
1336 1336 ActionTracker::Record.destroy_all
1337 1337 TinyMceArticle.create!(:profile => profile, :name => 'An article about free software')
1338 1338 activity = ActionTracker::Record.last
... ... @@ -1380,7 +1380,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
1380 1380 end
1381 1381  
1382 1382 should 'display comment in wall if user was removed after click in view all comments' do
1383   - UserStampSweeper.any_instance.stubs(:current_user).returns(profile)
  1383 + User.current = profile.user
1384 1384 article = TinyMceArticle.create!(:profile => profile, :name => 'An article about free software')
1385 1385 to_be_removed = create_user('removed_user').person
1386 1386 comment = create(Comment, :author => to_be_removed, :title => 'Test Comment', :body => 'My author does not exist =(', :source_id => article.id, :source_type => 'Article')
... ... @@ -1397,7 +1397,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
1397 1397 end
1398 1398  
1399 1399 should 'not display spam comments in wall' do
1400   - UserStampSweeper.any_instance.stubs(:current_user).returns(profile)
  1400 + User.current = profile.user
1401 1401 article = TinyMceArticle.create!(:profile => profile, :name => 'An article about spam\'s nutritional attributes')
1402 1402 comment = create(Comment, :author => profile, :title => 'Test Comment', :body => 'This article makes me hungry', :source_id => article.id, :source_type => 'Article')
1403 1403 comment.spam!
... ... @@ -1408,7 +1408,7 @@ class ProfileControllerTest &lt; ActionController::TestCase
1408 1408 end
1409 1409  
1410 1410 should 'display comment in wall from non logged users after click in view all comments' do
1411   - UserStampSweeper.any_instance.stubs(:current_user).returns(profile)
  1411 + User.current = profile.user
1412 1412 article = TinyMceArticle.create!(:profile => profile, :name => 'An article about free software')
1413 1413 comment = create(Comment, :name => 'outside user', :email => 'outside@localhost.localdomain', :title => 'Test Comment', :body => 'My author does not exist =(', :source_id => article.id, :source_type => 'Article')
1414 1414  
... ...
test/test_helper.rb
... ... @@ -76,6 +76,12 @@ class ActiveSupport::TestCase
76 76  
77 77 end
78 78  
  79 + setup :global_setup
  80 +
  81 + def global_setup
  82 + User.current = nil
  83 + end
  84 +
79 85 alias :ok :assert_block
80 86  
81 87 def assert_equivalent(enum1, enum2)
... ...
test/unit/action_tracker_notification_test.rb
... ... @@ -89,7 +89,7 @@ class ActionTrackerNotificationTest &lt; ActiveSupport::TestCase
89 89 end
90 90  
91 91 should "have comments through article action_tracker" do
92   - person = fast_create(Person)
  92 + person = create_user.person
93 93 article = create(TextileArticle, :profile_id => person.id)
94 94 process_delayed_job_queue
95 95 notification = ActionTrackerNotification.last
... ...
test/unit/article_test.rb
... ... @@ -985,12 +985,12 @@ class ArticleTest &lt; ActiveSupport::TestCase
985 985  
986 986 should 'track action when a published article is created in a community' do
987 987 community = fast_create(Community)
988   - p1 = fast_create(Person)
989   - p2 = fast_create(Person)
990   - p3 = fast_create(Person)
  988 + p1 = create_user.person
  989 + p2 = create_user.person
  990 + p3 = create_user.person
991 991 community.add_member(p1)
992 992 community.add_member(p2)
993   - UserStampSweeper.any_instance.expects(:current_user).returns(p1).at_least_once
  993 + User.current = p1.user
994 994  
995 995 article = create(TinyMceArticle, :profile_id => community.id)
996 996 activity = article.activity
... ... @@ -1085,11 +1085,11 @@ class ArticleTest &lt; ActiveSupport::TestCase
1085 1085 end
1086 1086  
1087 1087 should 'create the notification to organization and all organization members' do
1088   - Profile.delete_all
1089   - ActionTracker::Record.delete_all
  1088 + Profile.destroy_all
  1089 + ActionTracker::Record.destroy_all
1090 1090  
1091 1091 community = fast_create(Community)
1092   - member_1 = fast_create(Person)
  1092 + member_1 = create_user.person
1093 1093 community.add_member(member_1)
1094 1094  
1095 1095 article = create TinyMceArticle, :name => 'Tracked Article 1', :profile_id => community.id
... ... @@ -1116,7 +1116,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1116 1116 Article.destroy_all
1117 1117 ActionTracker::Record.destroy_all
1118 1118 ActionTrackerNotification.destroy_all
1119   - UserStampSweeper.any_instance.expects(:current_user).returns(profile).at_least_once
  1119 + User.current = profile.user
1120 1120 article = create(TinyMceArticle, :profile_id => profile.id)
1121 1121  
1122 1122 process_delayed_job_queue
... ... @@ -1127,7 +1127,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1127 1127 f1 = fast_create(Person)
1128 1128 profile.add_friend(f1)
1129 1129  
1130   - UserStampSweeper.any_instance.expects(:current_user).returns(profile).at_least_once
  1130 + User.current = profile.user
1131 1131 article = create TinyMceArticle, :name => 'Tracked Article 1', :profile_id => profile.id
1132 1132 assert_equal 1, ActionTracker::Record.find_all_by_verb('create_article').count
1133 1133 process_delayed_job_queue
... ... @@ -1147,7 +1147,7 @@ class ArticleTest &lt; ActiveSupport::TestCase
1147 1147 Article.destroy_all
1148 1148 ActionTracker::Record.destroy_all
1149 1149 ActionTrackerNotification.destroy_all
1150   - UserStampSweeper.any_instance.expects(:current_user).returns(profile).at_least_once
  1150 + User.current = profile.user
1151 1151 article = create(TinyMceArticle, :profile_id => profile.id)
1152 1152 activity = article.activity
1153 1153  
... ... @@ -1165,11 +1165,11 @@ class ArticleTest &lt; ActiveSupport::TestCase
1165 1165  
1166 1166 should 'destroy action_tracker and notifications when an article is destroyed in a community' do
1167 1167 community = fast_create(Community)
1168   - p1 = fast_create(Person)
1169   - p2 = fast_create(Person)
  1168 + p1 = create_user.person
  1169 + p2 = create_user.person
1170 1170 community.add_member(p1)
1171 1171 community.add_member(p2)
1172   - UserStampSweeper.any_instance.expects(:current_user).returns(p1).at_least_once
  1172 + User.current = p1.user
1173 1173  
1174 1174 article = create(TinyMceArticle, :profile_id => community.id)
1175 1175 activity = article.activity
... ...
test/unit/comment_test.rb
... ... @@ -286,7 +286,7 @@ class CommentTest &lt; ActiveSupport::TestCase
286 286 end
287 287  
288 288 should "return activities comments as a thread" do
289   - person = fast_create(Person)
  289 + person = create_user.person
290 290 a = TextileArticle.create!(:profile => person, :name => 'My article', :body => 'Article body')
291 291 c0 = Comment.create!(:source => a, :body => 'My comment', :author => person)
292 292 c1 = Comment.create!(:reply_of_id => c0.id, :source => a, :body => 'bla', :author => person)
... ... @@ -302,7 +302,7 @@ class CommentTest &lt; ActiveSupport::TestCase
302 302 end
303 303  
304 304 should "return activities comments when some comment on thread is spam and not display its replies" do
305   - person = fast_create(Person)
  305 + person = create_user.person
306 306 a = TextileArticle.create!(:profile => person, :name => 'My article', :body => 'Article body')
307 307 c0 = Comment.create(:source => a, :body => 'Root comment', :author => person)
308 308 c1 = Comment.create(:reply_of_id => c0.id, :source => a, :body => 'c1', :author => person)
... ...
test/unit/community_test.rb
... ... @@ -301,8 +301,8 @@ class CommunityTest &lt; ActiveSupport::TestCase
301 301 ActionTrackerNotification.delete_all
302 302 p1 = Person.first
303 303 community = fast_create(Community)
304   - p2 = fast_create(Person)
305   - p3 = fast_create(Person)
  304 + p2 = create_user.person
  305 + p3 = create_user.person
306 306 community.add_member(p3)
307 307 article = create(TextileArticle, :profile_id => community.id)
308 308 time = article.activity.updated_at + 1.day
... ... @@ -372,10 +372,10 @@ class CommunityTest &lt; ActiveSupport::TestCase
372 372 end
373 373  
374 374 should 'return tracked_actions of community as activities' do
375   - person = fast_create(Person)
  375 + person = create_user.person
376 376 community = fast_create(Community)
377 377  
378   - UserStampSweeper.any_instance.expects(:current_user).returns(person).at_least_once
  378 + User.current = person.user
379 379 assert_difference 'ActionTracker::Record.count', 1 do
380 380 article = create(TinyMceArticle, :profile => community, :name => 'An article about free software')
381 381 assert_equal [article.activity], community.activities.map { |a| a.klass.constantize.find(a.id) }
... ... @@ -387,7 +387,7 @@ class CommunityTest &lt; ActiveSupport::TestCase
387 387 community = fast_create(Community)
388 388 community2 = fast_create(Community)
389 389  
390   - UserStampSweeper.any_instance.expects(:current_user).returns(person).at_least_once
  390 + User.current = person.user
391 391 article = create(TinyMceArticle, :profile => community2, :name => 'Another article about free software')
392 392  
393 393 assert_not_includes community.activities.map { |a| a.klass.constantize.find(a.id) }, article.activity
... ...
test/unit/enterprise_test.rb
... ... @@ -475,7 +475,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
475 475 person = fast_create(Person)
476 476 enterprise = fast_create(Enterprise)
477 477  
478   - UserStampSweeper.any_instance.expects(:current_user).returns(person).at_least_once
  478 + User.current = person.user
479 479 article = create(TinyMceArticle, :profile => enterprise, :name => 'An article about free software')
480 480  
481 481 assert_equal [article.activity], enterprise.activities.map { |a| a.klass.constantize.find(a.id) }
... ... @@ -486,7 +486,7 @@ class EnterpriseTest &lt; ActiveSupport::TestCase
486 486 enterprise = fast_create(Enterprise)
487 487 enterprise2 = fast_create(Enterprise)
488 488  
489   - UserStampSweeper.any_instance.expects(:current_user).returns(person).at_least_once
  489 + User.current = person.user
490 490 article = create(TinyMceArticle, :profile => enterprise2, :name => 'Another article about free software')
491 491  
492 492 assert_not_includes enterprise.activities.map { |a| a.klass.constantize.find(a.id) }, article.activity
... ...
test/unit/person_test.rb
... ... @@ -1249,9 +1249,9 @@ class PersonTest &lt; ActiveSupport::TestCase
1249 1249 person = create_user.person
1250 1250 another_person = create_user.person
1251 1251  
1252   - UserStampSweeper.any_instance.stubs(:current_user).returns(another_person)
  1252 + User.current = another_person.user
1253 1253 scrap = create(Scrap, defaults_for_scrap(:sender => another_person, :receiver => person, :content => 'A scrap'))
1254   - UserStampSweeper.any_instance.expects(:current_user).returns(person).at_least_once
  1254 + User.current = person.user
1255 1255 article = create(TinyMceArticle, :profile => person, :name => 'An article about free software')
1256 1256  
1257 1257 assert_equivalent [scrap,article.activity], person.activities.map { |a| a.klass.constantize.find(a.id) }
... ... @@ -1259,17 +1259,17 @@ class PersonTest &lt; ActiveSupport::TestCase
1259 1259  
1260 1260 should 'not return tracked_actions and scraps from others as activities' do
1261 1261 ActionTracker::Record.destroy_all
1262   - person = fast_create(Person)
1263   - another_person = fast_create(Person)
  1262 + person = create_user.person
  1263 + another_person = create_user.person
1264 1264  
1265 1265 person_scrap = create(Scrap, defaults_for_scrap(:sender => person, :receiver => person, :content => 'A scrap from person'))
1266 1266 another_person_scrap = create(Scrap, defaults_for_scrap(:sender => another_person, :receiver => another_person, :content => 'A scrap from another person'))
1267 1267  
1268   - UserStampSweeper.any_instance.stubs(:current_user).returns(another_person)
  1268 + User.current = another_person.user
1269 1269 create(TinyMceArticle, :profile => another_person, :name => 'An article about free software from another person')
1270 1270 another_person_activity = ActionTracker::Record.last
1271 1271  
1272   - UserStampSweeper.any_instance.stubs(:current_user).returns(person)
  1272 + User.current = person.user
1273 1273 create(TinyMceArticle, :profile => person, :name => 'An article about free software')
1274 1274 person_activity = ActionTracker::Record.last
1275 1275  
... ...
test/unit/textile_article_test.rb
1 1 require_relative "../test_helper"
2 2  
3 3 class TextileArticleTest < ActiveSupport::TestCase
4   -
  4 +
5 5 def setup
6 6 @profile = create_user('testing').person
7   - ActionTracker::Record.stubs(:current_user_from_model).returns(fast_create(Person))
8 7 end
9 8 attr_reader :profile
10 9  
... ... @@ -128,7 +127,7 @@ class TextileArticleTest &lt; ActiveSupport::TestCase
128 127 assert_equal true, a.notifiable?
129 128 assert_equal true, a.advertise?
130 129 assert_equal true, a.is_trackable?
131   -
  130 +
132 131 a.published=false
133 132 assert_equal false, a.published?
134 133 assert_equal false, a.is_trackable?
... ...
test/unit/uploaded_file_test.rb
... ... @@ -325,7 +325,6 @@ class UploadedFileTest &lt; ActiveSupport::TestCase
325 325  
326 326 should 'group trackers activity of image\'s upload' do
327 327 ActionTracker::Record.delete_all
328   - ActionTracker::Record.stubs(:current_user_from_model).returns(profile)
329 328 gallery = fast_create(Gallery, :profile_id => profile.id)
330 329  
331 330 image1 = UploadedFile.create!(:uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'), :parent => gallery, :profile => profile)
... ...
vendor/plugins/action_tracker/init.rb
1   -require "user_stamp"
2   -
3   -UserStamp.creator_attribute = :user
4   -UserStamp.updater_attribute = :user
5   -
6   -class ActionController::Base
7   - extend UserStamp::ClassMethods
8   -end
9   -
10 1 require "action_tracker"
... ...
vendor/plugins/action_tracker/lib/action_tracker.rb
... ... @@ -5,7 +5,6 @@ module ActionTracker
5 5 module ControllerMethods
6 6  
7 7 def self.included(base)
8   - base.send :user_stamp, ActionTracker::Record
9 8 base.send :extend, ClassMethods
10 9 end
11 10  
... ... @@ -42,7 +41,7 @@ module ActionTracker
42 41 elsif keep_params.to_s == 'all'
43 42 stored_params = params
44 43 end
45   - user = send ActionTrackerConfig.current_user_method
  44 + user = send ActionTrackerConfig.current_user
46 45 tracked_action = case ActionTrackerConfig.verb_type(verb)
47 46 when :groupable
48 47 Record.add_or_create :verb => verb, :user => user, :params => stored_params
... ... @@ -90,7 +89,7 @@ module ActionTracker
90 89  
91 90 def save_action_for_verb(verb, keep_params = :all, post_proc = Proc.new{}, custom_user = nil, custom_target = nil)
92 91 user = self.send(custom_user) unless custom_user.blank?
93   - user ||= ActionTracker::Record.current_user_from_model
  92 + user ||= ActionTracker::Record.current_user
94 93 target = self.send(custom_target) unless custom_target.blank?
95 94 return nil if user.nil?
96 95 if keep_params.is_a? Array
... ... @@ -115,7 +114,7 @@ module ActionTracker
115 114 end
116 115 tracked_action.target = target || self
117 116 user.tracked_actions << tracked_action
118   - post_proc.call tracked_action.reload
  117 + post_proc.call tracked_action
119 118 end
120 119  
121 120 end
... ...
vendor/plugins/action_tracker/lib/action_tracker_config.rb
... ... @@ -20,12 +20,12 @@ class ActionTrackerConfig
20 20 verbs.keys.map(&:to_s)
21 21 end
22 22  
23   - def self.current_user_method
24   - config[:current_user_method] || :current_user
  23 + def self.current_user
  24 + config[:current_user] || proc{ nil }
25 25 end
26 26  
27   - def self.current_user_method=(method_name)
28   - UserStamp.current_user_method = config[:current_user_method] = method_name
  27 + def self.current_user= block
  28 + config[:current_user] = block
29 29 end
30 30  
31 31 def self.default_filter_time
... ...
vendor/plugins/action_tracker/lib/action_tracker_model.rb
... ... @@ -27,14 +27,12 @@ module ActionTracker
27 27 scope :recent, :conditions => ['created_at >= ?', RECENT_DELAY.days.ago]
28 28 scope :visible, :conditions => { :visible => true }
29 29  
30   - def self.current_user_from_model
31   - u = new
32   - u.valid?
33   - u.user
  30 + def self.current_user
  31 + ActionTrackerConfig.current_user.call
34 32 end
35 33  
36 34 def self.update_or_create(params)
37   - u = params[:user] || current_user_from_model
  35 + u = params[:user] || current_user
38 36 return if u.nil?
39 37 target_hash = params[:target].nil? ? {} : {:target_type => params[:target].class.base_class.to_s, :target_id => params[:target].id}
40 38 conditions = { :user_id => u.id, :user_type => u.class.base_class.to_s, :verb => params[:verb].to_s }.merge(target_hash)
... ... @@ -44,7 +42,7 @@ module ActionTracker
44 42 end
45 43  
46 44 def self.add_or_create(params)
47   - u = params[:user] || current_user_from_model
  45 + u = params[:user] || current_user
48 46 return if u.nil?
49 47 target_hash = params[:target].nil? ? {} : {:target_type => params[:target].class.base_class.to_s, :target_id => params[:target].id}
50 48 l = last :conditions => { :user_id => u.id, :user_type => u.class.base_class.to_s, :verb => params[:verb].to_s }.merge(target_hash)
... ...
vendor/plugins/action_tracker/test/action_tracker_config_test.rb
... ... @@ -33,16 +33,6 @@ class ActionTrackerConfigTest &lt; ActiveSupport::TestCase
33 33 %w(search delete login).each { |verb| assert ActionTrackerConfig.verb_names.include?(verb) }
34 34 end
35 35  
36   - def test_current_user_is_default_method
37   - ActionTrackerConfig.config[:current_user_method] = nil
38   - assert_equal :current_user, ActionTrackerConfig.current_user_method
39   - end
40   -
41   - def test_current_user_can_be_set
42   - ActionTrackerConfig.current_user_method = :logged_in_user
43   - assert_equal :logged_in_user, ActionTrackerConfig.current_user_method
44   - end
45   -
46 36 def test_default_filter_time_is_after
47 37 ActionTrackerConfig.config[:default_filter_time] = nil
48 38 assert_equal :after, ActionTrackerConfig.default_filter_time
... ... @@ -66,7 +56,7 @@ class ActionTrackerConfigTest &lt; ActiveSupport::TestCase
66 56 def test_get_verb_return_hash
67 57 assert_kind_of Hash, ActionTrackerConfig.get_verb(:search)
68 58 end
69   -
  59 +
70 60 def test_get_verb_symbol_search_by_symbol
71 61 ActionTrackerConfig.verbs = { :search => { :description => "Got it" } }
72 62 assert_equal "Got it", ActionTrackerConfig.get_verb(:search)[:description]
... ...
vendor/plugins/action_tracker/test/action_tracker_test.rb
... ... @@ -43,10 +43,6 @@ class ThingsController &lt; ActionController::Base
43 43 render :text => "test"
44 44 end
45 45  
46   - def current_user
47   - SomeModel.first || SomeModel.create!
48   - end
49   -
50 46 def rescue_action(e)
51 47 raise e
52 48 end
... ... @@ -58,9 +54,7 @@ ActionController::Routing::Routes.draw { |map| map.resources :things, :collectio
58 54 class ActionTrackerTest < ActiveSupport::TestCase
59 55  
60 56 def setup
61   - UserStamp.creator_attribute = :user
62   - UserStamp.updater_attribute = :user
63   - ActionTrackerConfig.current_user_method = :current_user
  57 + ActionTrackerConfig.current_user = proc{ SomeModel.first || SomeModel.create! }
64 58 ActionTracker::Record.delete_all
65 59 ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something" } }
66 60 @request = ActionController::TestRequest.new
... ... @@ -108,7 +102,7 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
108 102 end
109 103  
110 104 def test_track_actions_executes_block
111   - @controller = create_controller do
  105 + @controller = create_controller do
112 106 track_actions :some_verb do
113 107 throw :some_symbol
114 108 end
... ... @@ -162,7 +156,7 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
162 156 assert_difference 'ActionTracker::Record.count' do
163 157 get :index, :foo => 5
164 158 end
165   - assert_equal({"action"=>"index", "foo"=>"5", "controller"=>"things"}, ActionTracker::Record.first.params)
  159 + assert_equal({"action"=>"index", "foo"=>"5", "controller"=>"things"}, ActionTracker::Record.first.params)
166 160 end
167 161  
168 162 def test_keep_params_not_set_should_store_all_params
... ... @@ -228,16 +222,15 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
228 222 def test_store_user
229 223 @controller = create_controller do
230 224 track_actions_before :some_verb
231   - def current_user
232   - SomeModel.create! :some_column => "test"
233   - end
234 225 end
  226 + ActionTrackerConfig.current_user = proc{ SomeModel.create! :some_column => "test" }
  227 +
235 228 assert_difference 'ActionTracker::Record.count' do
236 229 get :test
237 230 end
238 231 assert_equal "test", ActionTracker::Record.last.user.some_column
239 232 end
240   -
  233 +
241 234 def test_should_update_when_verb_is_updatable_and_no_timeout
242 235 ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something", :type => :updatable } }
243 236 ActionTrackerConfig.timeout = 5.minutes
... ... @@ -252,7 +245,7 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
252 245 assert_no_difference 'ActionTracker::Record.count' do
253 246 get :test
254 247 end
255   - end
  248 + end
256 249  
257 250 def test_should_create_when_verb_is_updatable_and_timeout
258 251 ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something", :type => :updatable } }
... ... @@ -268,7 +261,7 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
268 261 assert_difference 'ActionTracker::Record.count' do
269 262 get :test
270 263 end
271   - end
  264 + end
272 265  
273 266 def test_should_update_when_verb_is_groupable_and_no_timeout
274 267 ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something", :type => :groupable } }
... ... @@ -284,7 +277,7 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
284 277 assert_no_difference 'ActionTracker::Record.count' do
285 278 get :test, :foo => "test"
286 279 end
287   - end
  280 + end
288 281  
289 282 def test_should_create_when_verb_is_groupable_and_timeout
290 283 ActionTrackerConfig.verbs = { :some_verb => { :description => "Did something", :type => :groupable } }
... ... @@ -330,7 +323,7 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
330 323 def test_should_get_time_spent_doing_something
331 324 ActionTrackerConfig.verbs = { :some_verb => { :type => :updatable }, :other_verb => { :type => :updatable } }
332 325 m = SomeModel.create!
333   - @controller = create_controller do
  326 + @controller = create_controller do
334 327 track_actions :some_verb
335 328 end
336 329 @controller.stubs(:current_user).returns(m)
... ... @@ -394,7 +387,7 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
394 387 assert_equal "foo", ActionTracker::Record.last.params["other_column"]
395 388 assert_nil ActionTracker::Record.last.params["another_column"]
396 389 end
397   -
  390 +
398 391 def test_replace_dots_by_underline_in_param_name
399 392 ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
400 393 model = create_model do
... ... @@ -407,7 +400,7 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
407 400 assert_equal 3, ActionTracker::Record.last.params["other_column_size"]
408 401 assert_equal 5, ActionTracker::Record.last.params["another_column"]
409 402 end
410   -
  403 +
411 404 def test_track_actions_store_all_params
412 405 ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
413 406 model = create_model do
... ... @@ -452,7 +445,7 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
452 445 model = create_model { track_actions :test, :after_create, :keep_params => :all, :if => Proc.new { 2 > 1 } }
453 446 @controller = create_controller_for_model(model)
454 447 assert_difference('ActionTracker::Record.count') { get :test }
455   -
  448 +
456 449 model = create_model { track_actions :test, :after_create, :keep_params => :all, :if => Proc.new { 2 < 1 } }
457 450 @controller = create_controller_for_model(model)
458 451 assert_no_difference('ActionTracker::Record.count') { get :test }
... ... @@ -460,7 +453,7 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
460 453 model = create_model { track_actions :test, :after_create, :keep_params => :all, :unless => Proc.new { 2 > 1 } }
461 454 @controller = create_controller_for_model(model)
462 455 assert_no_difference('ActionTracker::Record.count') { get :test }
463   -
  456 +
464 457 model = create_model { track_actions :test, :after_create, :keep_params => :all, :unless => Proc.new { 2 < 1 } }
465 458 @controller = create_controller_for_model(model)
466 459 assert_difference('ActionTracker::Record.count') { get :test }
... ... @@ -498,13 +491,11 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
498 491 ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
499 492 model = create_model do
500 493 track_actions :test, :after_create, :custom_user => :test_custom_user
501   - def current_user
502   - SomeModel.create!
503   - end
504 494 def test_custom_user
505 495 OtherModel.create!
506 496 end
507 497 end
  498 + ActionTrackerConfig.current_user = proc{ SomeModel.create! }
508 499 @controller = create_controller_for_model(model, :another_column => 2)
509 500 assert_difference('ActionTracker::Record.count') { get :test }
510 501 assert_kind_of OtherModel, ActionTracker::Record.last.user
... ... @@ -514,13 +505,11 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
514 505 ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
515 506 model = create_model do
516 507 track_actions :test, :after_create, "custom_user" => :test_custom_user
517   - def current_user
518   - SomeModel.create!
519   - end
520 508 def test_custom_user
521 509 OtherModel.create!
522 510 end
523 511 end
  512 + ActionTrackerConfig.current_user = proc{ SomeModel.create! }
524 513 @controller = create_controller_for_model(model, :another_column => 2)
525 514 assert_difference('ActionTracker::Record.count') { get :test }
526 515 assert_kind_of OtherModel, ActionTracker::Record.last.user
... ... @@ -530,13 +519,11 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
530 519 ActionTrackerConfig.verbs = { :test => { :description => "Some" } }
531 520 model = create_model do
532 521 track_actions :test, :after_create
533   - def current_user
534   - SomeModel.create!
535   - end
536 522 def test_custom_user
537 523 OtherModel.create!
538 524 end
539 525 end
  526 + ActionTrackerConfig.current_user = proc{ SomeModel.create! }
540 527 @controller = create_controller_for_model(model, :another_column => 2)
541 528 assert_difference('ActionTracker::Record.count') { get :test }
542 529 assert_kind_of SomeModel, ActionTracker::Record.last.user
... ... @@ -625,10 +612,8 @@ class ActionTrackerTest &lt; ActiveSupport::TestCase
625 612 render :text => "test"
626 613 end
627 614  
628   - def current_user
629   - SomeModel.create! :some_column => "test"
630   - end
631 615 end
  616 + ActionTrackerConfig.current_user = proc{ SomeModel.create! :some_column => "test" }
632 617 end
633 618  
634 619 end
... ...
vendor/plugins/user_stamp/MIT-LICENSE
... ... @@ -1,20 +0,0 @@
1   -Copyright (c) 2008 [John Nunemaker]
2   -
3   -Permission is hereby granted, free of charge, to any person obtaining
4   -a copy of this software and associated documentation files (the
5   -"Software"), to deal in the Software without restriction, including
6   -without limitation the rights to use, copy, modify, merge, publish,
7   -distribute, sublicense, and/or sell copies of the Software, and to
8   -permit persons to whom the Software is furnished to do so, subject to
9   -the following conditions:
10   -
11   -The above copyright notice and this permission notice shall be
12   -included in all copies or substantial portions of the Software.
13   -
14   -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15   -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16   -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17   -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18   -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19   -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20   -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
vendor/plugins/user_stamp/README
... ... @@ -1,37 +0,0 @@
1   -= UserStamp
2   -
3   -Rails plugin that makes stamping records with a user when they are
4   -created and updated dirt simple. It assumes that your controller has
5   -a current_user method. It also assumes that any record being stamped
6   -has two attributes--creator_id and updater_id. You can override both
7   -of these assumptions easily.
8   -
9   -== Setup
10   -
11   -1. script/plugin install git://github.com/jnunemaker/user_stamp.git
12   -2. Add user_stamp to application.rb, like the following:
13   -
14   - class ApplicationController < ActionController::Base
15   - user_stamp Post, Asset, Job
16   - end
17   -
18   -
19   -== Defaults
20   -
21   - UserStamp.creator_attribute = :creator_id
22   - UserStamp.updater_attribute = :updater_id
23   - UserStamp.current_user_method = :current_user
24   -
25   -If your user stamped columns and current_user method are different,
26   -just create an initializer such as config/initializers/user_stamp.rb
27   -and copy and paste the defaults above, changing them to fit your app.
28   -
29   -== Problems?
30   -
31   -Use the issue tracker on Github.
32   -
33   -== Docs
34   -
35   -http://rdoc.info/projects/jnunemaker/user_stamp
36   -
37   -Copyright (c) 2008 [John Nunemaker], released under the MIT license
vendor/plugins/user_stamp/Rakefile
... ... @@ -1,11 +0,0 @@
1   -require 'rake'
2   -require 'spec/rake/spectask'
3   -
4   -desc 'Default: run specs.'
5   -task :default => :spec
6   -
7   -desc 'Run the specs'
8   -Spec::Rake::SpecTask.new(:spec) do |t|
9   - t.spec_opts = ['--colour --format progress --loadby mtime --reverse']
10   - t.spec_files = FileList['spec/**/*_spec.rb']
11   -end
12 0 \ No newline at end of file
vendor/plugins/user_stamp/init.rb
... ... @@ -1,5 +0,0 @@
1   -require 'user_stamp'
2   -
3   -class ActionController::Base
4   - extend UserStamp::ClassMethods
5   -end
vendor/plugins/user_stamp/install.rb
... ... @@ -1,15 +0,0 @@
1   -instructions = <<EOF
2   -
3   -#{'*' * 62}
4   -Don't forget to add user stamp to your application controller.
5   -
6   - class ApplicationController < ActionController::Base
7   - user_stamp Post, Asset, Job
8   - end
9   -
10   -View the README for more information.
11   -#{'*' * 62}
12   -
13   -EOF
14   -
15   -puts instructions
16 0 \ No newline at end of file
vendor/plugins/user_stamp/lib/user_stamp.rb
... ... @@ -1,50 +0,0 @@
1   -module UserStamp
2   - mattr_accessor :creator_attribute
3   - mattr_accessor :updater_attribute
4   - mattr_accessor :current_user_method
5   -
6   - def self.creator_assignment_method
7   - "#{UserStamp.creator_attribute}="
8   - end
9   -
10   - def self.updater_assignment_method
11   - "#{UserStamp.updater_attribute}="
12   - end
13   -
14   - module ClassMethods
15   - def user_stamp(*models)
16   - models.each { |klass| UserStampSweeper.observe(klass) }
17   -
18   - class_eval do
19   - cache_sweeper :user_stamp_sweeper
20   - end
21   - end
22   - end
23   -end
24   -
25   -UserStamp.creator_attribute = :creator_id
26   -UserStamp.updater_attribute = :updater_id
27   -UserStamp.current_user_method = :current_user
28   -
29   -class UserStampSweeper < ActionController::Caching::Sweeper
30   - def before_validation(record)
31   - return unless current_user
32   -
33   - attribute, method = UserStamp.creator_attribute, UserStamp.creator_assignment_method
34   - if record.respond_to?(method) && record.new_record?
35   - record.send(method, current_user) unless record.send("#{attribute}_id_changed?") || record.send("#{attribute}_type_changed?")
36   - end
37   -
38   - attribute, method = UserStamp.updater_attribute, UserStamp.updater_assignment_method
39   - if record.respond_to?(method)
40   - record.send(method, current_user) if record.send(attribute).blank?
41   - end
42   - end
43   -
44   - private
45   - def current_user
46   - if controller.respond_to?(UserStamp.current_user_method)
47   - controller.send UserStamp.current_user_method
48   - end
49   - end
50   -end
vendor/plugins/user_stamp/spec/spec_helper.rb
... ... @@ -1,24 +0,0 @@
1   -require 'rubygems'
2   -
3   -gem 'rspec'
4   -require 'spec'
5   -
6   -%w[activesupport activerecord actionpack].each do |lib|
7   - gem lib
8   - require lib
9   -end
10   -
11   -require 'action_controller'
12   -
13   -$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
14   -require 'user_stamp'
15   -
16   -UserStampSweeper.instance
17   -
18   -class User
19   - attr_accessor :id
20   -
21   - def initialize(id);
22   - @id = id
23   - end
24   -end
vendor/plugins/user_stamp/spec/user_stamp_class_methods_spec.rb
... ... @@ -1,51 +0,0 @@
1   -require File.dirname(__FILE__) + '/spec_helper'
2   -
3   -class FauxModelBase
4   - def self.add_observer(observer_instance); end
5   -end
6   -
7   -class Post < FauxModelBase; end
8   -class Category < FauxModelBase; end
9   -class Label < FauxModelBase; end
10   -
11   -class FauxApplicationController
12   - def self.cache_sweeper(sweepers); end
13   -
14   - def self.current_user
15   - User.new(220)
16   - end
17   -end
18   -
19   -class PostsController < FauxApplicationController
20   - extend UserStamp::ClassMethods
21   -end
22   -
23   -describe UserStamp::ClassMethods do
24   - before do
25   - UserStamp.creator_attribute = :creator_id
26   - UserStamp.updater_attribute = :updater_id
27   - UserStamp.current_user_method = :current_user
28   - end
29   -
30   - it "should add user_stamp method" do
31   - PostsController.respond_to?(:user_stamp).should == true
32   - end
33   -
34   - def user_stamp
35   - PostsController.user_stamp Post, Category, Label
36   - end
37   -
38   - describe "#user_stamp" do
39   - it "should add UserStampSweeper as observer for each model" do
40   - [Post, Category, Label].each do |klass|
41   - klass.should_receive(:add_observer).with(UserStampSweeper.instance).once
42   - end
43   - user_stamp
44   - end
45   -
46   - it "should setup cache sweeper for controller" do
47   - PostsController.should_receive(:cache_sweeper).with(:user_stamp_sweeper).once
48   - user_stamp
49   - end
50   - end
51   -end
52 0 \ No newline at end of file
vendor/plugins/user_stamp/spec/user_stamp_spec.rb
... ... @@ -1,51 +0,0 @@
1   -require File.dirname(__FILE__) + '/spec_helper'
2   -
3   -describe UserStamp do
4   - before do
5   - UserStamp.creator_attribute = :creator_id
6   - UserStamp.updater_attribute = :updater_id
7   - UserStamp.current_user_method = :current_user
8   - end
9   -
10   - it "should default creator_attribute to creator_id" do
11   - UserStamp.creator_attribute.should == :creator_id
12   - end
13   -
14   - it "should default updater_attribute to updater_id" do
15   - UserStamp.updater_attribute.should == :updater_id
16   - end
17   -
18   - it "should default current_user_method to current_user" do
19   - UserStamp.current_user_method.should == :current_user
20   - end
21   -
22   - it "should have accessor for creator_attribute" do
23   - UserStamp.creator_attribute = 'mofo_id'
24   - UserStamp.creator_attribute.should == 'mofo_id'
25   - end
26   -
27   - it "should have accessor for updater_attribute" do
28   - UserStamp.updater_attribute = 'mofo_id'
29   - UserStamp.updater_attribute.should == 'mofo_id'
30   - end
31   -
32   - it "should have accessor for current_user_method" do
33   - UserStamp.current_user_method = 'my_current_user'
34   - UserStamp.current_user_method.should == 'my_current_user'
35   - end
36   -
37   - describe "assignment methods" do
38   - before do
39   - UserStamp.creator_attribute = 'creator_mofo_id'
40   - UserStamp.updater_attribute = 'updater_mofo_id'
41   - end
42   -
43   - it "should include creator assignment method" do
44   - UserStamp.creator_assignment_method.should == 'creator_mofo_id='
45   - end
46   -
47   - it "should include updater assignment method" do
48   - UserStamp.updater_assignment_method.should == 'updater_mofo_id='
49   - end
50   - end
51   -end
52 0 \ No newline at end of file
vendor/plugins/user_stamp/spec/user_stamp_sweeper_spec.rb
... ... @@ -1,194 +0,0 @@
1   -require File.dirname(__FILE__) + '/spec_helper'
2   -
3   -class PostsController
4   - def self.current_user
5   - @@user
6   - end
7   -end
8   -
9   -describe UserStampSweeper, "#before_validation" do
10   - before do
11   - @@user = User.new(220)
12   - UserStamp.creator_attribute = :creator
13   - UserStamp.updater_attribute = :updater
14   - UserStamp.current_user_method = :current_user
15   - @sweeper = UserStampSweeper.instance
16   - @sweeper.stub!(:controller).and_return(PostsController)
17   - end
18   -
19   - describe "(with new record)" do
20   - it "should set creator if attribute exists" do
21   - 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)
22   - record.should_receive(:creator=).with(@@user).once
23   - @sweeper.before_validation(record)
24   - end
25   -
26   - it "should NOT set creator if attribute does not exist" do
27   - record = mock('Record', :new_record? => true, :updater= => nil, :respond_to? => false)
28   - record.should_receive(:respond_to?).with("creator=").and_return(false)
29   - record.should_not_receive(:creator=)
30   - @sweeper.before_validation(record)
31   - end
32   - end
33   -
34   - describe "(with non new record)" do
35   - it "should NOT set creator if attribute exists" do
36   - 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)
37   - record.should_not_receive(:creator=)
38   - @sweeper.before_validation(record)
39   - end
40   -
41   - it "should NOT set creator if attribute does not exist" do
42   - 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)
43   - record.should_not_receive(:creator=)
44   - @sweeper.before_validation(record)
45   - end
46   - end
47   -
48   - it "should set updater if attribute exists" do
49   - record = mock('Record', :creator= => nil, :updater= => nil, :new_record? => false, :updater => nil)
50   - record.should_receive(:updater=)
51   - @sweeper.before_validation(record)
52   - end
53   -
54   - it "should NOT set updater if attribute does not exist" do
55   - record = mock('Record', :creator= => nil, :updater= => nil, :new_record? => :false, :respond_to? => false)
56   - record.should_receive(:respond_to?).with("updater=").and_return(false)
57   - record.should_not_receive(:updater=)
58   - @sweeper.before_validation(record)
59   - end
60   -end
61   -
62   -describe UserStampSweeper, "#before_validation (with custom attribute names)" do
63   - before do
64   - UserStamp.creator_attribute = :created_by
65   - UserStamp.updater_attribute = :updated_by
66   - UserStamp.current_user_method = :current_user
67   - @sweeper = UserStampSweeper.instance
68   - @sweeper.stub!(:controller).and_return(PostsController)
69   - end
70   -
71   - describe "(with new record)" do
72   - it "should set created_by if attribute exists" do
73   - 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)
74   - record.should_receive(:created_by=).with(@@user).once
75   - @sweeper.before_validation(record)
76   - end
77   -
78   - it "should NOT set created_by if attribute does not exist" do
79   - record = mock('Record', :new_record? => true, :updated_by= => nil, :respond_to? => false)
80   - record.should_receive(:respond_to?).with("created_by=").and_return(false)
81   - record.should_not_receive(:created_by=)
82   - @sweeper.before_validation(record)
83   - end
84   - end
85   -
86   - describe "(with non new record)" do
87   - it "should NOT set created_by if attribute exists" do
88   - 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)
89   - record.should_not_receive(:created_by=)
90   - @sweeper.before_validation(record)
91   - end
92   -
93   - it "should NOT set created_by if attribute does not exist" do
94   - record = mock('Record', :updated_by= => nil, :updated_by => nil, :new_record? => false, :updated_by_id_changed? => false, :updated_by_type_changed? => false)
95   - record.should_not_receive(:created_by=)
96   - @sweeper.before_validation(record)
97   - end
98   - end
99   -
100   - it "should set updated_by if attribute exists" do
101   - 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)
102   - record.should_receive(:updated_by=)
103   - @sweeper.before_validation(record)
104   - end
105   -
106   - it "should NOT set updated_by if attribute does not exist" do
107   - record = mock('Record', :created_by= => nil, :updated_by= => nil, :new_record? => :false, :respond_to? => false)
108   - record.should_receive(:respond_to?).with("updated_by=").and_return(false)
109   - record.should_not_receive(:updated_by=)
110   - @sweeper.before_validation(record)
111   - end
112   -
113   - it "should NOT set created_by if attribute changed" do
114   - record = mock('Record', :created_by= => nil, :updated_by= => nil, :new_record? => true, :created_by_id_changed? => true, :created_by_type_changed? => true)
115   - record.should_receive(:respond_to?).with("updated_by=").and_return(false)
116   - record.should_receive(:respond_to?).with("created_by=").and_return(true)
117   - record.should_not_receive(:created_by=)
118   - @sweeper.before_validation(record)
119   - end
120   -
121   - it "should NOT set updated_by if attribute is not nil" do
122   - record = mock('Record', :created_by= => nil, :updated_by= => nil, :updated_by => 1, :new_record? => false)
123   - record.should_receive(:respond_to?).with("updated_by=").and_return(true)
124   - record.should_receive(:respond_to?).with("created_by=").and_return(false)
125   - record.should_not_receive(:updated_by=)
126   - @sweeper.before_validation(record)
127   - end
128   -
129   - it "should set created_by if attribute has not changed" do
130   - record = mock('Record', :created_by= => nil, :updated_by= => nil, :new_record? => true, :created_by_id_changed? => false, :created_by_type_changed? => false)
131   - record.should_receive(:respond_to?).with("updated_by=").and_return(false)
132   - record.should_receive(:respond_to?).with("created_by=").and_return(true)
133   - record.should_receive(:created_by=)
134   - @sweeper.before_validation(record)
135   - end
136   -
137   - it "should set updated_by if attribute is nil" do
138   - record = mock('Record', :created_by= => nil, :updated_by= => nil, :updated_by => nil, :new_record? => false)
139   - record.should_receive(:respond_to?).with("updated_by=").and_return(true)
140   - record.should_receive(:respond_to?).with("created_by=").and_return(false)
141   - record.should_receive(:updated_by=)
142   - @sweeper.before_validation(record)
143   - end
144   -end
145   -
146   -describe UserStampSweeper, "#current_user" do
147   - before do
148   - UserStamp.creator_attribute = :creator
149   - UserStamp.updater_attribute = :updater
150   - UserStamp.current_user_method = :current_user
151   - @sweeper = UserStampSweeper.instance
152   - end
153   -
154   - it "should send current_user if controller responds to it" do
155   - user = mock('User')
156   - controller = mock('Controller', :current_user => user)
157   - @sweeper.stub!(:controller).and_return(controller)
158   - controller.should_receive(:current_user)
159   - @sweeper.send(:current_user)
160   - end
161   -
162   - it "should not send current_user if controller does not respond to it" do
163   - user = mock('User')
164   - controller = mock('Controller', :respond_to? => false)
165   - @sweeper.stub!(:controller).and_return(controller)
166   - controller.should_not_receive(:current_user)
167   - @sweeper.send(:current_user)
168   - end
169   -end
170   -
171   -describe UserStampSweeper, "#current_user (with custom current_user_method)" do
172   - before do
173   - UserStamp.creator_attribute = :creator
174   - UserStamp.updater_attribute = :updater
175   - UserStamp.current_user_method = :my_user
176   - @sweeper = UserStampSweeper.instance
177   - end
178   -
179   - it "should send current_user if controller responds to it" do
180   - user = mock('User')
181   - controller = mock('Controller', :my_user => user)
182   - @sweeper.stub!(:controller).and_return(controller)
183   - controller.should_receive(:my_user)
184   - @sweeper.send(:current_user)
185   - end
186   -
187   - it "should not send current_user if controller does not respond to it" do
188   - user = mock('User')
189   - controller = mock('Controller', :respond_to? => false)
190   - @sweeper.stub!(:controller).and_return(controller)
191   - controller.should_not_receive(:my_user)
192   - @sweeper.send(:current_user)
193   - end
194   -end