@@ -654,11 +654,16 @@ You may customize how a filter behaves by supplying a callable to the `:apply` o
654654apply that filter. The callable is passed the ` records ` , which is an ` ActiveRecord::Relation ` , the ` value ` , and an
655655` _options ` hash. It is expected to return an ` ActiveRecord::Relation ` .
656656
657+ Note: When a filter is not supplied a ` verify ` callable to modify the ` value ` that the ` apply ` callable receives,
658+ ` value ` defaults to an array of the string values provided to the filter parameter.
659+
657660This example shows how you can implement different approaches for different filters.
658661
659662``` ruby
663+ # When given the following parameter:'filter[visibility]': 'public'
664+
660665filter :visibility , apply: -> (records, value, _options ) {
661- records.where(' users.publicly_visible = ?' , value == : public )
666+ records.where(' users.publicly_visible = ?' , value[ 0 ] == ' public' )
662667}
663668```
664669
@@ -674,12 +679,12 @@ def self.apply_filter(records, filter, value, options)
674679 value.each do |val |
675680 records = records.where(_model_class .arel_table[filter].matches(val))
676681 end
677- return records
682+ records
678683 else
679684 records.where(_model_class .arel_table[filter].matches(value))
680685 end
681686 else
682- return super (records, filter, value)
687+ super (records, filter, value)
683688 end
684689end
685690```
@@ -692,13 +697,26 @@ verified value, which may be modified.
692697
693698``` ruby
694699 filter :ids ,
695- verify: -> (values, context) {
696- verify_keys(values, context)
697- return values
698- },
699- apply: -> (records, value, _options ) {
700- records.where(' id IN (?)' , value)
701- }
700+ verify: -> (values, context) {
701+ verify_keys(values, context)
702+ values
703+ },
704+ apply: -> (records, value, _options ) {
705+ records.where(' id IN (?)' , value)
706+ }
707+ ```
708+
709+ ``` ruby
710+ # A more complex example, showing how to filter for any overlap between the
711+ # value array and the possible_ids, using both verify and apply callables.
712+
713+ filter :possible_ids ,
714+ verify: -> (values, context) {
715+ values.map {|value | value.to_i}
716+ },
717+ apply: -> (records, value, _options ) {
718+ records.where(' possible_ids && ARRAY[?]' , value)
719+ }
702720```
703721
704722##### Finders
@@ -819,12 +837,12 @@ def self.apply_filter(records, filter, value, options)
819837 value.each do |val |
820838 records = records.where(_model_class .arel_table[filter].matches(val))
821839 end
822- return records
840+ records
823841 else
824842 records.where(_model_class .arel_table[filter].matches(value))
825843 end
826844 else
827- return super (records, filter, value)
845+ super (records, filter, value)
828846 end
829847end
830848```
0 commit comments