I have a model class with attr_reader - type_slugs (returns an array of string) which I'd like to use to generate several methods.
For example:
module Orders
module Extensions
module Order
module TypeCheckable
extend ::ActiveSupport::Concern
::Orders::Config.type_slugs.each do |type|
define_method("#{type}_order_type?") { order_type == type }
end
end
end
end
end
But it gives me NoMethodError: undefined method `type_slugs' for Orders::Config:Module
Same error happens if I use any model method like:
some_method_defined_in_model.each do |type|
define_method("#{type}_order_type?") { order_type == type }
end
But it works just fine if I use a word array
%w(message product another).each do |type|
define_method("#{type}_order_type?") { order_type == type }
end
Is it even possible to use Orders::Config.type_slugs in this case? If not, why so?
From the information you provided, it follows that the module
Orders::Configdoes not havetype_slugsmethodIf you want your code works, you need define it, something like this:
Or if that list doesn't change dynamically:
In that case change
::Orders::Config.type_slugsto::Orders::Config::TYPE_SLUGSWhen use Rails concerns, you should use
includedfor instance methodsAfter that you can use it in your models
And call methods like this