Sha256: 36e4eacdd51544f99fab6ff0556a2a9891578207200af5f8e3061b93bb1d52b2

Contents?: true

Size: 1.07 KB

Versions: 3

Compression:

Stored size: 1.07 KB

Contents

module Gitter

  class SelectFilter < AbstractFilter

    attr_reader :filters, :values

    def initialize grid, name, filters, opts = {} 
      super grid, name, opts
      @filters = filters.inject({}){|memo,filter| memo[filter.name] = filter; memo}
      @values = opts[:values]
      if @input_options == true
        @input_options = {}
        @input_options[:collection] = @filters.keys
      end
    end

    def apply driver, value 
      if filter = @filters[value]
        filter.apply driver
      else
        driver
      end
    end

    def counts driver = nil
      values_names = Hash === values ? values.keys : values
      @filters.values.inject({}) do |memo,filter|
        if !values_names || values_names.include?(filter.name)
          count = filter.counts[true]
          memo[filter.name] = count if count > 0
        end
        memo
      end
    end

    # returns map { :name => label }
    def distinct_values driver = nil
      @distinct_values ||= values || @filters.values.inject({}){|memo, filter| memo[filter.name] = filter.label; memo }
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
gitter-1.1.5 lib/gitter/filters/select_filter.rb
gitter-1.1.4 lib/gitter/filters/select_filter.rb
gitter-1.1.3 lib/gitter/filters/select_filter.rb