简体   繁体   中英

Filterrific, scope without argument and check boxes

I'm getting in filterrific, and find it really good for its integration with draper decorators and so.

But I would like to make a simple request with a scope without parameter.

Imagine a model scope like scope :unassigned, -> { where(support_user: nil)} . Then you can do Ticket.unassigned to perform the query.

How do I integrate this unparametred scope with filterrific? With an "unassigned" check box for example.

EDIT: The 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 that we miss is filter with enums. If I have

class Ticket < ActiveRecord::Base

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

And want to make a filter that call Ticket.wait , how would it be possible?

Thank you!

Use a checkbox like so:

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

Make sure that the unassigned scope exists and is added to the filterrific directive in your model.

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

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

I've adapted filterrific in a way it can afford scope with arity 0 and enum requests. As arity doesn't reveal correctly the number of necessary arguments I use ArgumentError exception. Hello! I had the same expect for fiterrific. I've made a fork. As arity for scope and enums doesn't work as expected I use 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

Fork available here: https://github.com/hachpai/filterrific

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM