rubyruby-on-rails-4filterrific

Update filters based on associations with Filterrific


In my application, I have Country, Region & City.

class Country < ActiveRecord::Base
  has_many :cities
  has_many :regions


class Region < ActiveRecord::Base
  belongs_to :country
  has_many :cities

class City < ActiveRecord::Base
  belongs_to :country
  belongs_to :region

This is my model where I have the filters

scope :with_country_id, lambda { |country_ids|
  where(:country_id => [*country_ids])
}
delegate :name, to: :country, prefix: true

scope :with_region_id, lambda { |region_ids|
  where(:region_id => [*region_ids])
}
delegate :name, to: :region, prefix: true

scope :with_city_id, lambda { |city_ids|
  where(:city_id => [*city_ids])
}
delegate :name, to: :city, prefix: true

The filter itself works fine, but how can I make the filter so when the user has selected country, my with_region_id & with_city_id, also get updated based on their association?

For instance, I have countries: USA, UK when the user selects UK, with_region_id gets updated and only shows regions that belongs to UK.


Solution

  • I used dynamic menus to solve this issue:

    I changed:

    = f.collection_select :with_country_id,@filterrific.select_options[:with_country_id]
    = f.collection_select :with_region_id,@filterrific.select_options[:with_region_id]
    

    to:

    = f.collection_select :with_country_id, Country.order(:name), :id, :name, {}, { class: 'class'}
    = f.grouped_collection_select :with_region_id, Country.order(:name), :regions, :name, :id, :name, include_blank: true
    

    JS:

    (function() {
    jQuery(function() {
        var regions;
    
        regions = $('#filterrific_with_region_id').html();
    
        if($('#filterrific_with_country_id option').is(':selected')){
            var country, escaped_country, options;
            country = $('#filterrific_with_country_id :selected').text();
            escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1');
            options = $(regions).filter("optgroup[label='" + escaped_country + "']").html();
    
            if (options) {
                $('#filterrific_with_region_id').html('<option value="">(Select one)</option>'+options);
    
            } else {
                $('#filterrific_with_region_id').html('<option value="">Please Select a Country</option>');
    
            }
    
            return $('#filterrific_with_country_id').change(function() {
                var country, escaped_country, options;
                country = $('#filterrific_with_country_id :selected').text();
                escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1');
                console.log(escaped_country);
                options = $(regions).filter("optgroup[label='" + escaped_country + "']").html();
                if (options) {
                    $('#filterrific_with_region_id').html('<option value="">(Select one)</option>'+options);
    
                } else {
                    $('#filterrific_with_region_id').html('<option value="">Please Select a Country</option>');
    
                }
            });
    
    
        } else {
            console.warn('NOT SELECTED');
    
            return $('#filterrific_with_country_id').change(function() {
                var country, escaped_country, options;
                country = $('#filterrific_with_country_id :selected').text();
                escaped_country = country.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1');
                console.log(escaped_country);
                options = $(regions).filter("optgroup[label='" + escaped_country + "']").html();
                if (options) {
                    $('#filterrific_with_region_id').html('<option value="">(Select one)</option>'+options);
    
                } else {
                    $('#filterrific_with_region_id').html('<option value="">Please Select a Country</option>');
    
                }
            });
        }
    
    });