Sha256: c7be234bcaf9fe3b478e7eadf666b8af91981ba102d5f431e0ef564ee212fe7c

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 KB

Contents

module RangedFilter
  

  def initialize(grid, name, options, &block)
    super(grid, name, options, &block)
    if range?
      options[:multiple] = true
    end
  end

  def parse_values(value)
    result = super(value)
    return result if !range? || result.nil?
    # Simulate single point range
    return result..result unless result.is_a?(Array)

    case result.size
    when 0
      nil
    when 1
      result.first
    when 2
      if result.first && result.last && result.first > result.last
        # If wrong range is given - reverse it to be always valid
        result.reverse
      else
        result
      end
    else
      raise ArgumentError, "Can not create a date range from array of more than two: #{result.inspect}"
    end

  end

  def range?
    options[:range]
  end

  def default_filter_where(driver, scope, value)
    if range? && value.is_a?(Array)
      left, right = value
      if left
        scope = driver.greater_equal(scope, name, left)
      end
      if right
        scope = driver.less_equal(scope, name, right)
      end
      scope
    else 
      super(driver, scope, value)
    end
  end


end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
datagrid-1.3.7 lib/datagrid/filters/ranged_filter.rb
datagrid-1.3.6 lib/datagrid/filters/ranged_filter.rb
datagrid-1.3.5 lib/datagrid/filters/ranged_filter.rb
datagrid-1.3.4 lib/datagrid/filters/ranged_filter.rb