I am using Ruby on Rails v3.2.2. In a module I am trying to "dynamically" open a class so to add to it a Ruby on Rails "scope method" that makes use of a local variable, this way:
module MyModule
extend ActiveSupport::Concern
included do
# Note: The `CLASS_NAME` is not the class where `MyModule` is included. That
# is, for instance, if the including class of `MyModule` is `Article` then
# the `CLASS_NAME` is `User`.
CLASS_NAME = self.get_class_name.constantize # => User
counter_cache_column = self.get_counter_cache # => "counter_count"
class CLASS_NAME
def self.order_by_counter
order("#{counter_cache_column} DESC")
end
end
end
end
If I run the above code, I get the following error:
NameError
undefined local variable or method `counter_cache_column' for #<Class:0x0000010775c558>
It happens because the counter_cache_column
in not called in the context of the module. How should I properly state the order_by_counter
scope method?
Bonus: What do you advice about the above "so dynamic" implementation?