Skip to content
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

Merged
merged 1 commit into from
Jun 24, 2018

Conversation

fatkodima
Copy link
Contributor

enum status: { new_status: "New", active: "In progress", complete: "Complete" }

Currently, when record is saved or destroyed, their audits contain enum values ("New", "In progress" or "Complete"). So I fixed "update" audits to store enum values as well for consistency. Also storing enum values (but not keys) allows to easily change enum key namings in future without changing existing audits.

Fixes #202
Fixes #214
Fixes #412

end

def normalize_enum_changes(changes)
changes.each do |key, value|
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of looping over all the changes, how about looping on the definded enums? in most cases there would be 0-2 enums while changes could be many more.

end

filtered_changes = normalize_enum_changes(filtered_changes)
filtered_changes.to_hash
Copy link
Collaborator

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?

Copy link
Contributor Author

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)

Copy link

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 call to_hash on it, it's not anymore, it's simply a Hash 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 as ActiveSupport::HashWithIndifferentAccess anymore. Was this intended?

@fatkodima
Copy link
Contributor Author

Updated.

Copy link
Collaborator

@tbrisker tbrisker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @fatkodima, could you please also add a test for create and destroy audits to make sure the audited_changes are still stored correctly, since those audits don't use the audited_changes method?

@@ -290,7 +290,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' )
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be status: :active to ensure that is converted correctly?

@fatkodima fatkodima force-pushed the fix-enums branch 2 times, most recently from 40683f0 to 072ec46 Compare April 10, 2018 10:47
@fatkodima
Copy link
Contributor Author

@tbrisker done

if changes[name].is_a?(Array)
changes[name].map { |v| values[v] }
elsif rails_below?('5.0')
changes[name]
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

@tbrisker tbrisker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @fatkodima !

@tbrisker tbrisker merged commit fab1170 into collectiveidea:master Jun 24, 2018
@travisofthenorth
Copy link

@tbrisker any idea when this will be in a published version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Strange new entry in audited_changes column in 4.6.0 [More to a Upgrade] "enum" fields Enum field change
4 participants