Skip to content

Commit

Permalink
Added support for attributes to be arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderweiss authored and Robert Bakker committed Mar 24, 2020
1 parent b3a314a commit 8448261
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/audited/audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ 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

# 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
Expand Down
2 changes: 1 addition & 1 deletion lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 8 additions & 3 deletions spec/audited/audit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 8 additions & 1 deletion spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion spec/audited_spec_helpers.rb
Original file line number Diff line number Diff line change
@@ -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 = {})
Expand Down
4 changes: 4 additions & 0 deletions spec/support/active_record/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down

0 comments on commit 8448261

Please sign in to comment.