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

save object_changes on create #172

Merged
merged 1 commit into from Oct 16, 2012
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
12 changes: 11 additions & 1 deletion lib/paper_trail/has_paper_trail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,17 @@ def source_version

def record_create
if switched_on?
send(self.class.versions_association_name).create merge_metadata(:event => 'create', :whodunnit => PaperTrail.whodunnit)
data = {
:event => 'create',
:whodunnit => PaperTrail.whodunnit
}

if changed_notably? and version_class.column_names.include?('object_changes')
# The double negative (reject, !include?) preserves the hash structure of self.changes.
data[:object_changes] = self.changes.reject { |k, _| !notably_changed.include?(k) }.to_yaml
end

send(self.class.versions_association_name).create merge_metadata(data)
end
end

Expand Down
11 changes: 9 additions & 2 deletions test/unit/model_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,15 @@ class HasPaperTrailModelTest < ActiveSupport::TestCase
assert @widget.live?
end

should 'not have changes' do
assert_equal Hash.new, @widget.versions.last.changeset
should 'have changes' do
changes = {
'name' => [nil, 'Henry'],
'created_at' => [nil, @widget.created_at],
'updated_at' => [nil, @widget.updated_at],
'id' => [nil, 1]
}

assert_equal changes, @widget.versions.last.changeset
end

context 'and then updated without any changes' do
Expand Down