Class: Zadok::Filters::Ransack::NumericRangeFilter

Inherits:
Base
  • Object
show all
Defined in:
lib/zadok/filters/ransack/numeric_range_filter.rb

Instance Attribute Summary

Attributes inherited from Base

#params

Instance Method Summary collapse

Methods inherited from Base

#add_to_params, #i18n_name, #index, #initialize, #modify_columns, #options, #param, #remove_from_params, #text, #title

Constructor Details

This class inherits a constructor from Zadok::Filters::Ransack::Base

Instance Method Details

#active_in?(current_params = {}) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/zadok/filters/ransack/numeric_range_filter.rb', line 42

def active_in?(current_params = {})
  range_ends(current_params).any?
end

#common_prefix(strings) ⇒ Object



52
53
54
# File 'lib/zadok/filters/ransack/numeric_range_filter.rb', line 52

def common_prefix(strings)
  strings.abbrev.keys.sort_by(&:length).first[0..-2]
end

#describe(current_params = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/zadok/filters/ransack/numeric_range_filter.rb', line 9

def describe(current_params = {})
  left, right = range_ends current_params
  key = key_from_range_ends left, right
  if key
    I18n.t("filter_labels.numeric_range.#{key}", left: left, right: right)
  else
    left
  end
end

#key_from_range_ends(left, right) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/zadok/filters/ransack/numeric_range_filter.rb', line 26

def key_from_range_ends(left, right)
  if left && right
    "closed" if left != right
  elsif right
    "left_open"
  elsif left
    "right_open"
  else
    raise "Both range ends missing; filter shouldn't be active"
  end
end

#nameObject



46
47
48
49
50
# File 'lib/zadok/filters/ransack/numeric_range_filter.rb', line 46

def name
  prefix = common_prefix params.map(&:to_s)
  prefix = params.join("_") + "_" if prefix.empty?
  prefix + "range"
end

#popover_contentObject



67
68
69
# File 'lib/zadok/filters/ransack/numeric_range_filter.rb', line 67

def popover_content
  I18n.t("filter_labels.numeric_range.popover_text")
end

#range_ends(current_params = {}) ⇒ Object



19
20
21
22
23
24
# File 'lib/zadok/filters/ransack/numeric_range_filter.rb', line 19

def range_ends(current_params = {})
  params.map do |param|
    x = current_params[param]
    x == "" ? nil : x
  end
end

#typeObject



38
39
40
# File 'lib/zadok/filters/ransack/numeric_range_filter.rb', line 38

def type
  :numeric_range_field
end

#value(current_params) ⇒ Object



56
57
58
59
60
61
62
63
64
65
# File 'lib/zadok/filters/ransack/numeric_range_filter.rb', line 56

def value(current_params)
  left, right = range_ends current_params
  if left == right
    left
  elsif left || right
    "#{left} - #{right}"
  else
    ""
  end
end