Module: HalApi::Controller::Filtering

Extended by:
ActiveSupport::Concern
Included in:
HalApi::Controller
Defined in:
lib/hal_api/controller/filtering.rb

Defined Under Namespace

Modules: ClassMethods Classes: FilterParams

Instance Method Summary collapse

Instance Method Details

#filter_facetsObject



58
59
# File 'lib/hal_api/controller/filtering.rb', line 58

def filter_facets
end

#filtersObject



54
55
56
# File 'lib/hal_api/controller/filtering.rb', line 54

def filters
  @filters ||= parse_filters_param
end

#index_collectionObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hal_api/controller/filtering.rb', line 61

def index_collection
  collection = defined?(super) ? super : HalApi::PagedCollection.new([])

  # add facets if defined, removing filters/facets with counts of 0
  non_zero_facets = (filter_facets || {}).with_indifferent_access.tap do |hash|
    hash.each do |filter_key, facets|
      hash[filter_key] = facets.try(:select) { |f| f.try(:[], :count) > 0 }
      hash.delete(filter_key) if hash[filter_key].blank?
    end
  end
  collection.facets = non_zero_facets unless non_zero_facets.blank?

  collection
end