Skip to content

Commit

Permalink
update updater_id on destroy as well
Browse files Browse the repository at this point in the history
  • Loading branch information
atog committed Oct 28, 2009
1 parent 65e2719 commit d963fce
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/user_stamp.rb
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -9,6 +9,7 @@
end

require 'action_controller'
require 'active_record/observer'

$:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
require 'user_stamp'
Expand Down
26 changes: 26 additions & 0 deletions spec/user_stamp_sweeper_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit d963fce

Please sign in to comment.