Sha256: 38ace510cdc13c8ed8764501b64d6e94999e5d7cf30168320c123ebbf86b01ed

Contents?: true

Size: 1.01 KB

Versions: 5

Compression:

Stored size: 1.01 KB

Contents

module RangedFilter
  

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

  def format_values(value)
    result = super(value)
    if range? 
      if result.is_a?(Array)
        case result.size
        when 0
          nil
        when 1
          result.first
        when 2
          result
        else
          raise ArgumentError, "Can not create a date range from array of more than two: #{result.inspect}"
        end
      else
        # Simulate single point range
        result..result
      end

    else
      result
    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

5 entries across 5 versions & 1 rubygems

Version Path
datagrid-0.8.2 lib/datagrid/filters/ranged_filter.rb
datagrid-0.8.1 lib/datagrid/filters/ranged_filter.rb
datagrid-0.8.0 lib/datagrid/filters/ranged_filter.rb
datagrid-0.7.1 lib/datagrid/filters/ranged_filter.rb
datagrid-0.7.0 lib/datagrid/filters/ranged_filter.rb