Sha256: 47c7dfd2e6c3588c51741e0af4e2db050aa9e597a51e8a90a27f78727050eda3

Contents?: true

Size: 1.2 KB

Versions: 22

Compression:

Stored size: 1.2 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)
    if range? 
      if 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
      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

22 entries across 22 versions & 1 rubygems

Version Path
datagrid-1.3.3 lib/datagrid/filters/ranged_filter.rb
datagrid-1.3.2 lib/datagrid/filters/ranged_filter.rb
datagrid-1.3.1 lib/datagrid/filters/ranged_filter.rb
datagrid-1.3.0 lib/datagrid/filters/ranged_filter.rb
datagrid-1.2.3 lib/datagrid/filters/ranged_filter.rb
datagrid-1.2.2 lib/datagrid/filters/ranged_filter.rb
datagrid-1.2.1 lib/datagrid/filters/ranged_filter.rb
datagrid-1.2.0 lib/datagrid/filters/ranged_filter.rb
datagrid-1.1.2 lib/datagrid/filters/ranged_filter.rb
datagrid-1.1.1 lib/datagrid/filters/ranged_filter.rb
datagrid-1.1.0 lib/datagrid/filters/ranged_filter.rb
datagrid-1.0.5 lib/datagrid/filters/ranged_filter.rb
datagrid-1.0.4 lib/datagrid/filters/ranged_filter.rb
datagrid-1.0.3 lib/datagrid/filters/ranged_filter.rb
datagrid-1.0.2 lib/datagrid/filters/ranged_filter.rb
datagrid-1.0.1 lib/datagrid/filters/ranged_filter.rb
datagrid-1.0.0 lib/datagrid/filters/ranged_filter.rb
datagrid-0.9.3 lib/datagrid/filters/ranged_filter.rb
datagrid-0.9.2 lib/datagrid/filters/ranged_filter.rb
datagrid-0.9.0 lib/datagrid/filters/ranged_filter.rb