Skip to content

Commit

Permalink
Ensure enum changes are stored consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Apr 4, 2018
1 parent fd95166 commit 84d509a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ Changed

Fixed

- None
- Ensure enum changes are stored consistently
[#429](https://github.com/collectiveidea/audited/pull/429)

## 4.7.0 (2018-03-14)

Expand Down
20 changes: 16 additions & 4 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,22 @@ def revision_with(attributes)

def audited_changes
all_changes = respond_to?(:changes_to_save) ? changes_to_save : changes
if audited_options[:only].present?
all_changes.slice(*self.class.audited_columns)
else
all_changes.except(*self.class.non_audited_columns)
filtered_changes = \
if audited_options[:only].present?
all_changes.slice(*self.class.audited_columns)
else
all_changes.except(*self.class.non_audited_columns)
end

filtered_changes = normalize_enum_changes(filtered_changes)
filtered_changes.to_hash
end

def normalize_enum_changes(changes)
changes.each do |key, value|
if enum = self.class.defined_enums[key]
changes[key] = value.map { |v| enum[v] }
end
end
end

Expand Down
17 changes: 16 additions & 1 deletion spec/audited/auditor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def non_column_attr=(val)

describe "on update" do
before do
@user = create_user( name: 'Brandon', audit_comment: 'Update' )
@user = create_user( name: 'Brandon', status: 0, audit_comment: 'Update' )
end

it "should save an audit" do
Expand All @@ -312,6 +312,11 @@ def non_column_attr=(val)
expect(@user.audits.last.audited_changes).to eq({ 'name' => ['Brandon', 'Changed'] })
end

it "should store changed enum as integers" do
@user.update_attributes status: 1
expect(@user.audits.last.audited_changes["status"]).to eq([0, 1])
end

it "should store audit comment" do
expect(@user.audits.last.comment).to eq('Update')
end
Expand Down Expand Up @@ -628,6 +633,16 @@ def stub_global_max_audits(max_audits)
expect(u.revision(1).username).to eq('brandon')
end

it "should correctly restore revision with enum" do
u = Models::ActiveRecord::User.create(status: :active)
u.update_attribute(:status, :reliable)
u.update_attribute(:status, :banned)

expect(u.revision(3)).to be_banned
expect(u.revision(2)).to be_reliable
expect(u.revision(1)).to be_active
end

it "should be able to get time for first revision" do
suspended_at = Time.zone.now
u = Models::ActiveRecord::User.create(suspended_at: suspended_at)
Expand Down
1 change: 1 addition & 0 deletions spec/support/active_record/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class User < ::ActiveRecord::Base
audited except: :password
attribute :non_column_attr if Rails.version >= '5.1'
attr_protected :logins if respond_to?(:attr_protected)
enum status: { active: 0, reliable: 1, banned: 2 }

def name=(val)
write_attribute(:name, CGI.escapeHTML(val))
Expand Down
1 change: 1 addition & 0 deletions spec/support/active_record/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
t.column :username, :string
t.column :password, :string
t.column :activated, :boolean
t.column :status, :integer, default: 0
t.column :suspended_at, :datetime
t.column :logins, :integer, default: 0
t.column :created_at, :datetime
Expand Down

0 comments on commit 84d509a

Please sign in to comment.