filterrific
filterrific copied to clipboard
Check if filters are set in the view
I was wondering if there's a way to check if the filters are set in the view. I'd like to provide a hint to my users that the view is currently filtered.
@octant great suggestion. I have thought about this in the past but never got around to implementing it. I think it's possible and I can add this to filterrific pretty quickly. What do you think of the following API:
We want a flag that indicates if Filterrific's default settings have been modified by either adding another filter, or changing one of the default values.
This would be a method on the @filterrific
object. What should we call this method?
-
#default_settings?
-
#has_filters_applied?
Hmm. Do you have any suggestions?
Thanks for using Filterrific.
For this particular use case I think #has_filters_applied?
reads better than #default_settings?
.
Thank you for creating such a great gem!
@octant sorry for the delay on this. I was traveling last week and am kind of stuck on what to do if a filterrific default param is given as a Proc. I'm still working on this, just need a bit more time.
To expand on this, I would like my users to see what filters are in place and to be able to selectively remove filters. Something like this:
http://www.qualtrics.com/wp-content/uploads/2013/08/delete.png http://i.stack.imgur.com/BKKvw.png
Would it make sense to also have a #applied_filters method that returns an array/hash of filters in place?
@alankessler great idea on the #applied_filters
method. I'm thinking of implementing 'dirty' object tracking on the @filterrific
object. If @filterrific
is initialized with the defaults, it's considered clean. If it's initialized with params that are different from the defaults, it's considered 'dirty'.
This approach will allow me to return a collection of custom filter params that can be used to clear filters as shown in your screenshots.
I'm thinking of these two new methods:
-
#custom_filter_params
, returns an Array of Hashes like so: [{ name: :search_query, value: 'foo' }] -
#has_custom_filter_params
, implemented ascustom_filter_params.any?
, returningtrue
orfalse
That would do it. Seems like a reasonable solution.
jhund, did you happen to make any progress on this?
@alankessler unfortunately I haven't resolved this yet. I could use it in one of my other open source projects. So the motivation is there, it's just not super high on my priority list right now.
@alankessler I started working on this, and then got stuck on how to handle Procs as default values. I'm sure I can figure it out, just need to sit down and do it...
Just noticed the 2.0.5 version already contains a stub for this feature :)
# Returns true if this instance of Filterrific has params other than the
# defaults.
# @return [Boolean]
def has_filters_applied?
sdf
end
Would be great if this was completed :+1:
@jhund Suggested implementation & temporary monkey patch.
Filterrific::ParamSet.class_eval do
def has_filters_applied?
self.to_hash.keys.length > 0
end
end
Have there been any progress on this?