Class: Headmin::Filter::Date

Inherits:
Base
  • Object
show all
Defined in:
app/models/headmin/filter/date.rb

Constant Summary collapse

OPERATORS =
%w[eq not_eq gt gteq lt lteq between not_between in not_in is_null is_not_null]
QUERY_OPERATOR_CONVERT_TO =
{
  between: %w[eq],
  not_between: %w[not_eq]
}
OPERATORS_CONVERT_TO =
{
  convert_to_range: %w[between not_between],
  convert_to_array: %w[in not_in],
  convert_to_value: %w[matches does_not_match is_null is_not_null starts_with ends_with],
  convert_to_datetime_equals: %w[eq not_eq],
  convert_to_datetime_begin: %w[lt gteq],
  convert_to_datetime_end: %w[gt lteq]
}

Constants inherited from Base

Base::QUERY_VALUE_CONVERT_TO

Instance Attribute Summary

Attributes inherited from Base

#attribute, #instructions, #raw_value

Instance Method Summary collapse

Methods inherited from Base

#build_query, #conditionals, #initialize, #is_i?, #operators, #query, #string

Constructor Details

This class inherits a constructor from Headmin::Filter::Base

Instance Method Details

#cast_value(value) ⇒ Object



20
21
22
23
24
# File 'app/models/headmin/filter/date.rb', line 20

def cast_value(value)
  value.present? ? value.to_date : ::Date.current
rescue
  raise TypeError, "The value that was passed to this filter is not a date"
end

#convert_to_datetime_begin(value) ⇒ Object



46
47
48
49
# File 'app/models/headmin/filter/date.rb', line 46

def convert_to_datetime_begin(value)
  casted_date = cast_value(value)
  casted_date.beginning_of_day
end

#convert_to_datetime_end(value) ⇒ Object



51
52
53
54
# File 'app/models/headmin/filter/date.rb', line 51

def convert_to_datetime_end(value)
  casted_date = cast_value(value)
  casted_date.end_of_day
end

#convert_to_datetime_equals(value) ⇒ Object



40
41
42
43
44
# File 'app/models/headmin/filter/date.rb', line 40

def convert_to_datetime_equals(value)
  casted_date = cast_value(value)

  (casted_date - 1.day).end_of_day..casted_date.end_of_day
end

#display_value(value) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/headmin/filter/date.rb', line 56

def display_value(value)
  # This uses the default date format of headmin.
  # Can be overwritten by setting default date format of the application.

  current_operator = instructions.find { |instruction| instruction[:value] == value }[:operator]

  # To make the operators eq and not_eq work, we pass a range.
  # However, display value should return this as a date and not a range.
  if value.class.to_s == "Range" && (current_operator == "eq" || current_operator == "not_eq")
    I18n.l(value.last.to_date)
  elsif values.class.to_s
    "#{I18n.l(value.first.to_date)} - #{I18n.l(value.last.to_date)}"
  else
    I18n.l(value.to_date)
  end
end

#output_value(instruction) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'app/models/headmin/filter/date.rb', line 30

def output_value(instruction)
  if %w[eq not_eq].include?(instruction[:operator])
    instruction[:value].last.to_date
  elsif %w[lt lteq gt gteq].include?(instruction[:operator])
    instruction[:value].to_date
  else
    instruction[:value]
  end
end

#valuesObject



26
27
28
# File 'app/models/headmin/filter/date.rb', line 26

def values
  @instructions.map { |instruction| output_value(instruction) }.compact
end