diff --git a/lib/audited/audit.rb b/lib/audited/audit.rb index eadd0fb0..a26411fa 100644 --- a/lib/audited/audit.rb +++ b/lib/audited/audit.rb @@ -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 diff --git a/spec/audited/auditor_spec.rb b/spec/audited/auditor_spec.rb index 08407d1b..df09852a 100644 --- a/spec/audited/auditor_spec.rb +++ b/spec/audited/auditor_spec.rb @@ -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")