Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't try to write timestamp attributes just because the method exists; only write it if it is a real attribute. #280

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def item_before_change
previous = self.dup
# `dup` clears timestamps so we add them back.
all_timestamp_attributes.each do |column|
previous[column] = send(column) if respond_to?(column) && !send(column).nil?
previous[column] = send(column) if attributes.has_key?(column.to_s) && !send(column).nil?
end
previous.tap do |prev|
prev.id = id
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/models/wotsit.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class Wotsit < ActiveRecord::Base
has_paper_trail
belongs_to :widget

def created_on
created_at.to_date
end
end
25 changes: 25 additions & 0 deletions test/unit/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,31 @@ def without(&block)
end


context 'Timestamps' do
setup do
@wotsit = Wotsit.create! :name => 'wotsit'
end

should 'record timestamps' do
@wotsit.update_attributes! :name => 'changed'
assert_not_nil @wotsit.versions.last.reify.created_at
assert_not_nil @wotsit.versions.last.reify.updated_at
end

should 'not generate warning' do
# Tests that it doesn't try to write created_on as an attribute just because a created_on
# method exists.
warnings = capture(:stderr) { # Deprecation warning in Rails 3.2
assert_nothing_raised { # ActiveModel::MissingAttributeError in Rails 4
@wotsit.update_attributes! :name => 'changed'
}
}
assert_equal '', warnings
end

end


context 'A subclass' do
setup do
@foo = FooWidget.create
Expand Down