Sha256: 98ece441d98bd89eb947dbdb47e87f9b03dfda3a0b8b1df1f986d7970ee444f3

Contents?: true

Size: 1.23 KB

Versions: 5

Compression:

Stored size: 1.23 KB

Contents

require "datagrid/filters/filter_eval"

class Datagrid::Filters::BaseFilter

  attr_accessor :grid, :options, :block, :name

  def initialize(grid, name, options = {}, &block)
    self.grid = grid
    self.name = name
    self.options = options
    self.block = block
  end

  def format(value)
    raise NotImplementedError, "#format(value) suppose to be overwritten"
  end

  def apply(scope, value)
    return scope if value.nil? && !options[:allow_nil]
    return scope if value.blank? && !options[:allow_blank]
    ::Datagrid::Filters::FilterEval.new(self, scope, value).run
  end

  def format_values(value)
    if !self.multiple && value.is_a?(Array) 
      raise Datagrid::ArgumentError, "#{grid.class}.#{name} filter can not accept Array argument. Use :multiple option." 
    end
    values = Array(value)
    values.map! do |value|
      self.format(value)
    end
    self.multiple ? values : values.first
  end

  def header
    options[:header] || 
      I18n.translate(self.name, :scope => "datagrid.#{grid.param_name}.filters", :default => self.name.to_s.humanize)
  end

  def default
    default = self.options[:default]
    default.respond_to?(:call) ? default.call : default
  end

  def multiple
    self.options[:multiple]
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
datagrid-0.3.4 lib/datagrid/filters/base_filter.rb
datagrid-0.3.3 lib/datagrid/filters/base_filter.rb
datagrid-0.3.2 lib/datagrid/filters/base_filter.rb
datagrid-0.3.1 lib/datagrid/filters/base_filter.rb
datagrid-0.3.0 lib/datagrid/filters/base_filter.rb