Skip to content

Commit

Permalink
Merge pull request #2670 from jhawthorn/cbrunsdon-move_applicable_fil…
Browse files Browse the repository at this point in the history
…ters

Move Spree::Taxon#applicable_filters (rebase)
  • Loading branch information
jhawthorn authored Apr 4, 2018
2 parents 90d5b8f + 17f81e8 commit 50465c3
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 3 deletions.
2 changes: 2 additions & 0 deletions core/app/models/spree/taxon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class Taxon < Spree::Base
# @note This method is meant to be overridden on a store by store basis.
# @return [Array] filters that should be used for a taxon
def applicable_filters
Spree::Deprecation.warn "Spree::Taxon#applicable_filters is deprecated, if you are using this functionality please move it into your own application."

fs = []
fs << Spree::Core::ProductFilters.price_filter if Spree::Core::ProductFilters.respond_to?(:price_filter)
fs << Spree::Core::ProductFilters.brand_filter if Spree::Core::ProductFilters.respond_to?(:brand_filter)
Expand Down
2 changes: 1 addition & 1 deletion frontend/app/controllers/spree/products_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ProductsController < Spree::StoreController
before_action :load_product, only: :show
before_action :load_taxon, only: :index

helper 'spree/taxons'
helper 'spree/taxons', 'spree/taxon_filters'

respond_to :html

Expand Down
2 changes: 1 addition & 1 deletion frontend/app/controllers/spree/taxons_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Spree
class TaxonsController < Spree::StoreController
helper 'spree/products'
helper 'spree/products', 'spree/taxon_filters'

respond_to :html

Expand Down
13 changes: 13 additions & 0 deletions frontend/app/helpers/spree/taxon_filters_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'spree/core/product_filters'

module Spree
module TaxonFiltersHelper
def applicable_filters_for(_taxon)
[:brand_filter, :price_filter].map do |filter_name|
Spree::Core::ProductFilters.send(filter_name) if Spree::Core::ProductFilters.respond_to?(filter_name)
end.compact
end
end
end
2 changes: 1 addition & 1 deletion frontend/app/views/spree/shared/_filters.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<% filters = @taxon.applicable_filters %>
<% filters = applicable_filters_for(@taxon) %>
<% unless filters.empty? %>
<%= form_tag '', method: :get, id: 'sidebar_products_search' do %>
<%= hidden_field_tag 'per_page', params[:per_page] %>
Expand Down
12 changes: 12 additions & 0 deletions frontend/spec/helpers/taxon_filters_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Spree::TaxonFiltersHelper, type: :helper do
let(:taxon) { nil }
subject { applicable_filters_for(taxon) }

it "returns the price/brand filters" do
expect(subject.map { |y| y[:name] }).to eq ['Brands', 'Price Range']
end
end

0 comments on commit 50465c3

Please sign in to comment.