Sha256: b1f0922207f21a3e7d81bdca42f34b6701bd5348c6ac9ecc3ce507e7beec9cca

Contents?: true

Size: 1.59 KB

Versions: 21

Compression:

Stored size: 1.59 KB

Contents

class Agilibox::SmallData::FilterStrategyByDateOrDatetimePeriod < ::Agilibox::SmallData::FilterStrategyByKeyValue
  def initialize(*)
    if self.class == Agilibox::SmallData::FilterStrategyByDateOrDatetimePeriod
      raise "please use FilterStrategyByDatePeriod or FilterStrategyByDatetimePeriod"
    end

    super
  end

  def apply(query, value) # rubocop:disable Metrics/MethodLength
    value = value.to_s

    if value == "today"
      a = now
      b = now
    elsif value == "yesterday"
      a = (now - 1.day)
      b = (now - 1.day)
    elsif value == "this_week"
      a = now.beginning_of_week
      b = now.end_of_week
    elsif value == "this_month"
      a = now.beginning_of_month
      b = now.end_of_month
    elsif value == "this_year"
      a = now.beginning_of_year
      b = now.end_of_year
    elsif value == "last_week"
      a = (now - 1.week).beginning_of_week
      b = (now - 1.week).end_of_week
    elsif value == "last_month"
      a = (now - 1.month).beginning_of_month
      b = (now - 1.month).end_of_month
    elsif value == "last_year"
      a = (now - 1.year).beginning_of_year
      b = (now - 1.year).end_of_year
    elsif (m = value.match(/last\.([0-9]+)\.(days?|weeks?|months?|years?)/))
      a = now - m[1].to_i.public_send(m[2])
      b = now
    else
      return query
    end

    if now.is_a?(Time)
      a = a.beginning_of_day if a
      b = b.end_of_day       if b
    end

    column = key.is_a?(Symbol) ? "#{query.model.table_name}.#{key}" : key.to_s

    query = query.where("#{column} >= ?", a) if a
    query = query.where("#{column} <= ?", b) if b

    query
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
agilibox-1.5.5 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.5.4 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.5.3 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.5.2 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.5.1 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.5.0 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.4.4 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.4.3 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.4.2 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.4.1 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.4.0 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.3.6 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.3.5 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.3.4 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.3.3 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.3.2 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.3.1 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.3.0 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.2.3 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb
agilibox-1.2.2 app/filters/agilibox/small_data/filter_strategy_by_date_or_datetime_period.rb