Module: LoyalCore::ActsAsNamedFilterAble::ClassMethods

Defined in:
lib/loyal_core/acts/named_filter_able.rb

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#loyal_core_acts_as_named_filter_able(*args) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/loyal_core/acts/named_filter_able.rb', line 11

def loyal_core_acts_as_named_filter_able *args
  options = args.extract_options!

  field_name = args.first
  config = options[:config] || ::LoyalCore::ConfigUtil.new

  # color_named_filter_config
  self.class.send :define_method, :"#{field_name}_named_filter_config" do
    config
  end

  # filter_by_named_color
  scope :"filter_by_named_#{field_name}", ->(*names) do
    where :"#{field_name}" => config.values_at(*names)
  end

  # filter_named_color
  define_method :"filter_named_#{field_name}" do
    config.item(self.send field_name)
  end

  include InstanceMethods
end