diff --git a/lib/audited/audit.rb b/lib/audited/audit.rb index 4d96d02c1..abfe4acfe 100644 --- a/lib/audited/audit.rb +++ b/lib/audited/audit.rb @@ -72,7 +72,7 @@ def revision # Returns a hash of the changed attributes with the new values def new_attributes (audited_changes || {}).inject({}.with_indifferent_access) do |attrs, (attr, values)| - attrs[attr] = values.is_a?(Array) ? values.last : values + attrs[attr] = self.action == 'update' ? values.last : values attrs end end @@ -80,7 +80,7 @@ def new_attributes # Returns a hash of the changed attributes with the old values def old_attributes (audited_changes || {}).inject({}.with_indifferent_access) do |attrs, (attr, values)| - attrs[attr] = Array(values).first + attrs[attr] = self.action == 'update' ? values.first : values attrs end diff --git a/lib/audited/auditor.rb b/lib/audited/auditor.rb index 716cefe5f..d8c940972 100644 --- a/lib/audited/auditor.rb +++ b/lib/audited/auditor.rb @@ -115,7 +115,7 @@ def without_auditing(&block) def revisions(from_version = 1) return [] unless audits.from_version(from_version).exists? - all_audits = audits.select([:audited_changes, :version]).to_a + all_audits = audits.select([:audited_changes, :version, :action]).to_a targeted_audits = all_audits.select { |audit| audit.version >= from_version } previous_attributes = reconstruct_attributes(all_audits - targeted_audits) diff --git a/spec/audited/audit_spec.rb b/spec/audited/audit_spec.rb index 3260072eb..b5a6bb66a 100644 --- a/spec/audited/audit_spec.rb +++ b/spec/audited/audit_spec.rb @@ -188,15 +188,20 @@ class Models::ActiveRecord::CustomUserSubclass < Models::ActiveRecord::CustomUse describe "new_attributes" do it "should return a hash of the new values" do - new_attributes = Audited::Audit.new(audited_changes: {a: [1, 2], b: [3, 4]}).new_attributes + new_attributes = Audited::Audit.new(audited_changes: {a: [1, 2], b: [3, 4]}, action: 'update').new_attributes expect(new_attributes).to eq({"a" => 2, "b" => 4}) end end describe "old_attributes" do it "should return a hash of the old values" do - old_attributes = Audited::Audit.new(audited_changes: {a: [1, 2], b: [3, 4]}).old_attributes - expect(old_attributes).to eq({"a" => 1, "b" => 3}) + old_attributes = Audited::Audit.new(audited_changes: {a: [1, 2], b: [[3, 4], [4, 5]] }, action: 'update').old_attributes + expect(old_attributes).to eq({"a" => 1, "b" => [3, 4]}) + end + + it "should return a hash with original (current) values" do + old_attributes = Audited::Audit.new(audited_changes: {a: 1, b: [3, 4] }, action: 'create').old_attributes + expect(old_attributes).to eq({"a" => 1, "b" => [3, 4]}) end end diff --git a/spec/audited/auditor_spec.rb b/spec/audited/auditor_spec.rb index f6c4543d7..8e16d3f2e 100644 --- a/spec/audited/auditor_spec.rb +++ b/spec/audited/auditor_spec.rb @@ -267,7 +267,8 @@ def non_column_attr=(val) end it "should store all the audited attributes" do - expect(user.audits.first.audited_changes).to eq(user.audited_attributes) + changes = user.audits.first.audited_changes + expect(changes).to eq(user.audited_attributes) end it "should store comment" do @@ -675,6 +676,12 @@ def stub_global_max_audits(max_audits) it "should be nil if given a time before audits" do expect(user.revision_at( 1.week.ago )).to be_nil end + + if ENV['DB'] == 'POSTGRES' + it "should store the json array successfully" do + expect(user.revision_at(DateTime.now).favourite_colours).to eq(user.favourite_colours) + end + end end describe "without auditing" do diff --git a/spec/audited_spec_helpers.rb b/spec/audited_spec_helpers.rb index 105c5fbf1..1f1f2eb34 100644 --- a/spec/audited_spec_helpers.rb +++ b/spec/audited_spec_helpers.rb @@ -1,7 +1,12 @@ module AuditedSpecHelpers def create_user(attrs = {}) - Models::ActiveRecord::User.create({name: 'Brandon', username: 'brandon', password: 'password', favourite_device: 'Android Phone'}.merge(attrs)) + attrs = {name: 'Brandon', username: 'brandon', password: 'password', favourite_device: 'Android Phone'}.merge(attrs) + if ENV['DB'] == 'POSTGRES' + attrs = attrs.merge({favourite_colours: ['sarcoline', 'coquelicot']}) + end + + Models::ActiveRecord::User.create(attrs) end def build_user(attrs = {}) diff --git a/spec/support/active_record/schema.rb b/spec/support/active_record/schema.rb index f340ff0c4..98fd82ec1 100644 --- a/spec/support/active_record/schema.rb +++ b/spec/support/active_record/schema.rb @@ -40,6 +40,10 @@ t.column :created_at, :datetime t.column :updated_at, :datetime t.column :favourite_device, :string + + if ENV['DB'] == 'POSTGRES' + t.column :favourite_colours, :json + end end create_table :companies do |t|