spree_products_filters 0.2.0 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +17 -0
- data/.gitlab-ci.yml +15 -0
- data/.rspec +2 -0
- data/Gemfile +13 -0
- data/LICENSE.txt +21 -0
- data/README.md +62 -0
- data/Rakefile +15 -0
- data/app/assets/javascripts/spree/frontend/filters.js +26 -0
- data/app/assets/javascripts/spree/frontend/filters_all.js +3 -0
- data/app/assets/stylesheets/spree/frontend/filters.css +15 -0
- data/app/assets/stylesheets/spree/frontend/filters_all.css +3 -0
- data/app/controllers/concerns/filters_initialization.rb +38 -0
- data/app/controllers/spree/home_controller_decorator.rb +11 -0
- data/app/controllers/spree/products_controller_decorator.rb +11 -0
- data/app/controllers/spree/taxons_controller_decorator.rb +14 -0
- data/app/helpers/spree/filters_helper.rb +3 -0
- data/app/helpers/spree/products_helper_decorator.rb +26 -0
- data/app/models/spree/filters_configuration.rb +10 -0
- data/app/models/spree/product_decorator.rb +6 -0
- data/app/overrides/spree/add_group_of_advanced_filters_to_home_index.rb +6 -0
- data/app/overrides/spree/add_group_of_advanced_filters_to_products_index.rb +6 -0
- data/app/overrides/spree/add_group_of_advanced_filters_to_taxon_show.rb +6 -0
- data/app/overrides/spree/shared/add_sorting_links_in_products.rb +4 -0
- data/app/services/advanced_filters.rb +62 -0
- data/app/views/spree/shared/_advanced_filters.html.haml +22 -0
- data/app/views/spree/shared/_options_filters.html.haml +10 -0
- data/app/views/spree/shared/_property_filters.html.haml +15 -0
- data/app/views/spree/shared/_sorting_bar.html.haml +7 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/config/initializers/core_search_custom_retrieve_products.rb +11 -0
- data/config/locales/en.yml +12 -0
- data/config/locales/ru.yml +12 -0
- data/lib/generators/spree_products_filters/install/install_generator.rb +32 -0
- data/lib/spree_products_filters.rb +3 -0
- data/lib/spree_products_filters/engine.rb +26 -0
- data/lib/spree_products_filters/version.rb +3 -0
- data/spec/controllers/home_controller_spec.rb +91 -0
- data/spec/controllers/taxons_controller_spec.rb +31 -0
- data/spec/spec_helper.rb +74 -0
- data/spec/spree/products/filters_spec.rb +7 -0
- data/spree_products_filters.gemspec +41 -0
- metadata +48 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56d9307ca041b44927270fb1cc3f019cd56f2993
|
4
|
+
data.tar.gz: 8cff7c4595f151c61bf1cc3bb418a7c812855221
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 40203fc4e81784c320393a4df9324056b1fe99ac4d10a612f60840119dd461cc53715c04fa6bd816d36da878d50b371b02e2e69a4b19cd1699c18cb992301ff4
|
7
|
+
data.tar.gz: 1c84957302f522c1df6fa56e7c88e3438aee0451a8a341d5139a62aa122953dfd3193df630013582320aa9f78933e52fc1b07a475de05ebba0d96997fb254065
|
data/.gitignore
ADDED
data/.gitlab-ci.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
image: ruby:2.4.2
|
2
|
+
|
3
|
+
cache:
|
4
|
+
paths:
|
5
|
+
- cache
|
6
|
+
|
7
|
+
before_script:
|
8
|
+
- apt-get update -q && apt-get install nodejs -yqq
|
9
|
+
- gem install bundler -v 1.15.4 --no-ri --no-rdoc
|
10
|
+
- bundle install -j $(nproc) --path cache
|
11
|
+
|
12
|
+
test:
|
13
|
+
script:
|
14
|
+
- bundle exec rake test_app
|
15
|
+
- rspec spec
|
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
# Specify your gem's dependencies in spree-products-filters.gemspec
|
4
|
+
gem 'spree', '~> 3.6.0'
|
5
|
+
gem 'spree_auth_devise', '~> 3.3'
|
6
|
+
gem 'haml'
|
7
|
+
gem 'jquery-ui-rails'
|
8
|
+
|
9
|
+
group :test do
|
10
|
+
gem 'timecop'
|
11
|
+
end
|
12
|
+
|
13
|
+
gemspec
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 JetRuby Agency
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
# Spree::Products::Filters
|
2
|
+
|
3
|
+
Current gem provides easy installation of advanced product filters for your Spree Commerce application.
|
4
|
+
|
5
|
+
Spree Commerce versions:
|
6
|
+
- Spree Core - 3.6
|
7
|
+
- Spree Auth Devise - 3.3
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'spree-products-filters'
|
15
|
+
```
|
16
|
+
|
17
|
+
And then execute:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
Or install it yourself as:
|
22
|
+
|
23
|
+
$ gem install spree-products-filters
|
24
|
+
|
25
|
+
Bundle your dependencies and run the installation generator:
|
26
|
+
|
27
|
+
```shell
|
28
|
+
bundle
|
29
|
+
bundle exec rails g spree_products_filters:install
|
30
|
+
```
|
31
|
+
|
32
|
+
## Usage
|
33
|
+
|
34
|
+
Before use add allowed sorting scopes in config/initializers/spree.rb
|
35
|
+
|
36
|
+
```shell
|
37
|
+
Spree::FiltersConfiguration::Config.allowed_sortings += [:descend_by_updated_at, :ascend_by_master_price, :descend_by_master_price, :ascend_by_name, :descend_by_name]
|
38
|
+
```
|
39
|
+
|
40
|
+
Some properties can be removed from filters, for example 'Material', 'Size' (add this line to config/initializers/spree.rb):
|
41
|
+
|
42
|
+
```shell
|
43
|
+
Spree::FiltersConfiguration::Config.hidden_properties += ['Material', 'Size']
|
44
|
+
```
|
45
|
+
|
46
|
+
## Testing
|
47
|
+
|
48
|
+
Be sure to bundle your dependencies and then create a dummy test app for the specs to run against.
|
49
|
+
|
50
|
+
```shell
|
51
|
+
bundle
|
52
|
+
bundle exec rake test_app
|
53
|
+
bundle exec rspec spec
|
54
|
+
```
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
59
|
+
|
60
|
+
## License
|
61
|
+
|
62
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rspec/core/rake_task'
|
5
|
+
require 'spree/testing_support/extension_rake'
|
6
|
+
|
7
|
+
RSpec::Core::RakeTask.new(:spec)
|
8
|
+
|
9
|
+
task :default => :spec
|
10
|
+
|
11
|
+
desc 'Generates a dummy app for testing'
|
12
|
+
task :test_app do
|
13
|
+
ENV['LIB_NAME'] = 'spree_products_filters'
|
14
|
+
Rake::Task['extension:test_app'].invoke
|
15
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
$(document).ready(function() {
|
2
|
+
if ($('#slider-range').length > 0) {
|
3
|
+
var collection_min_price = parseInt($( '#range_limits' ).data('collection-min-price'));
|
4
|
+
var collection_max_price = parseInt($( '#range_limits' ).data('collection-max-price'));
|
5
|
+
var min_range = parseInt($( '#range_limits' ).data('min-range'));
|
6
|
+
var max_range = parseInt($( '#range_limits' ).data('max-range'));
|
7
|
+
var currency = $('#amount').data('currency');
|
8
|
+
|
9
|
+
var slider = $('#slider-range').slider({
|
10
|
+
range: true,
|
11
|
+
min: collection_min_price,
|
12
|
+
max: collection_max_price,
|
13
|
+
values: [min_range, max_range],
|
14
|
+
slide: function(event, ui) {
|
15
|
+
$('#amount').val(currency + ' ' + ui.values[0] + ' - '+ currency + ' ' + ui.values[1]);
|
16
|
+
},
|
17
|
+
stop: function(_, ui) {
|
18
|
+
$('#min_price_range').val(ui.values[0]);
|
19
|
+
$('#max_price_range').val(ui.values[1]);
|
20
|
+
}
|
21
|
+
});
|
22
|
+
|
23
|
+
$('#amount').val(currency + ' ' + $('#slider-range' ).slider('values', 0) +
|
24
|
+
' - ' + currency + ' ' + $('#slider-range').slider('values', 1));
|
25
|
+
}
|
26
|
+
});
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module FiltersInitialization
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
helper_method :param_sorting
|
6
|
+
end
|
7
|
+
|
8
|
+
def param_sorting
|
9
|
+
params[:sorting].try(:to_sym) || default_sorting
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def default_sorting
|
15
|
+
:descend_by_updated_at
|
16
|
+
end
|
17
|
+
|
18
|
+
def sorting_scope
|
19
|
+
Spree::FiltersConfiguration::Config.allowed_sortings.include?(param_sorting) ? param_sorting : default_sorting
|
20
|
+
end
|
21
|
+
|
22
|
+
def filters_init
|
23
|
+
filters_hash = AdvancedFilters.new(params, @products).filter_results
|
24
|
+
@properties = filters_hash[:properties]
|
25
|
+
@product_properties = filters_hash[:product_properties]
|
26
|
+
@price_range = filters_hash[:price_range]
|
27
|
+
products = filters_hash[:products].page(params[:page]).
|
28
|
+
per(params[:per_page] || Spree::Config[:products_per_page])
|
29
|
+
@products = if sorting_scope.to_s.include?('price')
|
30
|
+
products.select('spree_products.*', 'spree_prices.amount').send(sorting_scope)
|
31
|
+
else
|
32
|
+
products.send(sorting_scope)
|
33
|
+
end
|
34
|
+
if params[:controller] == 'spree/taxons' && params[:action] == 'show'
|
35
|
+
@option_types = filters_hash[:option_types]
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Spree::HomeController.class_eval do
|
2
|
+
include FiltersInitialization
|
3
|
+
|
4
|
+
def index
|
5
|
+
@searcher = build_searcher(params.merge(include_images: true))
|
6
|
+
@products = @searcher.custom_retrieve_products
|
7
|
+
@taxonomies = Spree::Taxonomy.includes(root: :children)
|
8
|
+
|
9
|
+
filters_init
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
Spree::ProductsController.class_eval do
|
2
|
+
include FiltersInitialization
|
3
|
+
|
4
|
+
def index
|
5
|
+
@searcher = build_searcher(params.merge(include_images: true))
|
6
|
+
@products = @searcher.custom_retrieve_products
|
7
|
+
@taxonomies = Spree::Taxonomy.includes(root: :children)
|
8
|
+
|
9
|
+
filters_init
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Spree::TaxonsController.class_eval do
|
2
|
+
include FiltersInitialization
|
3
|
+
|
4
|
+
def show
|
5
|
+
@taxon = Spree::Taxon.friendly.find(params[:id])
|
6
|
+
return unless @taxon
|
7
|
+
|
8
|
+
@searcher = build_searcher(params.merge(taxon: @taxon.id, include_images: true))
|
9
|
+
@products = @searcher.custom_retrieve_products
|
10
|
+
@taxonomies = Spree::Taxonomy.includes(root: :children)
|
11
|
+
|
12
|
+
filters_init
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Spree::ProductsHelper.class_eval do
|
2
|
+
def cache_key_for_products
|
3
|
+
count = @products.count
|
4
|
+
first_id = @products.any? ? @products.first.id : nil
|
5
|
+
sorting = params[:sorting]
|
6
|
+
"#{I18n.locale}/#{current_currency}/spree/products/all-#{params[:page]}-#{first_id}-#{sorting}-#{count}"
|
7
|
+
end
|
8
|
+
|
9
|
+
def link_to_sort(key)
|
10
|
+
if current_sorting?(key)
|
11
|
+
klass = 'btn btn-primary btn-sm'
|
12
|
+
else
|
13
|
+
klass = 'btn btn-default btn-sm'
|
14
|
+
end
|
15
|
+
|
16
|
+
link_to Spree.t(key).html_safe, params.merge(sorting: key), class: klass
|
17
|
+
end
|
18
|
+
|
19
|
+
def current_sorting?(key)
|
20
|
+
param_sorting == key.to_sym
|
21
|
+
end
|
22
|
+
|
23
|
+
def filter_checked?(keys, value)
|
24
|
+
(params[:filters][keys[0]].present? && params[:filters][keys[0]][keys[1]].include?(value)) ? true : false
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,6 @@
|
|
1
|
+
Spree::Product.class_eval do
|
2
|
+
scope :with_property_values, -> (values_array) { joins(:properties).
|
3
|
+
where("#{Spree::ProductProperty.table_name}.value IN (?)", values_array)}
|
4
|
+
scope :with_option_values, -> (values_array) { joins(variants_including_master: :option_values).
|
5
|
+
where("#{Spree::OptionValue.table_name}.name IN (?)", values_array).distinct }
|
6
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
class AdvancedFilters
|
2
|
+
def initialize(params = {}, products)
|
3
|
+
@params = params
|
4
|
+
@products = products
|
5
|
+
end
|
6
|
+
|
7
|
+
def filter_results
|
8
|
+
@params[:filters] ||= {}
|
9
|
+
params_clone = @params[:filters].deep_dup.delete_if { |_query, value| value.blank? }
|
10
|
+
|
11
|
+
products = filters_merger(params_clone, @products).distinct
|
12
|
+
min_price = products.map(&:price).min.to_i
|
13
|
+
max_price = products.map(&:price).max.round.to_i
|
14
|
+
price_range = {min_range: params_clone[:min_price_range].present? ? params_clone[:min_price_range].to_i : min_price,
|
15
|
+
max_range: params_clone[:max_price_range].present? ? params_clone[:max_price_range].to_i : max_price,
|
16
|
+
collection_min_price: min_price,
|
17
|
+
collection_max_price: max_price}
|
18
|
+
properties = products.map(&:properties).flatten.uniq
|
19
|
+
properties = properties.blank? ? nil : allowed_properties(properties)
|
20
|
+
|
21
|
+
product_properties = products.map(&:product_properties).flatten.uniq.map(&:value).uniq
|
22
|
+
product_properties = product_properties.blank? ? nil : product_properties
|
23
|
+
|
24
|
+
result_hash = { properties: properties, product_properties: product_properties,
|
25
|
+
products: products, price_range: price_range }
|
26
|
+
|
27
|
+
if @params[:controller] == 'spree/taxons' && @params[:action] == 'show'
|
28
|
+
option_types = products.map(&:option_types).flatten.uniq
|
29
|
+
option_types = option_types.blank? ? nil : option_types
|
30
|
+
|
31
|
+
result_hash[:option_types] = option_types
|
32
|
+
end
|
33
|
+
|
34
|
+
result_hash
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def filters_merger(params, products)
|
40
|
+
if params[:min_price_range].present? && params[:max_price_range].present?
|
41
|
+
products = products.send('price_between', params[:min_price_range].to_i, params[:max_price_range].to_i)
|
42
|
+
end
|
43
|
+
|
44
|
+
if params[:option_types].present? && !params[:option_types][:names].blank?
|
45
|
+
products = products.with_option_values(params[:option_types][:names])
|
46
|
+
end
|
47
|
+
|
48
|
+
if params[:properties].present? && !params[:properties][:types].blank?
|
49
|
+
products = products.with_property_values(params[:properties][:types])
|
50
|
+
end
|
51
|
+
|
52
|
+
products
|
53
|
+
end
|
54
|
+
|
55
|
+
def allowed_properties(properties)
|
56
|
+
if Spree::FiltersConfiguration::Config.hidden_properties.any?
|
57
|
+
properties - Spree::Property.where(name: Spree::FiltersConfiguration::Config.hidden_properties)
|
58
|
+
else
|
59
|
+
properties
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
= form_tag '', method: :get, id: 'advanced_filters' do
|
2
|
+
-# Search request repeater
|
3
|
+
- if params[:keywords].present?
|
4
|
+
= hidden_field_tag :keywords, params[:keywords]
|
5
|
+
|
6
|
+
-# Price Range filter
|
7
|
+
%h4= Spree.t(:price_range_filter)
|
8
|
+
%p
|
9
|
+
= label_tag 'amount', Spree.t(:price_range)
|
10
|
+
%input#amount{readonly: '', type: '1', data: {currency: current_currency}}
|
11
|
+
#slider-range
|
12
|
+
%span.hidden#range_limits{data: @price_range }
|
13
|
+
= hidden_field_tag 'filters[min_price_range]', @price_range[:min_range], {id: :min_price_range}
|
14
|
+
= hidden_field_tag 'filters[max_price_range]', @price_range[:max_range], {id: :max_price_range}
|
15
|
+
- if @option_types.present?
|
16
|
+
= render 'spree/shared/options_filters', option_types: @option_types
|
17
|
+
- if @properties.present?
|
18
|
+
= render 'spree/shared/property_filters', properties: @properties, product_properties: @product_properties
|
19
|
+
|
20
|
+
= submit_tag 'Filter', class: 'btn btn-primary'
|
21
|
+
= link_to 'Clear filters', products_path, class: 'btn btn-primary'
|
22
|
+
%br
|
@@ -0,0 +1,10 @@
|
|
1
|
+
%h4= Spree.t(:options_filter)
|
2
|
+
- option_types.each do |option_type|
|
3
|
+
= label_tag option_type.presentation, "#{option_type.presentation}:", class: 'filters_label'
|
4
|
+
%br
|
5
|
+
- option_type.option_values.each do |option_value|
|
6
|
+
- is_checked = filter_checked?([:option_types, :names], option_value.name)
|
7
|
+
= check_box('filters[option_types]', 'names', { multiple: true, checked: is_checked }, option_value.name, nil)
|
8
|
+
= option_value.presentation
|
9
|
+
%br
|
10
|
+
%br
|
@@ -0,0 +1,15 @@
|
|
1
|
+
- if product_properties.present?
|
2
|
+
%h4= Spree.t(:property_filters)
|
3
|
+
- properties.each do |property|
|
4
|
+
= label_tag property.presentation, "#{property.presentation}:", class: 'filters_label'
|
5
|
+
%br
|
6
|
+
- property.product_properties.pluck(:value).uniq.each do |value|
|
7
|
+
- is_checked = filter_checked?([:properties, :types], value)
|
8
|
+
- if params[:filters].present? || (params[:controller] == 'spree/taxons' && params[:action] == 'show')
|
9
|
+
- if product_properties.include?(value)
|
10
|
+
= check_box('filters[properties]', 'types', { multiple: true, checked: is_checked }, value, nil)
|
11
|
+
= value
|
12
|
+
- else
|
13
|
+
= check_box('filters[properties]', 'types', { multiple: true, checked: is_checked }, value, nil)
|
14
|
+
= value
|
15
|
+
%br
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'spree_products_filters'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
Spree::Core::Search::Base.class_eval do
|
2
|
+
def custom_retrieve_products
|
3
|
+
@products = get_base_scope
|
4
|
+
|
5
|
+
unless Spree::Config.show_products_without_price
|
6
|
+
@products = @products.where('spree_prices.amount IS NOT NULL').where('spree_prices.currency' => current_currency)
|
7
|
+
end
|
8
|
+
|
9
|
+
@products
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
en:
|
2
|
+
spree:
|
3
|
+
price_range_filter: 'Price range filter:'
|
4
|
+
options_filter: 'Option types filters:'
|
5
|
+
property_filters: 'Properties filters:'
|
6
|
+
price_range: 'Price range:'
|
7
|
+
sort_by: 'Sort by:'
|
8
|
+
descend_by_updated_at: 'Latest'
|
9
|
+
ascend_by_master_price: "Low Price <i class='glyphicon glyphicon-sort-by-attributes'></i>"
|
10
|
+
descend_by_master_price: "High Price <i class='glyphicon glyphicon-sort-by-attributes-alt'></i>"
|
11
|
+
ascend_by_name: "A - Z <i class='glyphicon glyphicon-sort-by-alphabet'></i>"
|
12
|
+
descend_by_name: "Z - A <i class='glyphicon glyphicon-sort-by-alphabet-alt'></i>"
|
@@ -0,0 +1,12 @@
|
|
1
|
+
ru:
|
2
|
+
spree:
|
3
|
+
price_range_filter: 'Ценовой фильтр:'
|
4
|
+
options_filter: 'Опциональные фильтры:'
|
5
|
+
property_filters: 'Фильтры свойств товаров:'
|
6
|
+
price_range: 'Диапазон цен:'
|
7
|
+
sort_by: 'Сортировать:'
|
8
|
+
descend_by_updated_at: 'Новые'
|
9
|
+
ascend_by_master_price: "Дешевые <i class='glyphicon glyphicon-sort-by-attributes'></i>"
|
10
|
+
descend_by_master_price: "Дорогие <i class='glyphicon glyphicon-sort-by-attributes-alt'></i>"
|
11
|
+
ascend_by_name: "А - Я <i class='glyphicon glyphicon-sort-by-alphabet'></i>"
|
12
|
+
descend_by_name: "Я - A <i class='glyphicon glyphicon-sort-by-alphabet-alt'></i>"
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module SpreeProductsFilters
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
|
5
|
+
class_option :auto_run_migrations, type: :boolean, default: false
|
6
|
+
|
7
|
+
def add_javascripts
|
8
|
+
append_file 'vendor/assets/javascripts/spree/frontend/all.js',
|
9
|
+
"//= require spree/frontend/filters_all\n//= require jquery-ui\n"
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_stylesheets
|
13
|
+
inject_into_file 'vendor/assets/stylesheets/spree/frontend/all.css',
|
14
|
+
" *= require spree/frontend/filters_all\n *= require jquery-ui\n",
|
15
|
+
before: /\*\//, verbose: true
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_migrations
|
19
|
+
run 'bundle exec rake railties:install:migrations FROM=spree-products-filters'
|
20
|
+
end
|
21
|
+
|
22
|
+
def run_migrations
|
23
|
+
run_migrations = options[:auto_run_migrations] || ['', 'y', 'Y'].include?(ask 'Would you like to run the migrations now? [Y/n]')
|
24
|
+
if run_migrations
|
25
|
+
run 'bundle exec rake db:migrate'
|
26
|
+
else
|
27
|
+
puts 'Skipping rake db:migrate, don\'t forget to run it!'
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module SpreeProductsFilters
|
2
|
+
class Engine < ::Rails::Engine
|
3
|
+
require 'spree/core'
|
4
|
+
|
5
|
+
isolate_namespace Spree
|
6
|
+
|
7
|
+
engine_name 'spree_products_filters'
|
8
|
+
|
9
|
+
# use rspec for tests
|
10
|
+
config.generators do |g|
|
11
|
+
g.test_framework :rspec
|
12
|
+
end
|
13
|
+
|
14
|
+
initializer "spree.register.filters_configuration", before: :load_config_initializers do |app|
|
15
|
+
Spree::FiltersConfiguration::Config = Spree::FiltersConfiguration.new
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.activate
|
19
|
+
Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
|
20
|
+
Rails.configuration.cache_classes ? require(c) : load(c)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
config.to_prepare &method(:activate).to_proc
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'timecop'
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
RSpec.describe Spree::HomeController, type: :controller do
|
5
|
+
routes { Spree::Core::Engine.routes }
|
6
|
+
|
7
|
+
describe '#index' do
|
8
|
+
context 'price filter' do
|
9
|
+
let!(:cheap_product) { create :base_product, price: 10 }
|
10
|
+
let!(:expensive_product) { create :base_product, price: 100 }
|
11
|
+
|
12
|
+
it 'should return cheap product' do
|
13
|
+
get :index, params: { filters: { min_price_range: 5, max_price_range: 20 } }
|
14
|
+
|
15
|
+
expect(response.status).to eq(200)
|
16
|
+
expect(assigns[:products]).to eq [cheap_product]
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should return expensive product' do
|
20
|
+
get :index, params: { filters: { min_price_range: 50, max_price_range: 101 } }
|
21
|
+
|
22
|
+
expect(response.status).to eq(200)
|
23
|
+
expect(assigns[:products]).to eq [expensive_product]
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should return both products' do
|
27
|
+
get :index, params: { filters: { min_price_range: 9, max_price_range: 101 } }
|
28
|
+
|
29
|
+
expect(response.status).to eq(200)
|
30
|
+
expect(assigns[:products]).to eq [expensive_product, cheap_product]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context 'property filter' do
|
35
|
+
let! (:property) { create :property }
|
36
|
+
let! (:product_red) { create :base_product }
|
37
|
+
let! (:product_blue) { create :base_product }
|
38
|
+
let! (:property_blue) { create :product_property, product: product_blue, property: property, value: 'Blue' }
|
39
|
+
let! (:property_red) { create :product_property, product: product_red, property: property, value: 'Red' }
|
40
|
+
|
41
|
+
it 'should return product with property value' do
|
42
|
+
get :index, params: { filters: { properties: { types: ['Red'] } } }
|
43
|
+
|
44
|
+
expect(response.status).to eq(200)
|
45
|
+
expect(assigns[:products]).to eq [product_red]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context 'sorting' do
|
50
|
+
before { Spree::FiltersConfiguration::Config.allowed_sortings += [:descend_by_updated_at, :ascend_by_master_price, :descend_by_master_price, :ascend_by_name, :descend_by_name] }
|
51
|
+
let! (:product_red) { create :base_product, price: 10, name: 'Zulu' }
|
52
|
+
before { Timecop.travel Time.now + 5.minutes }
|
53
|
+
let! (:product_blue) { create :base_product, price: 20, name: 'Alpha' }
|
54
|
+
|
55
|
+
it 'should return products in order :descend_by_updated_at' do
|
56
|
+
get :index, params: { sorting: 'descend_by_updated_at' }
|
57
|
+
|
58
|
+
expect(response.status).to eq(200)
|
59
|
+
expect(assigns[:products]).to eq [product_blue, product_red]
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should return products in order :ascend_by_master_price' do
|
63
|
+
get :index, params: { sorting: 'ascend_by_master_price' }
|
64
|
+
|
65
|
+
expect(response.status).to eq(200)
|
66
|
+
expect(assigns[:products]).to eq [product_red, product_blue]
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'should return products in order :descend_by_master_price' do
|
70
|
+
get :index, params: { sorting: 'descend_by_master_price' }
|
71
|
+
|
72
|
+
expect(response.status).to eq(200)
|
73
|
+
expect(assigns[:products]).to eq [product_blue, product_red]
|
74
|
+
end
|
75
|
+
|
76
|
+
it 'should return products in order :ascend_by_name' do
|
77
|
+
get :index, params: { sorting: 'ascend_by_name' }
|
78
|
+
|
79
|
+
expect(response.status).to eq(200)
|
80
|
+
expect(assigns[:products]).to eq [product_blue, product_red]
|
81
|
+
end
|
82
|
+
|
83
|
+
it 'should return products in order :descend_by_name' do
|
84
|
+
get :index, params: { sorting: 'descend_by_name' }
|
85
|
+
|
86
|
+
expect(response.status).to eq(200)
|
87
|
+
expect(assigns[:products]).to eq [product_red, product_blue]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
RSpec.describe Spree::TaxonsController, type: :controller do
|
4
|
+
routes { Spree::Core::Engine.routes }
|
5
|
+
|
6
|
+
describe '#show' do
|
7
|
+
let!(:taxon) { create :taxon }
|
8
|
+
let!(:product_m_size) { create :base_product, price: 10 }
|
9
|
+
let!(:product_s_size) { create :base_product, price: 20 }
|
10
|
+
let!(:option_m) { create :option_value, name: 'size-m', presentation: 'M'}
|
11
|
+
let!(:option_s) { create :option_value, name: 'size-s', presentation: 'S'}
|
12
|
+
let!(:variant_m_size) { create :base_variant, product: product_m_size }
|
13
|
+
let!(:variant_s_size) { create :base_variant, product: product_s_size }
|
14
|
+
|
15
|
+
before do
|
16
|
+
product_m_size.taxons << taxon
|
17
|
+
product_s_size.taxons << taxon
|
18
|
+
product_m_size.option_types << option_m.option_type
|
19
|
+
product_s_size.option_types << option_s.option_type
|
20
|
+
variant_m_size.option_values << option_m
|
21
|
+
variant_s_size.option_values << option_s
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'should return product with option value' do
|
25
|
+
get :show, params: { id: taxon, filters: { option_types: { names: ['size-m'] } } }
|
26
|
+
|
27
|
+
expect(response.status).to eq(200)
|
28
|
+
expect(assigns[:products]).to eq [product_m_size]
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Configure Rails Environment
|
2
|
+
ENV['RAILS_ENV'] = 'test'
|
3
|
+
|
4
|
+
require File.expand_path('../dummy/config/environment.rb', __FILE__)
|
5
|
+
|
6
|
+
require 'rspec/rails'
|
7
|
+
require 'database_cleaner'
|
8
|
+
require 'ffaker'
|
9
|
+
require 'rails-controller-testing'
|
10
|
+
|
11
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
12
|
+
# in spec/support/ and its subdirectories.
|
13
|
+
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }
|
14
|
+
|
15
|
+
# Requires factories defined in spree_core
|
16
|
+
require 'spree/testing_support/factories'
|
17
|
+
require 'spree/testing_support/controller_requests'
|
18
|
+
require 'spree/testing_support/authorization_helpers'
|
19
|
+
require 'spree/testing_support/url_helpers'
|
20
|
+
|
21
|
+
|
22
|
+
RSpec.configure do |config|
|
23
|
+
config.include FactoryBot::Syntax::Methods
|
24
|
+
|
25
|
+
config.include Devise::TestHelpers, type: :controller
|
26
|
+
# == URL Helpers
|
27
|
+
#
|
28
|
+
# Allows access to Spree's routes in specs:
|
29
|
+
#
|
30
|
+
# visit spree.admin_path
|
31
|
+
# current_path.should eql(spree.products_path)
|
32
|
+
config.include Spree::TestingSupport::UrlHelpers
|
33
|
+
|
34
|
+
# == Mock Framework
|
35
|
+
#
|
36
|
+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
|
37
|
+
#
|
38
|
+
# config.mock_with :mocha
|
39
|
+
# config.mock_with :flexmock
|
40
|
+
# config.mock_with :rr
|
41
|
+
config.mock_with :rspec
|
42
|
+
config.color = true
|
43
|
+
|
44
|
+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
45
|
+
config.fixture_path = "#{::Rails.root}/spec/fixtures"
|
46
|
+
|
47
|
+
# Capybara javascript drivers require transactional fixtures set to false, and we use DatabaseCleaner
|
48
|
+
# to cleanup after each test instead. Without transactional fixtures set to false the records created
|
49
|
+
# to setup a test will be unavailable to the browser, which runs under a seperate server instance.
|
50
|
+
config.use_transactional_fixtures = false
|
51
|
+
|
52
|
+
# Ensure Suite is set to use transactions for speed.
|
53
|
+
config.before(:suite) do
|
54
|
+
DatabaseCleaner.clean_with :truncation
|
55
|
+
end
|
56
|
+
|
57
|
+
config.before(:each) do
|
58
|
+
if Capybara.current_driver == :rack_test
|
59
|
+
DatabaseCleaner.strategy = :transaction
|
60
|
+
else
|
61
|
+
DatabaseCleaner.strategy = :truncation
|
62
|
+
end
|
63
|
+
|
64
|
+
DatabaseCleaner.start
|
65
|
+
end
|
66
|
+
|
67
|
+
config.after(:each) do
|
68
|
+
DatabaseCleaner.clean
|
69
|
+
end
|
70
|
+
|
71
|
+
config.fail_fast = ENV['FAIL_FAST'] || false
|
72
|
+
|
73
|
+
Rails::Controller::Testing.install
|
74
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'spree_products_filters/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'spree_products_filters'
|
8
|
+
spec.version = SpreeProductsFilters::VERSION
|
9
|
+
spec.author = 'Jet Ruby Agency'
|
10
|
+
|
11
|
+
spec.summary = 'Advanced product filters for your Spree Commerce app'
|
12
|
+
spec.description = 'Advanced product filters for your Spree Commerce app'
|
13
|
+
spec.email = 'info@jetruby.com'
|
14
|
+
spec.homepage = 'http://jetruby.com/'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
spec.required_ruby_version = '>= 1.9.3'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0")
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
|
21
|
+
spec.require_path = 'lib'
|
22
|
+
spec.requirements << 'none'
|
23
|
+
|
24
|
+
spec.add_runtime_dependency 'spree_core', '~> 3.6.0'
|
25
|
+
spec.add_runtime_dependency 'haml'
|
26
|
+
spec.add_runtime_dependency 'jquery-ui-rails'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
29
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
30
|
+
|
31
|
+
spec.add_development_dependency 'factory_bot_rails', '~> 4.10.0'
|
32
|
+
spec.add_development_dependency 'database_cleaner'
|
33
|
+
spec.add_development_dependency 'sqlite3'
|
34
|
+
spec.add_development_dependency 'capybara', '~> 2.1'
|
35
|
+
spec.add_development_dependency 'coffee-rails'
|
36
|
+
spec.add_development_dependency 'ffaker'
|
37
|
+
spec.add_development_dependency 'rspec-rails', '~> 3.7.1'
|
38
|
+
spec.add_development_dependency 'rails-controller-testing'
|
39
|
+
spec.add_development_dependency 'selenium-webdriver'
|
40
|
+
spec.add_development_dependency 'sass-rails'
|
41
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spree_products_filters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jet Ruby Agency
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: spree_core
|
@@ -222,10 +222,54 @@ dependencies:
|
|
222
222
|
version: '0'
|
223
223
|
description: Advanced product filters for your Spree Commerce app
|
224
224
|
email: info@jetruby.com
|
225
|
-
executables:
|
225
|
+
executables:
|
226
|
+
- console
|
227
|
+
- setup
|
226
228
|
extensions: []
|
227
229
|
extra_rdoc_files: []
|
228
|
-
files:
|
230
|
+
files:
|
231
|
+
- ".gitignore"
|
232
|
+
- ".gitlab-ci.yml"
|
233
|
+
- ".rspec"
|
234
|
+
- Gemfile
|
235
|
+
- LICENSE.txt
|
236
|
+
- README.md
|
237
|
+
- Rakefile
|
238
|
+
- app/assets/javascripts/spree/frontend/filters.js
|
239
|
+
- app/assets/javascripts/spree/frontend/filters_all.js
|
240
|
+
- app/assets/stylesheets/spree/frontend/filters.css
|
241
|
+
- app/assets/stylesheets/spree/frontend/filters_all.css
|
242
|
+
- app/controllers/concerns/filters_initialization.rb
|
243
|
+
- app/controllers/spree/home_controller_decorator.rb
|
244
|
+
- app/controllers/spree/products_controller_decorator.rb
|
245
|
+
- app/controllers/spree/taxons_controller_decorator.rb
|
246
|
+
- app/helpers/spree/filters_helper.rb
|
247
|
+
- app/helpers/spree/products_helper_decorator.rb
|
248
|
+
- app/models/spree/filters_configuration.rb
|
249
|
+
- app/models/spree/product_decorator.rb
|
250
|
+
- app/overrides/spree/add_group_of_advanced_filters_to_home_index.rb
|
251
|
+
- app/overrides/spree/add_group_of_advanced_filters_to_products_index.rb
|
252
|
+
- app/overrides/spree/add_group_of_advanced_filters_to_taxon_show.rb
|
253
|
+
- app/overrides/spree/shared/add_sorting_links_in_products.rb
|
254
|
+
- app/services/advanced_filters.rb
|
255
|
+
- app/views/spree/shared/_advanced_filters.html.haml
|
256
|
+
- app/views/spree/shared/_options_filters.html.haml
|
257
|
+
- app/views/spree/shared/_property_filters.html.haml
|
258
|
+
- app/views/spree/shared/_sorting_bar.html.haml
|
259
|
+
- bin/console
|
260
|
+
- bin/setup
|
261
|
+
- config/initializers/core_search_custom_retrieve_products.rb
|
262
|
+
- config/locales/en.yml
|
263
|
+
- config/locales/ru.yml
|
264
|
+
- lib/generators/spree_products_filters/install/install_generator.rb
|
265
|
+
- lib/spree_products_filters.rb
|
266
|
+
- lib/spree_products_filters/engine.rb
|
267
|
+
- lib/spree_products_filters/version.rb
|
268
|
+
- spec/controllers/home_controller_spec.rb
|
269
|
+
- spec/controllers/taxons_controller_spec.rb
|
270
|
+
- spec/spec_helper.rb
|
271
|
+
- spec/spree/products/filters_spec.rb
|
272
|
+
- spree_products_filters.gemspec
|
229
273
|
homepage: http://jetruby.com/
|
230
274
|
licenses:
|
231
275
|
- MIT
|