Module: Datagrid::Filters::RangedFilter

Included in:
DateFilter, DateTimeFilter, FloatFilter, IntegerFilter, StringFilter
Defined in:
lib/datagrid/filters/ranged_filter.rb

Instance Method Summary collapse

Instance Method Details

#default_filter_where(scope, value) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/datagrid/filters/ranged_filter.rb', line 40

def default_filter_where(scope, value)
  if range? && value.is_a?(Array)
    left, right = value
    if left
      scope = driver.greater_equal(scope, name, left)
    end
    if right
      scope = driver.less_equal(scope, name, right)
    end
    scope
  else
    super(scope, value)
  end
end

#initialize(grid, name, options, &block) ⇒ Object


3
4
5
6
7
8
# File 'lib/datagrid/filters/ranged_filter.rb', line 3

def initialize(grid, name, options, &block)
  super(grid, name, options, &block)
  if range?
    options[:multiple] = true
  end
end

#parse_values(value) ⇒ Object


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/datagrid/filters/ranged_filter.rb', line 10

def parse_values(value)
  result = super(value)
  return result if !range? || result.nil?
  # Simulate single point range
  return [result, result] unless result.is_a?(Array)

  case result.size
  when 0
    nil
  when 1
    result.first
  when 2
    if result.first && result.last && result.first > result.last
      # If wrong range is given - reverse it to be always valid
      result.reverse
    elsif !result.first && !result.last
      nil
    else
      result
    end
  else
    raise ArgumentError, "Can not create a date range from array of more than two: #{result.inspect}"
  end

end

#range?Boolean

Returns:

  • (Boolean)

36
37
38
# File 'lib/datagrid/filters/ranged_filter.rb', line 36

def range?
  options[:range]
end