Filterrific, scope without arguments and checkboxes

I am getting filtering, and I think it is really good for integrating it with decorators-decorators, etc.

But I would like to make a simple scoped query with no parameters.

Imagine the scale of the model, for example scope :unassigned, -> { where(support_user: nil)}

. Then you can execute Ticket.unassigned

to fulfill the request.

How do I integrate this non-parameterized region with filtering? For example, with an "unassigned" checkbox.

EDIT: Form code:

<%= form_for_filterrific @filterrific do |f| %>
<%= f.hidden_field( :at_and_under_node_id,class: 'filterrific-periodically-observed') %>
<%= f.label "unassigned" %>
<%= f.check_box :unassigned, class: 'filterrific-periodically-observed' %>
<%= link_to('Reset filters',reset_filterrific_url) %>
 </div>
 <%# add an automated spinner to your form when the list is refreshed %>
 <%= render_filterrific_spinner %>
<% end %>

      

UPDATE: Another feature we missed is the enumerated filter. if I have

class Ticket < ActiveRecord::Base

 enum status: [:wait, :closed, :deleted]
 ...
end

      

And you want to create a filter that calls Ticket.wait

, how is it possible?

Thank!

+3


source to share


2 answers


I adapted filterrific so that it can create a scope with arity 0 and enum objects. Since arity does not correctly expand on the number of required arguments, I use the ArgumentError exception. Hello! I also expected it to be appropriate. I made a fork. Since arity for scope and enums doesn't work as expected, I am using the ArgumentError exception.

filterrific_available_filters.each do |filter_name|
    filter_param = filterrific_param_set.send(filter_name)
      next if filter_param.blank? # skip blank filter_params
      begin
        ar_rel = ar_rel.send(filter_name, filter_param)
      rescue ArgumentError #if we have a scope with arity 0 or enum query, we can perform the request without the parameter
        ar_rel = ar_rel.send(filter_name) if (filter_param == 1)
      end
    end
    ar_rel
end

      



A fork is available here: https://github.com/hachpai/filterrific

+1


source


Check the box:

<%= f.check_box "unassigned", class: 'filterrific-periodically-observed' %>

      

Make sure the scope unassigned

exists and is added to the directive filterrific

in your model.



Then pass the argument to your scope and check the value:

scope :unassigned, ->(yes_or_no) {
  return nil  if '0' == yes_or_no
  where(support_user: nil)
}

      

+2


source







All Articles