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



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

def filter_facets
end

#filtersObject



52
53
54
# File 'lib/hal_api/controller/filtering.rb', line 52

def filters
  @filters ||= parse_filters_param
end

#index_collectionObject



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

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