class Datagrid::Filters::DynamicFilter

Constants

AVAILABLE_OPERATIONS
DEFAULT_OPERATIONS
EQUAL_OPERATION
LESS_EQUAL_OPERATION
LIKE_OPERATION
MORE_EQUAL_OPERATION

Public Class Methods

new(*) click to toggle source
Calls superclass method Datagrid::Filters::BaseFilter::new
# File lib/datagrid/filters/dynamic_filter.rb, line 19
def initialize(*)
  super
  options[:select] ||= default_select
  options[:operations] ||= DEFAULT_OPERATIONS
  unless options.has_key?(:include_blank)
    options[:include_blank] = false
  end
end

Public Instance Methods

default_filter_where(scope, filter) click to toggle source
# File lib/datagrid/filters/dynamic_filter.rb, line 39
def default_filter_where(scope, filter)
  field, operation, value = filter
  date_conversion = value.is_a?(Date) && driver.is_timestamp?(scope, field)

  return scope if field.blank? || operation.blank?
  unless operations.include?(operation)
    raise Datagrid::FilteringError, "Unknown operation: #{operation.inspect}. Available operations: #{operations.join(' ')}"
  end
  case operation
  when EQUAL_OPERATION
    if date_conversion
      value = Datagrid::Utils.format_date_as_timestamp(value)
    end
    driver.where(scope, field, value)
  when LIKE_OPERATION
    if column_type(field) == :string
      driver.contains(scope, field, value)
    else
      if date_conversion
        value = Datagrid::Utils.format_date_as_timestamp(value)
      end
      driver.where(scope, field, value)
    end
  when MORE_EQUAL_OPERATION
    if date_conversion
      value = value.beginning_of_day
    end
    driver.greater_equal(scope, field, value)
  when LESS_EQUAL_OPERATION
    if date_conversion
      value = value.end_of_day
    end
    driver.less_equal(scope, field, value)
  else
    raise Datagrid::FilteringError, "Unknown operation: #{operation.inspect}. Use filter block argument to implement operation"
  end
end
operations() click to toggle source
# File lib/datagrid/filters/dynamic_filter.rb, line 77
def operations
  options[:operations]
end
operations_select() click to toggle source
# File lib/datagrid/filters/dynamic_filter.rb, line 81
def operations_select
  operations.map do |operation|
    [I18n.t(operation, scope: "datagrid.filters.dynamic.operations").html_safe, operation]
  end
end
parse_values(filter) click to toggle source
# File lib/datagrid/filters/dynamic_filter.rb, line 28
def parse_values(filter)
  field, operation, value = filter

  [field, operation, type_cast(field, value)]
end
unapplicable_value?(filter) click to toggle source
# File lib/datagrid/filters/dynamic_filter.rb, line 34
def unapplicable_value?(filter)
  _, _, value = filter
  super(value)
end

Protected Instance Methods

column_type(field) click to toggle source
# File lib/datagrid/filters/dynamic_filter.rb, line 122
def column_type(field)
  grid_class.driver.normalized_column_type(grid_class.scope, field)
end
default_select() click to toggle source
# File lib/datagrid/filters/dynamic_filter.rb, line 89
def default_select
  proc {|grid|
    grid.driver.column_names(grid.scope).map do |name|
      # Mongodb/Rails problem:
      # '_id'.humanize returns ''
      [name.gsub(/^_/, '').humanize.strip, name]
    end
  }
end
type_cast(field, value) click to toggle source
# File lib/datagrid/filters/dynamic_filter.rb, line 99
def type_cast(field, value)
  type = column_type(field)
  return nil if value.blank?
  case type
  when :string
    value.to_s
  when :integer
    value.is_a?(Numeric) || value =~ /^\d/ ?  value.to_i : nil
  when :float
    value.is_a?(Numeric) || value =~ /^\d/ ?  value.to_f : nil
  when :date
    Datagrid::Utils.parse_date(value)
  when :timestamp
    Datagrid::Utils.parse_date(value)
  when :boolean
    Datagrid::Utils.booleanize(value)
  when nil
    value
  else
    raise NotImplementedError, "unknown column type: #{type.inspect}"
  end
end