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 Mar 31, 2018
1 parent c48893a commit 5424021
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,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 @@ -201,10 +201,22 @@ def rails_below?(rails_version)

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

filtered_changes = normalize_enum_changes(filtered_changes) unless rails_below?('4.1')
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
19 changes: 18 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,12 @@ 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
skip if rails_below?("4.1")
@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 +634,17 @@ def stub_global_max_audits(max_audits)
expect(u.revision(1).username).to eq('brandon')
end

it "should correctly restore revision with enum" do
skip if rails_below?("4.1")
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 } if Rails.version >= '4.1'

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 5424021

Please sign in to comment.