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

Pluralized enum raises error when attempting to modify #34517

merged 1 commit into from Nov 24, 2018


Copy link

@EByrdS EByrdS commented Nov 23, 2018


Fixes #34514

The pluralized method created to access each enum now raises an error when attempting to modify the original.

Problem being addresed:

expected_copy = Model.states # {'foo'=>0, 'bar'=>1, 'baz'=>2}

# All the application would be silently affected with the following lines
expected_copy.delete('foo') # previously deleted foo from Model.states
expected_copy['bad_key'] = 10 # previously added a new key on Model.states
@@ -157,7 +157,7 @@ def enum(definitions)

# def self.statuses() statuses end
detect_enum_conflict!(name, name.pluralize, true)
singleton_class.send(:define_method, name.pluralize) { enum_values }
singleton_class.send(:define_method, name.pluralize) { enum_values.dup }
Copy link

@composerinteralia composerinteralia Nov 23, 2018

What do you think about freezing enum_values instead? That way we would get an error if we try to mutate the values, rather than failing silently.

We would have to do it after all the values are set:

enum_values[label] = value

Copy link
Contributor Author

@EByrdS EByrdS Nov 23, 2018

It seems a smarter way to fix it, will push the change.

@EByrdS EByrdS force-pushed the immutable-enum branch from 058b31c to 4c8c317 Nov 23, 2018
@EByrdS EByrdS changed the title Pluralized enum method returns copy of enum_values Pluralized enum raises error when attempting to modify Nov 23, 2018
@rafaelfranca rafaelfranca merged commit 171e32f into rails:master Nov 24, 2018
2 checks passed
rafaelfranca added a commit that referenced this issue Nov 24, 2018
Pluralized enum raises error when attempting to modify
@EByrdS EByrdS deleted the immutable-enum branch Nov 24, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants