-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Filter facets #18
Filter facets #18
Changes from all commits
3045a52
7270703
ba3c801
3d2fc0e
ba8c539
0fdfa15
4035e56
6d430bc
94d7034
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,24 @@ def filters | |
@filters ||= parse_filters_param | ||
end | ||
|
||
def filter_facets | ||
end | ||
|
||
def index_collection | ||
collection = defined?(super) ? super : HalApi::PagedCollection.new([]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I kinda thought some of this might end up in or extending paged collection, but I can see the separation of the controller knowing about the facets and setting them, like with other model data. I wonder if the controller or the paged collection should be more aware of the structure of it though...probably not, probably overkill to have mutator and other methods on the collection...maybe a method to add/set a facet? I guess I am unsure where to put all the filtering functionality. I think your move here to keep it in a concern/mixin may actually be better, as it keeps most of the logic in the same place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I could see doing an optimization like that. But to start, I wanted to leave that up to the implementing app (augury.prx.org in this case) and see where that takes us. Rather than locking them into a more controlled data structure. |
||
|
||
# add facets if defined, removing filters/facets with counts of 0 | ||
non_zero_facets = (filter_facets || {}).with_indifferent_access.tap do |hash| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the filter_key have more than just an id? Should there be a human readable label for it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have talked myself out of this, withdrawn! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd lump this in with "describing all available filters", which is sort of out-of-scope for this PR now. Opened #19. |
||
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 | ||
|
||
private | ||
|
||
def parse_filters_param | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,6 +75,10 @@ def resources_base | |
self.class.resource_class.where(nil) | ||
end | ||
|
||
def resources_query | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Convenience method for controllers implementing def filter_facets
{
type: resources_query.group(:type).count,
status: resources_query.group(:status).count,
}
end |
||
filtered(scoped(resources_base)) | ||
end | ||
|
||
def find_base | ||
filtered(scoped(included(resources_base))) | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
module HalApi | ||
module Rails | ||
VERSION = "0.6.0" | ||
VERSION = "0.7.0" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ module HalApi::Representer::CollectionPaging | |
class_eval do | ||
property :count | ||
property :total | ||
property :facets | ||
|
||
embeds :items, decorator: lambda{|*| item_decorator }, class: lambda{|*| item_class }, zoom: :always | ||
|
||
|
@@ -36,6 +37,14 @@ def self_url(represented) | |
href_url_helper(represented.params) | ||
end | ||
|
||
def vary_url(represented) | ||
href_url_helper(represented.params.except(*vary_params)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was necessary to remove any of the "varied query params" before getting the url. |
||
end | ||
|
||
def vary_params | ||
%w(page per zoom filters sorts) | ||
end | ||
|
||
def profile_url(represented) | ||
model_uri(:collection, represented.item_class) | ||
end | ||
|
@@ -46,6 +55,7 @@ def profile_url(represented) | |
# if it is a lambda, execute in the context against the represented.parent (if there is one) or represented | ||
def href_url_helper(options={}) | ||
if represented_url.nil? | ||
options = options.except(:format) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was an existing bug/annoyance where paged collections always had a self link with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yuck, good fix! |
||
result = url_for(options.merge(only_path: true)) rescue nil | ||
if represented.parent | ||
result ||= polymorphic_path([:api, represented.parent, represented.item_class], options) rescue nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
By default, the
facets
attribute won't render at all. You have to provide this method in your controller.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes sense to me - how does the API indicate which fields can be used as facets?
e.g. type, status, etc.?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The specifics of this were implemented in Augury (i.e. left up to the specific app). Ended up being a hash of "facet name" (which is the
?filters=
key name) to a list of options for the facet.