Sha256: f0e853ca4a66c2893ce4df0a04650a34083bcaf9109bdb41e49bc5742fb8cf94

Contents?: true

Size: 1.41 KB

Versions: 2

Compression:

Stored size: 1.41 KB

Contents

module FortyFacets
  # Base class for the objects representing a specific value for a specific
  # type of filter. Most FilterDefinitions will have their own Filter subclass
  # to control values for display and rendering to request parameters.
  Filter = Struct.new(:filter_definition, :search_instance, :value) do
    def name
      filter_definition.options[:name] || filter_definition.model_field
    end

    def empty?
      value.nil? || value == '' || value == []
    end

    # generate a search with this filter removed
    def without
      search = search_instance
      return search if empty?
      new_params = search_instance.params || {}
      new_params.delete(filter_definition.request_param)
      search_instance.class.new_unwrapped(new_params)
    end
  end

  # Base class for filter with multiple values and grouped facet values
  class FacetFilter < Filter
    def values
      @values ||= Array.wrap(value).sort.uniq
    end

      protected

      def order_facet!(facet)
        order_accessor = filter_definition.options[:order]
        if order_accessor
          if order_accessor.is_a?(Proc)
            facet.sort_by!{|facet_value| order_accessor.call(facet_value.entity) }
          else
            facet.sort_by!{|facet_value| facet_value.entity.send(order_accessor) }
          end
        else
          facet.sort_by!{|facet_value| -facet_value.count }
        end
        facet
      end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
forty_facets-0.0.8 lib/forty_facets/filter.rb
forty_facets-0.0.7 lib/forty_facets/filter.rb