Sha256: b7d7d1cc9a270bf2570d6d93cac08c599038478b3c24ba4c4153da6a2de4627f

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 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)
    if value.nil?
      return scope if !allow_nil?
    else
      return scope if value.blank? && !allow_blank?
    end
    ::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.wrap(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

  def allow_nil?
    options.has_key?(:allow_nil) ? options[:allow_nil] : options[:allow_blank]
  end

  def allow_blank?
    options[:allow_blank]
  end

end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
datagrid-0.5.4 lib/datagrid/filters/base_filter.rb
datagrid-0.5.3 lib/datagrid/filters/base_filter.rb
datagrid-0.5.2 lib/datagrid/filters/base_filter.rb
datagrid-0.5.1 lib/datagrid/filters/base_filter.rb
datagrid-0.5.0 lib/datagrid/filters/base_filter.rb
datagrid-0.4.1 lib/datagrid/filters/base_filter.rb
datagrid-0.4.0 lib/datagrid/filters/base_filter.rb
datagrid-0.3.6 lib/datagrid/filters/base_filter.rb
datagrid-0.3.5 lib/datagrid/filters/base_filter.rb