Sha256: a5352a96d42fdc2e8ddc61aba35f94cd85e2baa5be0e5351cf17b58b53974d5a

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module AjaxDatatablesRails
  module Datatable
    class Column
      module DateFilter

        RANGE_DELIMITER = '-'

        class DateRange
          attr_reader :begin, :end

          def initialize(date_start, date_end)
            @begin = date_start
            @end   = date_end
          end

          def exclude_end?
            false
          end
        end

        # Add delimiter option to handle range search
        def delimiter
          @delimiter ||= @view_column.fetch(:delimiter, RANGE_DELIMITER)
        end

        # A range value is in form '<range_start><delimiter><range_end>'
        # This returns <range_start>
        def range_start
          @range_start ||= formatted_value.split(delimiter)[0]
        end

        # A range value is in form '<range_start><delimiter><range_end>'
        # This returns <range_end>
        def range_end
          @range_end ||= formatted_value.split(delimiter)[1]
        end

        def empty_range_search?
          (formatted_value == delimiter) || (range_start.blank? && range_end.blank?)
        end

        # Do a range search
        def date_range_search
          return nil if empty_range_search?

          table[field].between(DateRange.new(range_start_casted, range_end_casted))
        end

        private

        def range_start_casted
          range_start.blank? ? parse_date('01/01/1970') : parse_date(range_start)
        end

        def range_end_casted
          range_end.blank? ? parse_date('9999-12-31 23:59:59') : parse_date("#{range_end} 23:59:59")
        end

        def parse_date(date)
          Time.zone ? Time.zone.parse(date) : Time.parse(date)
        end

      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ajax-datatables-rails-1.5.0 lib/ajax-datatables-rails/datatable/column/date_filter.rb
ajax-datatables-rails-1.4.0 lib/ajax-datatables-rails/datatable/column/date_filter.rb
ajax-datatables-rails-1.3.1 lib/ajax-datatables-rails/datatable/column/date_filter.rb
ajax-datatables-rails-1.3.0 lib/ajax-datatables-rails/datatable/column/date_filter.rb