Sha256: efc11984690d9c11f843ab280fe36cda0467f48e7c7168412915c93c5dfd66b5

Contents?: true

Size: 1.36 KB

Versions: 3

Compression:

Stored size: 1.36 KB

Contents

module QueryReport
  class Filter
    attr_reader :params, :column, :type, :comparators, :block, :custom

    def initialize(params, column, options, &block)
      @params = params
      @column = column
      @type = options if options.kind_of? String
      if options.kind_of? Hash
        @type = options[:type]
        @comparators = options[:comp] || detect_comparators(@type)
      end
      @block = block
      @custom = @block ? true : false
    end

    def self.supported_types
      [:date, :text]
    end

    def keys
      @keys ||= (@comparators || {}).keys.map { |comp| "#{column.to_s}_#{comp}" }
    end

    supported_types.each do |supported_type|
      define_method("#{supported_type.to_s}?") do
        @type == supported_type
      end
    end

    def filter_with_values
      hash = {}
      @comparators.each do |key, filter_name|
        [key, filter_name]
        param_key = "#{column.to_s}_#{key.to_s}"
        hash[filter_name] = @params['q'][param_key] || @params['custom_search'][param_key] rescue ''
      end
      hash
    end

    private
    def detect_comparators(type)
      case type
        when :date
          return {gteq: I18n.t('query_report.filters.from'), lteq: I18n.t('query_report.filters.to')}
        when :text
          return {cont: @column.to_s.humanize}
      end
      {eq: I18n.t('query_report.filters.equal')}
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
query_report-0.1.0 lib/query_report/filter.rb
query_report-0.0.9 lib/query_report/filter.rb
query_report-0.0.8 lib/query_report/filter.rb