Sha256: cb4295d25167b5082fdfda6005bd0339ceebbca47ccc62bb2732ac39b815acce

Contents?: true

Size: 1.1 KB

Versions: 3

Compression:

Stored size: 1.1 KB

Contents

# frozen_string_literal: true

# Parse date range params
#
#  date_range :created_at, keys: [:start_date, :end_date]
#
#  date_range :last_login_date
#
module QueryFilter
  module Rules
    class DateRange < Scope
      def name
        'date_range'
      end

      def valid?(values)
        period = build_period_from_params(values)
        !(period.nil? || period.default?)
      end

      def normalize_params(values)
        build_period_from_params(values)
      end

      protected

      def build_period_from_params(params)
        if params[key].present?
          QueryFilter::Utils::DatePeriod.parse_from_string(params[key], @options[:format])
        elsif keys_start_end_dates_exists?(params)
          QueryFilter::Utils::DatePeriod.new(*values_start_end_dates(params), @options[:format])
        end
      end

      def keys_start_end_dates_exists?(params)
        values = values_start_end_dates(params)
        !values.nil? && values.size == 2
      end

      def values_start_end_dates(params)
        return unless @options[:keys]

        params.values_at(*@options[:keys])
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
query_filter-0.2.3 lib/query_filter/rules/date_range.rb
query_filter-0.2.2 lib/query_filter/rules/date_range.rb
query_filter-0.2.0 lib/query_filter/rules/date_range.rb