Sha256: 2d44a98c018d20b59e86677da4c645fd955576ba67d7a503bf62991d1b4cfdfe

Contents?: true

Size: 1.29 KB

Versions: 5

Compression:

Stored size: 1.29 KB

Contents

class Agilibox::SmallData::FilterStrategyByTimePeriod < ::Agilibox::SmallData::FilterStrategyByKeyValue
  def apply(query, value) # rubocop:disable Metrics/MethodLength
    if value == "today"
      a = Time.zone.now.beginning_of_day
      b = Time.zone.now.end_of_day
    elsif value == "yesterday"
      a = (Time.zone.now - 1.day).beginning_of_day
      b = (Time.zone.now - 1.day).end_of_day
    elsif value == "this_week"
      a = Time.zone.now.beginning_of_week
      b = Time.zone.now.end_of_week
    elsif value == "this_month"
      a = Time.zone.now.beginning_of_month
      b = Time.zone.now.end_of_month
    elsif value == "this_year"
      a = Time.zone.now.beginning_of_year
      b = Time.zone.now.end_of_year
    elsif value == "last_week"
      a = (Time.zone.now - 1.week).beginning_of_week
      b = (Time.zone.now - 1.week).end_of_week
    elsif value == "last_month"
      a = (Time.zone.now - 1.month).beginning_of_month
      b = (Time.zone.now - 1.month).end_of_month
    elsif value == "last_year"
      a = (Time.zone.now - 1.year).beginning_of_year
      b = (Time.zone.now - 1.year).end_of_year
    else
      return query
    end

    criteria = "#{query.model.table_name}.#{key}"

    query
      .where("#{criteria} >= ?", a.to_date)
      .where("#{criteria} <= ?", b.to_date)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
agilibox-1.0.14 app/filters/agilibox/small_data/filter_strategy_by_time_period.rb
agilibox-1.0.13 app/filters/agilibox/small_data/filter_strategy_by_time_period.rb
agilibox-1.0.12 app/filters/agilibox/small_data/filter_strategy_by_time_period.rb
agilibox-1.0.11 app/filters/agilibox/small_data/filter_strategy_by_time_period.rb
agilibox-1.0.10 app/filters/agilibox/small_data/filter_strategy_by_time_period.rb