Skip to content

Commit

Permalink
close #384; Fix originator method so it doesn't return inappropriate …
Browse files Browse the repository at this point in the history
…cached values or inaccurate values for reified model isntances
  • Loading branch information
batter committed Jun 17, 2014
1 parent 51de996 commit e638392
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 3.0.3 (Unreleased)

- [#384](/~https://github.com/airblade/paper_trail/issues/384) - Fix `VersionConcern#originator` instance method.
- [#373](/~https://github.com/airblade/paper_trail/pull/373) - Fix default sort order for the `versions` association in Rails `4.1`
- [#372](/~https://github.com/airblade/paper_trail/pull/372) - Use [Arel](/~https://github.com/rails/arel) for SQL construction.
- [#347](/~https://github.com/airblade/paper_trail/pull/347) - Autoload `ActiveRecord` models in via a `Rails::Engine` when
Expand Down
5 changes: 3 additions & 2 deletions lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,8 @@ def live?

# Returns who put the object into its current state.
def originator
@originator ||= self.class.paper_trail_version_class.with_item_keys(self.class.base_class.name, id).last.try :whodunnit
return source_version.whodunnit if source_version
send(self.class.versions_association_name).last.try(:whodunnit)
end

# Returns the object (not a Version) as it was at the given timestamp.
Expand Down Expand Up @@ -290,7 +291,7 @@ def changes_for_paper_trail
end.tap { |changes| self.class.serialize_attribute_changes(changes) }
end

# Invoked via `after_update` callback for when a previous version is reified and then saved
# Invoked via`after_update` callback for when a previous version is reified and then saved
def clear_version_instance!
send("#{self.class.version_association_name}=", nil)
end
Expand Down
33 changes: 33 additions & 0 deletions spec/models/widget_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,39 @@

describe "Methods" do
describe "Instance", :versioning => true do
describe :originator do
it { should respond_to(:originator) }

describe "return value" do
let(:orig_name) { Faker::Name.name }
let(:new_name) { Faker::Name.name }
before { PaperTrail.whodunnit = orig_name }

context "accessed from live model instance" do
specify { widget.should be_live }

it "should return the originator for the model at a given state" do
widget.originator.should == orig_name
widget.whodunnit(new_name) { |w| w.update_attributes(:name => 'Elizabeth') }
widget.originator.should == new_name
end
end

context "accessed from a reified model instance" do
before do
widget.update_attributes(:name => 'Andy')
PaperTrail.whodunnit = new_name
widget.update_attributes(:name => 'Elizabeth')
end
let(:reified_widget) { widget.versions[1].reify }

it "should return the appropriate originator" do
reified_widget.originator.should == orig_name
end
end
end
end

describe :whodunnit do
it { should respond_to(:whodunnit) }

Expand Down

0 comments on commit e638392

Please sign in to comment.