Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

update updater_id on destroy as well

  • Loading branch information...
commit d963fce6e5cdac9924855dd46155aa0125650533 1 parent 65e2719
@atog atog authored
View
8 lib/user_stamp.rb
@@ -27,6 +27,14 @@ def user_stamp(*models)
UserStamp.current_user_method = :current_user
class UserStampSweeper < ActionController::Caching::Sweeper
+ def before_destroy(record)
+ return unless current_user
+
+ if record.respond_to?(UserStamp.updater_assignment_method)
+ record.send(UserStamp.updater_assignment_method, current_user.id)
+ end
+ end
+
def before_validation(record)
return unless current_user
View
1  spec/spec_helper.rb
@@ -9,6 +9,7 @@
end
require 'action_controller'
+require 'active_record/observer'
$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'user_stamp'
View
26 spec/user_stamp_sweeper_spec.rb
@@ -6,6 +6,32 @@ def self.current_user
end
end
+
+describe UserStampSweeper, "#before_destroy" do
+ before do
+ UserStamp.creator_attribute = :creator_id
+ UserStamp.updater_attribute = :updater_id
+ UserStamp.current_user_method = :current_user
+ @sweeper = UserStampSweeper.instance
+ @sweeper.stub!(:controller).and_return(PostsController)
+ 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=)
+ @sweeper.before_destroy(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=)
+ @sweeper.before_destroy(record)
+ end
+end
+
+
describe UserStampSweeper, "#before_validation" do
before do
UserStamp.creator_attribute = :creator_id
Please sign in to comment.
Something went wrong with that request. Please try again.