Sha256: 553d914b3a6eb04acc1a875f4a0ff380e775661b6616705cf5cd21114ce7df35

Contents?: true

Size: 1.95 KB

Versions: 22

Compression:

Stored size: 1.95 KB

Contents

module Symphonia
  module ModelFilters
    class Base
      attr_reader :name, :type, :caption, :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(_c)
        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=(q)
        if (m = q.downcase.match(/^([!~><]+)(.*)/))
          @value = m[2]
          self.operator = m[1]
        else
          @operator ||= 'eq'
          @value = q
        end

        @value
      end

      def operator=(o)
        @operator = case o
                    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

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
symphonia-3.4.0 lib/symphonia/model_filters/base.rb
symphonia-3.3.4 lib/symphonia/model_filters/base.rb
symphonia-3.3.3 lib/symphonia/model_filters/base.rb
symphonia-3.3.2 lib/symphonia/model_filters/base.rb
symphonia-3.3.0 lib/symphonia/model_filters/base.rb
symphonia-3.2.4 lib/symphonia/model_filters/base.rb
symphonia-3.2.3 lib/symphonia/model_filters/base.rb
symphonia-3.2.2 lib/symphonia/model_filters/base.rb
symphonia-3.2.1 lib/symphonia/model_filters/base.rb
symphonia-3.1.5 lib/symphonia/model_filters/base.rb
symphonia-3.1.4 lib/symphonia/model_filters/base.rb
symphonia-3.1.3 lib/symphonia/model_filters/base.rb
symphonia-3.1.2 lib/symphonia/model_filters/base.rb
symphonia-3.1.1 lib/symphonia/model_filters/base.rb
symphonia-3.1.0 lib/symphonia/model_filters/base.rb
symphonia-3.0.3 lib/symphonia/model_filters/base.rb
symphonia-3.0.2 lib/symphonia/model_filters/base.rb
symphonia-2.2.1 lib/symphonia/model_filters/base.rb
symphonia-3.0.1 lib/symphonia/model_filters/base.rb
symphonia-3.0.0 lib/symphonia/model_filters/base.rb