Sha256: 163d1ed847f36a2f1e4c0edda7a107c4ccb6057cfb7846bc12290be6a83bfe4f

Contents?: true

Size: 979 Bytes

Versions: 3

Compression:

Stored size: 979 Bytes

Contents

module Filternator
  class Filter

    attr_reader :scope, :scope_name, :presenter, :all_filters, :default_filter

    def initialize(options = {})
      @scope          = options.fetch(:scope)
      @scope_name     = options.fetch(:scope_name)     { derive_scope_name }
      @presenter      = options.fetch(:presenter)      { null_presenter }
      @all_filters    = options.fetch(:all_filters)    { scope.all_filters }
      @default_filter = options.fetch(:default_filter) { "all" }
    end

    def stats
      pairs = all_filters.map { |filter|
        result = apply(filter: filter).count
        result = result.values.inject(:+) if result.is_a?(Hash) # ActiveRecord `group by` fix
        [ filter, result ]
      }
      Hash[pairs]
    end

    def apply(params)
      ApplyFilter.new(self, params)
    end

    private

    def derive_scope_name
      scope.model_name.element.pluralize
    end

    def null_presenter
      lambda { |item| item }
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
filternator-0.0.6 lib/filternator/filter.rb
filternator-0.0.5 lib/filternator/filter.rb
filternator-0.0.4 lib/filternator/filter.rb