Sha256: 3c4b3e86e2af8b7f14c62f7aa7b0014b128462e9fb58bdb6945fbfe6c417163d

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 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? ? Time.current : 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

2 entries across 2 versions & 1 rubygems

Version Path
ajax-datatables-rails-1.2.0 lib/ajax-datatables-rails/datatable/column/date_filter.rb
ajax-datatables-rails-1.1.0 lib/ajax-datatables-rails/datatable/column/date_filter.rb