mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Filter products by distributor
This commit is contained in:
8
app/models/spree/taxon_decorator.rb
Normal file
8
app/models/spree/taxon_decorator.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
Spree::Taxon.class_eval do
|
||||
# Indicate which filters should be used for this taxon
|
||||
def applicable_filters
|
||||
fs = []
|
||||
fs << Spree::ProductFilters.distributor_filter if Spree::ProductFilters.respond_to? :distributor_filter
|
||||
fs
|
||||
end
|
||||
end
|
||||
28
app/views/spree/shared/_filters.html.erb
Normal file
28
app/views/spree/shared/_filters.html.erb
Normal file
@@ -0,0 +1,28 @@
|
||||
<% filters = @taxon ? @taxon.applicable_filters : [Spree::ProductFilters.distributor_filter] %>
|
||||
<% unless filters.empty? %>
|
||||
<%= form_tag '', :method => :get, :id => 'sidebar_products_search' do %>
|
||||
<% params[:search] ||= {} %>
|
||||
<%= hidden_field_tag 'per_page', params[:per_page] %>
|
||||
<% filters.each do |filter| %>
|
||||
<% labels = filter[:labels] || filter[:conds].map {|m,c| [m,m]} %>
|
||||
<% next if labels.empty? %>
|
||||
<div class="navigation" data-hook="navigation">
|
||||
<span class="category"> <%= filter[:name] %> </span>
|
||||
<ul class="filter_choices">
|
||||
<% labels.each do |nm,val| %>
|
||||
<% label = "#{filter[:name]}_#{nm}".gsub(/\s+/,'_') %>
|
||||
<li class="nowrap">
|
||||
<input type="checkbox"
|
||||
id="<%= label %>"
|
||||
name="search[<%= filter[:scope].to_s %>][]"
|
||||
value="<%= val %>"
|
||||
<%= params[:search][filter[:scope]] && params[:search][filter[:scope]].include?(val.to_s) ? "checked" : "" %> />
|
||||
<label class="nowrap" for="<%= label %>"> <%= nm %> </label>
|
||||
</li>
|
||||
<% end %>
|
||||
</ul>
|
||||
</div>
|
||||
<% end %>
|
||||
<%= submit_tag t(:search), :name => nil %>
|
||||
<% end %>
|
||||
<% end %>
|
||||
@@ -5,6 +5,11 @@
|
||||
#
|
||||
# In order to initialize a setting do:
|
||||
# config.setting_name = 'new value'
|
||||
|
||||
|
||||
require 'spree/product_filters'
|
||||
|
||||
|
||||
Spree.config do |config|
|
||||
# Example:
|
||||
# Uncomment to override the default site name.
|
||||
|
||||
21
lib/spree/product_filters.rb
Normal file
21
lib/spree/product_filters.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
module Spree
|
||||
module ProductFilters
|
||||
if Spree::Distributor.table_exists?
|
||||
Spree::Product.scope :distributor_any,
|
||||
lambda {|*opts|
|
||||
conds = opts.map {|o| ProductFilters.distributor_filter[:conds][o]}.reject {|c| c.nil?}
|
||||
Spree::Product.joins(:distributors).conditions_any(conds)
|
||||
}
|
||||
|
||||
def ProductFilters.distributor_filter
|
||||
distributors = Spree::Distributor.all.map(&:name).compact.uniq
|
||||
conds = Hash[*distributors.map { |d| [d, "#{Spree::Distributor.table_name}.name = '#{d}'"] }.flatten]
|
||||
{ :name => "Distributor",
|
||||
:scope => :distributor_any,
|
||||
:conds => conds,
|
||||
:labels => (distributors.sort).map { |k| [k, k] }
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
10
spec/lib/spree/product_filters_spec.rb
Normal file
10
spec/lib/spree/product_filters_spec.rb
Normal file
@@ -0,0 +1,10 @@
|
||||
require 'spec_helper'
|
||||
|
||||
describe Spree::ProductFilters do
|
||||
context "distributor filter" do
|
||||
it "provides filtering for all distributors" do
|
||||
3.times { create(:distributor) }
|
||||
Spree::ProductFilters.distributor_filter[:labels].should == Spree::Distributor.all.map { |d| [d.name, d.name] }
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user