Class: Chartable::RangeQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/chartable/range_query.rb

Class Method Summary collapse

Class Method Details

.call(scope, on:, from: nil, to: nil) ⇒ Model::ActiveRecord_Relation

Returns scope filtered according to the given date criteria. If dates are not given it returns original scope. Given value is parsed by Chronic gem and transformed into the data format

Returns:

  • (Model::ActiveRecord_Relation)


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/chartable/range_query.rb', line 10

def self.call(scope, on:, from: nil, to: nil)
  from_date = Chronic.parse(from)
  to_date = Chronic.parse(to)

  return scope if from_date.nil? && to_date.nil?

  case
    when from_date.nil?
      scope.where("DATE(#{on}) <= ?", to_date.to_date)
    when to_date.nil?
      scope.where("DATE(#{on}) >= ?", from_date.to_date)
    else
      scope.where("DATE(#{on}) >= ? AND DATE(#{on}) <= ?", from_date.to_date, to_date.to_date)
  end
end