Sha256: f8b6c01d95b17c574db64ce59e095533ade9d67b06f8339e9f93c41fcb634578

Contents?: true

Size: 757 Bytes

Versions: 2

Compression:

Stored size: 757 Bytes

Contents

module FlexibleEnum
  class ScopeConfigurator < AbstractConfigurator
    def apply
      configurator = self

      elements.each do |element_name, element_config|
        add_class_method(scope_name(element_name)) do
          where(configurator.attribute_name => element_config[:value])
        end

        if element_config[:inverse]
          add_class_method(scope_name(element_config[:inverse])) do
            where.not(configurator.attribute_name => element_config[:value])
          end
        end
      end
    end

    private

    def scope_name(option)
      if feature_module == module_for_elements
        option
      else
        "#{module_for_elements.to_s.split('::').last.underscore.singularize}_#{option}"
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flexible_enum-0.4.2 lib/flexible_enum/scope_configurator.rb
flexible_enum-0.4.1 lib/flexible_enum/scope_configurator.rb