Question

enumerations_mixin gem depends on deprecated method

here's the guilty line

what would be correct approach to patch it?

Was it helpful?

Solution

Quite a lot of patching I am afraid. This method has been replaced by class_attribute, however it works slightly different. Previously it was enough to use write_inheritable_attribute to create new class param, now you need to declare it first and then assign value.

On line 17 it is using 'write_inheritable_attribute` to set those values. It should now read

class_attribute :"acts_enumerated_#{key}" unless respond_to? "acts_enumerated_#{key}"
self.send(:"acts_enumerated_#{key}=", options[key])

Then, everywhere it is using read_inheritable_attribute(:attribute_name) just use self.attribute_name.

The only problem with this is that 'read_inheritable_attribute` returned nil if attribute is not set and the approach above will throw an error. You will notice that all read methods has default value, like (line 56):

read_inheritable_attribute(:acts_enumerated_on_lookup_failure) || :enforce_strict_literals

You will need to look for all those defaults and enforce them within acts as enumerated method:

def acts_as_enumerated(options = {})
      valid_keys = [:conditions, :order, :on_lookup_failure]
      default_options = {<all the default values from the code>}
      options = default_options.merge options
      options.assert_valid_keys(*valid_keys)
      valid_keys.each do |key|
        write_inheritable_attribute("acts_enumerated_#{key.to_s}".to_sym, options[key]) if options.has_key? key
      end

However this is not a perfect design. I would probably define class_attribute enumerated_options within append_features method, put all the options there as a hash instead of creating class_attribute for each option. This is absolutely up to you though.

Also please notice that this gem has been written over 4 years ago, and this method might be not the only deprecated one. I am not entirely sure what this gem is supposed to do, but it might be easier to rather implement what you need rather than to use it.

OTHER TIPS

Clone the 's repo locally and use it in your project as a path to a local gem. So specify in your :

gem :enumerations_mixin, :path => '/local/path/to/gem'

When you will have the gem patched, fork it on , replace gems origin in /local/path/to/gem/.git/config, push your changes to your fork, and replace that line in your to the follownig:

gem :enumerations_mixin, :github => 'your_acoount/enumerations_mixin'

Issue the to the root repo of the gem, and when the request will be accepted, replace that line in your to the follownig:

gem :enumerations_mixin, :github => 'protocool/enumerations_mixin'

When the gem will have been released the line can be replaced to:

gem :enumerations_mixin, '~> <new_verison>'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top