Class: Generators::Custom

Inherits:
Base
  • Object
show all
Defined in:
lib/toschas/filterable/generators/custom.rb

Instance Attribute Summary

Attributes inherited from Base

#filters, #model, #options

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Generators::Base

Instance Method Details

#custom_prefixesObject



16
17
18
19
# File 'lib/toschas/filterable/generators/custom.rb', line 16

def custom_prefixes
  prefix = options[:prefix].blank? ? 'by' : options[:prefix]
  ensure_prefix_array(prefix)
end

#ensure_prefix_array(prefix) ⇒ Object



21
22
23
# File 'lib/toschas/filterable/generators/custom.rb', line 21

def ensure_prefix_array(prefix)
  prefix.is_a?(Array) ? prefix.reject(&:blank?) : [prefix]
end

#generateObject



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/toschas/filterable/generators/custom.rb', line 3

def generate
  prefixes = custom_prefixes
  filters.each do |filter|
    prefixes.each do |prefix|
      filter_name = prefix == :none ? "#{filter}" : "#{prefix}_#{filter}"
      model.define_singleton_method(
        filter_name, 
        ->(_value) { send(:where, nil) }
      )
    end
  end
end