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

fix revision access failing when limit is enabled #639

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions lib/audited/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,14 @@ def revision
# Returns a hash of the changed attributes with the new values
def new_attributes
(audited_changes || {}).each_with_object({}.with_indifferent_access) do |(attr, values), attrs|
attrs[attr] = (action == "update" ? values.last : values)
attrs[attr] = (values.is_a?(Array) ? values.last : values)
end
end

# Returns a hash of the changed attributes with the old values
def old_attributes
(audited_changes || {}).each_with_object({}.with_indifferent_access) do |(attr, values), attrs|
attrs[attr] = (action == "update" ? values.first : values)
attrs[attr] = (values.is_a?(Array) ? values.first : values)
end
end

Expand Down
24 changes: 24 additions & 0 deletions spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,30 @@ def non_column_attr=(val)
end
end

it "should combine field that has never changed" do
stub_global_max_audits(2) do
user = Models::ActiveRecord::User.create!(name: "Brandon", username: "brandon")
user.update!(username: "keepers")
user.update!(activated: true)
audits = user.audits

expect(audits.count).to eq(2)
expect(audits[0].audited_changes).to include({"name" => "Brandon", "username" => ["brandon", "keepers"]})
expect(audits[1].audited_changes).to eq({"activated" => [nil, true]})
end
end

it "should be able to access revisions if field never changed" do
stub_global_max_audits(2) do
user = Models::ActiveRecord::User.create!(name: "Brandon", username: "brandon")
user.update!(username: "keepers")
user.update!(activated: true)
revisions = user.revisions

expect(revisions[0].attributes).to include({"name" => "Brandon", "username" => "keepers"})
end
end

it "should add comment line for combined audit" do
stub_global_max_audits(2) do
user = Models::ActiveRecord::User.create!(name: "Foobar 1")
Expand Down