Я использую фильтр с фильтром для фильтрации элементов на странице рынка.
Если я выберу опцию фильтра для Каталога, опции для Типа и Цвета все еще будут видны, даже если результат будет пустым.Как я могу удалить опции из выбора, если результат будет пустым?
Модели
catalog.rb
class Catalog < ApplicationRecord
belongs_to :vendor, optional: true
has_one :picture, as: :imageable
has_many :items
def self.options_for_select
order('LOWER(title)').map { |e| [e.title, e.id] }.reverse
end
end
color.rb
class Color < ApplicationRecord
has_many :items
def self.options_for_select
order('LOWER(title)').map { |e| [e.title, e.id] }.reverse
end
end
type.rb
class Type < ApplicationRecord
has_many :items
def self.options_for_select
order('LOWER(title)').map { |e| [e.title, e.id] }.reverse
end
end
item.rb
class Item < ApplicationRecord
belongs_to :catalog
belongs_to :color
belongs_to :type
filterrific(
default_filter_params: { sorted_by: 'price_at_asc' },
available_filters: [
:sorted_by,
:with_catalog_id,
:with_color_id,
:with_type_id
]
)
scope :with_catalog_id, lambda { |catalog_ids| where(catalog_id: [*catalog_ids]) }
scope :with_color_id, lambda { |color_ids| where(color_id: [*color_ids]) }
scope :with_type_id, lambda { |type_ids| where(type_id: [*type_ids]) }
scope :sorted_by, lambda { |sort_option|
direction = (sort_option =~ /desc$/) ? 'desc' : 'asc'
case sort_option.to_s
when /^price/
order("items.price #{ direction }")
else
raise(ArgumentError, "Invalid sort option: #{ sort_option.inspect }")
end
}
def self.options_for_sorted_by
[
['Price asc', 'price_asc'],
['Price desc', 'price_desc']
]
end
end
Контроллер
items_controller.rb
class ItemsController < ApplicationController
def show; end
def index
@filterrific = initialize_filterrific(Item, params[:filterrific],
select_options: {
sorted_by: Item.options_for_sorted_by,
with_catalog_id: Catalog.options_for_select,
with_type_id: Type.options_for_select,
with_color_id: Color.options_for_select},
persistence_id: 'shared_key',
default_filter_params: {}) || return
@obj = @filterrific.find
respond_to do |format|
format.html
format.js
end
end
end
Просмотр
%h1 Items
.row
= render 'layouts/filter'
.filterrific_results{id: 'filterrific_results'}
= render 'items/listing', items: @obj
Частицы
Фильтр
= form_for_filterrific @filterrific do |f|
.filter
Catalog
= f.select(:with_catalog_id, @filterrific.select_options[:with_catalog_id], class: 'filterrific-periodically-observed', include_blank: '- Any -')
.filter
Type
= console
= f.select(:with_type_id, @filterrific.select_options[:with_type_id].reject { |e| Type.find(e.last).items.count == 0}, class: 'filterrific-periodically-observed', include_blank: '- Any -')
.filter
Color
= f.select(:with_color_id, @filterrific.select_options[:with_color_id], class: 'filterrific-periodically-observed', include_blank: '- Any -')
.filter
Sort by price
= f.select(:sorted_by, @filterrific.select_options[:sorted_by], class: 'filterrific-periodically-observed', include_blank: '- Any -')
.filter
= link_to('Reset', reset_filterrific_url, class: 'btn btn-primary')
= render_filterrific_spinner
Листинг
- items.each do |item|
.col-xs-6.col-sm-6.col-md-4.col-lg-3
.thumbnail.hover-border
.caption.text-center
#{item.catalog.title} #{item.title} #{item.articul}
%br
#{item.price}