module Symphonia module ModelFilters class Base attr_reader :name, :type, :options, :query, :value, :operator # @param [Symphonia::ModelAttributes::Attribute] attribute # @param [Symphonia::Query] query # @param [Hash] options def initialize(attribute, query, options = {}) if attribute.is_a? String ActiveSupport::Deprecation.warn("name as argument is no longer supported - use Attribute") @name = attribute else @name = attribute.name @attribute = attribute end @options = options @query = query @operator = 'eq' end def caption @caption = @options[:caption] @caption ||= query.model.human_attribute_name(name) end def form_field(_context) raise NotImplementError end def inspect "#<#{self.class.name} name='#{name}' caption='#{caption}' options=#{@options.inspect}>" end def active? !!query.active_filters[name] end def value=(to) @value = if (m = to.downcase.match(/^([!~><]+)(.*)/)) self.operator = m[1] m[2] else @operator ||= 'eq' to end end def operator=(to) @operator = case to when '!' 'not_eq' when '~' @value = "%#{@value}%" 'matches' when '!~' @value = "%#{@value}%" 'not_matches' when '>' 'lt' when '<' 'gt' else 'eq' end end def apply(_scope) Rails.logger.debug("Apply filter #{name} '#{operator}' #{value}") end private def form_field_name name end end end end