Class: Plutonium::Query::Filters::DateRange

Inherits:
Plutonium::Query::Filter show all
Defined in:
lib/plutonium/query/filters/date_range.rb

Overview

DateRange filter for filtering between two dates

Examples:

Basic usage

filter :created_at, with: :date_range

With custom labels

filter :published_at, with: :date_range, from_label: "Published from", to_label: "Published to"

Instance Attribute Summary

Attributes inherited from Plutonium::Query::Filter

#key

Instance Method Summary collapse

Methods inherited from Plutonium::Query::Filter

lookup

Methods included from Definition::Presentable

#description, #icon, #label

Constructor Details

#initialize(from_label: nil, to_label: nil) ⇒ DateRange

Returns a new instance of DateRange.



13
14
15
16
17
# File 'lib/plutonium/query/filters/date_range.rb', line 13

def initialize(from_label: nil, to_label: nil, **)
  super(**)
  @from_label = from_label
  @to_label = to_label
end

Instance Method Details

#apply(scope, from: nil, to: nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/plutonium/query/filters/date_range.rb', line 19

def apply(scope, from: nil, to: nil)
  from_date = parse_date(from)
  to_date = parse_date(to)

  if from_date && to_date
    scope.where(key => from_date.beginning_of_day..to_date.end_of_day)
  elsif from_date
    scope.where(key => from_date.beginning_of_day..)
  elsif to_date
    scope.where(key => ..to_date.end_of_day)
  else
    scope
  end
end

#customize_inputsObject



34
35
36
37
38
39
# File 'lib/plutonium/query/filters/date_range.rb', line 34

def customize_inputs
  input :from, as: :date
  input :to, as: :date
  field :from, placeholder: @from_label || "#{key.to_s.humanize} from..."
  field :to, placeholder: @to_label || "#{key.to_s.humanize} to..."
end