-
Notifications
You must be signed in to change notification settings - Fork 679
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure enum changes are stored consistently #429
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,7 +142,8 @@ def revision_at(date_or_time) | |
|
||
# List of attributes that are audited. | ||
def audited_attributes | ||
attributes.except(*self.class.non_audited_columns) | ||
audited_attributes = attributes.except(*self.class.non_audited_columns) | ||
normalize_enum_changes(audited_attributes) | ||
end | ||
|
||
# Returns a list combined of record audits and associated audits. | ||
|
@@ -196,11 +197,35 @@ 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) | ||
self.class.defined_enums.each do |name, values| | ||
if changes.has_key?(name) | ||
changes[name] = \ | ||
if changes[name].is_a?(Array) | ||
changes[name].map { |v| values[v] } | ||
elsif rails_below?('5.0') | ||
changes[name] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why for rails<5 this is a noop but only if the change isn't an array? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is due to the fact, that rails stores enum changes differently for rails < 5 and for rails >= 5. |
||
else | ||
values[changes[name]] | ||
end | ||
end | ||
end | ||
changes | ||
end | ||
|
||
def rails_below?(rails_version) | ||
Gem::Version.new(Rails::VERSION::STRING) < Gem::Version.new(rails_version) | ||
end | ||
|
||
def audits_to(version = nil) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the
to_hash
? shouldn't this already be a hash?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a hash-like object (
ActiveSupport::HashWithIndifferentAccess
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fatkodima a little late to the party, but this object indeed was an
ActiveSupport::HashWithIndifferentAccess
, but after you callto_hash
on it, it's not anymore, it's simply aHash
object. The reason I'm writing this is that after upgrading from 4.8.0 to 4.9.0 I noticed that i cannot access some audited data by simbol, because they are not stored asActiveSupport::HashWithIndifferentAccess
anymore. Was this intended?