Sha256: 2c3c43fbe87404560898cfd4705687484f986b128cc0e03964b8dd8211df49b3

Contents?: true

Size: 1.54 KB

Versions: 2

Compression:

Stored size: 1.54 KB

Contents

module ReportsKit
  module Reports
    module FilterTypes
      class Datetime < Base
        DEFAULT_CRITERIA = {
          operator: 'between'
        }
        SEPARATOR = ' - '

        def apply_conditions(records)
          case criteria[:operator]
          when 'between'
            records.where.not(column => nil).where(column => start_at..end_at)
          else
            raise ArgumentError.new("Unsupported operator: '#{criteria[:operator]}'")
          end
        end

        def start_at_end_at
          @start_at_end_at ||= valid? ? adjust_range_to_dimension(*Reports::Data::Utils.parse_date_range(value, type: Array)) : nil
        end

        def start_at
          start_at_end_at.try(:[], 0)
        end

        def end_at
          start_at_end_at.try(:[], 1)
        end

        def adjust_range_to_dimension(start_at, end_at)
          return [start_at.beginning_of_day, end_at.end_of_day] unless primary_dimension.configured_by_time?
          return [start_at.beginning_of_day, end_at.end_of_day] if primary_dimension.granularity == 'day'
          return [
            start_at.beginning_of_week(ReportsKit.configuration.first_day_of_week),
            end_at.end_of_week(ReportsKit.configuration.first_day_of_week)
          ] if primary_dimension.granularity == 'week'
          return [
            start_at.beginning_of_month,
            end_at.end_of_month
          ] if primary_dimension.granularity == 'month'
          [start_at, end_at]
        end

        def valid?
          value.present?
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
reports_kit-0.7.1 lib/reports_kit/reports/filter_types/datetime.rb
reports_kit-0.7.0 lib/reports_kit/reports/filter_types/datetime.rb