spree_zero_stock_products 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ \#*
2
+ *~
3
+ .#*
4
+ .DS_Store
5
+ .idea
6
+ .project
7
+ .sass-cache
8
+ coverage
9
+ Gemfile.lock
10
+ tmp
11
+ nbproject
12
+ pkg
13
+ *.swp
14
+ spec/dummy
15
+ .ruby*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,15 @@
1
+ before_script:
2
+ - "bundle exec rake test_app"
3
+ script:
4
+ - "bundle exec rspec spec"
5
+ rvm:
6
+ - 1.9.3
7
+ - 2.0.0
8
+ - ruby-head
9
+ language: ruby
10
+ matrix:
11
+ allow_failures:
12
+ - rvm: ruby-head
13
+ notifications:
14
+ recipients:
15
+ - swrobel@gmail.com
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Stefan Wrobel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ 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, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ SpreeZeroStockProducts [![Build Status](https://travis-ci.org/swrobel/spree_zero_stock_products.png?branch=2-0-stable)](https://travis-ci.org/swrobel/spree_zero_stock_products)
2
+ ======================
3
+
4
+ Restore the `show_zero_stock_products` preference & related functionality in Spree 2.0+
5
+
6
+ The preference defaults to true, which is the out-of-the-box behavior in Spree 2.0+
7
+
8
+ Set it to `false` to avoid showing products with zero stock on any product listing/taxon pages.
9
+
10
+ Installation
11
+ ------------
12
+
13
+ Add spree_zero_stock_products to your Gemfile:
14
+
15
+ ```ruby
16
+ gem 'spree_zero_stock_products'
17
+ ```
18
+
19
+ Bundle your dependencies:
20
+
21
+ ```shell
22
+ bundle
23
+ ```
24
+
25
+ Set the preference in an intializer such as `config/initializers/spree.rb`:
26
+
27
+ ```ruby
28
+ Spree.config do |config|
29
+ config.show_zero_stock_products = false # Default is true
30
+ end
31
+ ```
32
+
33
+ Profit.
34
+
35
+ Testing
36
+ -------
37
+
38
+ Be sure to bundle your dependencies and then create a dummy test app for the specs to run against.
39
+
40
+ ```shell
41
+ bundle
42
+ bundle exec rake test_app
43
+ bundle exec rspec spec
44
+ ```
45
+
46
+ License
47
+ -------
48
+
49
+ Copyright (c) 2013 Stefan Wrobel, released under the MIT License
data/Rakefile ADDED
@@ -0,0 +1,18 @@
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
8
+
9
+ task :default => [:spec]
10
+
11
+ desc 'Generates a dummy app for testing'
12
+ task :test_app do
13
+ ENV['LIB_NAME'] = 'spree_zero_stock_products'
14
+
15
+ # use common:test_app intstead of extension:test_app
16
+ # because the extension version includes spree_frontend, which we don't need
17
+ Rake::Task['common:test_app'].invoke
18
+ end
data/Versionfile ADDED
@@ -0,0 +1,13 @@
1
+ # This file is used to designate compatibilty with different versions of Spree
2
+ # Please see http://spreecommerce.com/documentation/extensions.html#versionfile for details
3
+
4
+ # Examples
5
+ #
6
+ # '1.2.x' => { :branch => 'master' }
7
+ # '1.1.x' => { :branch => '1-1-stable' }
8
+ # '1.0.x' => { :branch => '1-0-stable' }
9
+ # '0.70.x' => { :branch => '0-70-stable' }
10
+ # '0.40.x' => { :tag => 'v1.0.0', :version => '1.0.0' }
11
+
12
+ "2.1.x" => { :branch => "master" }
13
+ "2.0.x" => { :branch => "2-0-stable" }
@@ -0,0 +1,5 @@
1
+ Spree::Product.class_eval do
2
+ add_search_scope :on_hand do
3
+ where("#{table_name}.id IN (SELECT product_id FROM #{Spree::Variant.table_name} v JOIN #{Spree::StockItem.table_name} i ON v.id = i.variant_id WHERE deleted_at IS NULL GROUP BY product_id HAVING SUM(count_on_hand) > 0)")
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ Spree::Core::Search::Base.class_eval do
2
+
3
+ protected
4
+ def get_base_scope
5
+ base_scope = Spree::Product.active
6
+ base_scope = base_scope.in_taxon(taxon) unless taxon.blank?
7
+ base_scope = get_products_conditions_for(base_scope, keywords)
8
+ base_scope = base_scope.on_hand unless Spree::Config[:show_zero_stock_products]
9
+ base_scope = add_search_scopes(base_scope)
10
+ base_scope
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ Spree::Taxon.class_eval do
2
+ def active_products
3
+ scope = products.active
4
+ scope = scope.on_hand unless Spree::Config[:show_zero_stock_products]
5
+ scope
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ # Sample localization file for English. Add more files in this directory for other locales.
2
+ # See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points.
3
+
4
+ en:
5
+ show_out_of_stock_products: "Show out-of-stock products"
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Spree::Core::Engine.routes.draw do
2
+ # Add your extension routes here
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'spree_core'
2
+ require 'spree_zero_stock_products/engine'
@@ -0,0 +1,28 @@
1
+ module SpreeZeroStockProducts
2
+ class Engine < Rails::Engine
3
+ require 'spree/core'
4
+ isolate_namespace Spree
5
+ engine_name 'spree_zero_stock_products'
6
+
7
+ config.autoload_paths += %W(#{config.root}/lib)
8
+
9
+ # use rspec for tests
10
+ config.generators do |g|
11
+ g.test_framework :rspec
12
+ end
13
+
14
+ initializer "spree.zero_stock_products.preferences", :before => :load_config_initializers do |app|
15
+ Spree::AppConfiguration.class_eval do
16
+ preference :show_zero_stock_products, :boolean, :default => true
17
+ end
18
+ end
19
+
20
+ def self.activate
21
+ Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
22
+ Rails.configuration.cache_classes ? require(c) : load(c)
23
+ end
24
+ end
25
+
26
+ config.to_prepare &method(:activate).to_proc
27
+ end
28
+ end
@@ -0,0 +1,6 @@
1
+ FactoryGirl.define do
2
+ # Define your Spree extensions Factories within this file to enable applications, and other extensions to use and override them.
3
+ #
4
+ # Example adding this to your spec_helper will load these Factories for use:
5
+ # require 'spree_zero_stock_products/factories'
6
+ end
data/script/rails ADDED
@@ -0,0 +1,7 @@
1
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
2
+
3
+ ENGINE_ROOT = File.expand_path('../..', __FILE__)
4
+ ENGINE_PATH = File.expand_path('../../lib/spree_zero_stock_products/engine', __FILE__)
5
+
6
+ require 'rails/all'
7
+ require 'rails/engine/commands'
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Core::Search::Base do
4
+
5
+ before do
6
+ stock_location = create(:stock_location)
7
+ @product_with_stock = create(:product)
8
+ @product_out_of_stock = create(:product)
9
+
10
+ stock_location.stock_items.where(:variant_id => @product_with_stock.master.id).first.adjust_count_on_hand(10)
11
+ stock_location.stock_items.where(:variant_id => @product_out_of_stock.master.id).first.adjust_count_on_hand(0)
12
+ end
13
+
14
+ context "with show_zero_stock_products = FALSE" do
15
+
16
+ before do
17
+ Spree::Config[:show_zero_stock_products] = false
18
+ end
19
+
20
+ it "returns all products by default" do
21
+ params = { :per_page => "" }
22
+ searcher = Spree::Core::Search::Base.new(params)
23
+ searcher.retrieve_products.count.should == 1
24
+ end
25
+ end
26
+
27
+ context "with show_zero_stock_products = TRUE" do
28
+
29
+ before do
30
+ Spree::Config[:show_zero_stock_products] = true
31
+ end
32
+
33
+ it "returns all products by default" do
34
+ params = { :per_page => "" }
35
+ searcher = Spree::Core::Search::Base.new(params)
36
+ searcher.retrieve_products.count.should == 2
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "Product scopes" do
4
+
5
+ context "with scope on_hand" do
6
+ before do
7
+ stock_location = create(:stock_location)
8
+ @product_with_stock = create(:product)
9
+ @product_out_of_stock = create(:product)
10
+
11
+ stock_location.stock_items.where(:variant_id => @product_with_stock.master.id).first.adjust_count_on_hand(10)
12
+ stock_location.stock_items.where(:variant_id => @product_out_of_stock.master.id).first.adjust_count_on_hand(0)
13
+ end
14
+
15
+ it "includes a product with available stock" do
16
+ Spree::Product.on_hand.should include @product_with_stock
17
+ end
18
+
19
+ it "excludes a product without available stock" do
20
+ Spree::Product.on_hand.should_not include @product_out_of_stock
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+
3
+ describe Spree::Taxon do
4
+
5
+ before do
6
+ stock_location = create(:stock_location)
7
+ @product_with_stock = create(:product)
8
+ @product_out_of_stock = create(:product)
9
+
10
+ stock_location.stock_items.where(:variant_id => @product_with_stock.master.id).first.adjust_count_on_hand(10)
11
+ stock_location.stock_items.where(:variant_id => @product_out_of_stock.master.id).first.adjust_count_on_hand(0)
12
+
13
+ @taxonomy = create(:taxonomy)
14
+ @root_taxon = @taxonomy.root
15
+
16
+ @parent_taxon = create(:taxon, :name => 'Parent', :taxonomy_id => @taxonomy.id, :parent => @root_taxon)
17
+ @child_taxon = create(:taxon, :name =>'Child 1', :taxonomy_id => @taxonomy.id, :parent => @parent_taxon)
18
+ @parent_taxon.reload # Need to reload for descendents to show up
19
+
20
+ @product_with_stock.taxons << @parent_taxon
21
+ @product_with_stock.taxons << @child_taxon
22
+ @product_out_of_stock.taxons << @parent_taxon
23
+ @product_out_of_stock.taxons << @child_taxon
24
+ end
25
+
26
+ context "with show_zero_stock_products = FALSE" do
27
+
28
+ before do
29
+ Spree::Config[:show_zero_stock_products] = false
30
+ end
31
+
32
+ it "includes a product with available stock" do
33
+ @parent_taxon.active_products.should include @product_with_stock
34
+ end
35
+
36
+ it "excludes a product without available stock" do
37
+ @parent_taxon.active_products.should_not include @product_out_of_stock
38
+ end
39
+ end
40
+
41
+ context "with show_zero_stock_products = TRUE" do
42
+
43
+ before do
44
+ Spree::Config[:show_zero_stock_products] = true
45
+ end
46
+
47
+ it "includes both products with and without stock" do
48
+ @parent_taxon.active_products.should include @product_with_stock
49
+ @parent_taxon.active_products.should include @product_out_of_stock
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,70 @@
1
+ # Run Coverage report
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_group 'Controllers', 'app/controllers'
5
+ add_group 'Helpers', 'app/helpers'
6
+ add_group 'Mailers', 'app/mailers'
7
+ add_group 'Models', 'app/models'
8
+ add_group 'Views', 'app/views'
9
+ add_group 'Libraries', 'lib'
10
+ end
11
+
12
+ # Configure Rails Environment
13
+ ENV['RAILS_ENV'] = 'test'
14
+
15
+ require File.expand_path('../dummy/config/environment.rb', __FILE__)
16
+
17
+ require 'rspec/rails'
18
+ require 'database_cleaner'
19
+ require 'ffaker'
20
+
21
+ # Requires supporting ruby files with custom matchers and macros, etc,
22
+ # in spec/support/ and its subdirectories.
23
+ Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }
24
+
25
+ # Requires factories defined in spree_core
26
+ require 'spree/testing_support/factories'
27
+
28
+ # Requires factories defined in lib/spree_zero_stock_products/factories.rb
29
+ require 'spree_zero_stock_products/factories'
30
+
31
+ RSpec.configure do |config|
32
+ config.include FactoryGirl::Syntax::Methods
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.strategy = :transaction
55
+ DatabaseCleaner.clean_with :truncation
56
+ end
57
+
58
+ # Before each spec check if it is a Javascript test and switch between using database transactions or not where necessary.
59
+ config.before :each do
60
+ DatabaseCleaner.strategy = example.metadata[:js] ? :truncation : :transaction
61
+ DatabaseCleaner.start
62
+ end
63
+
64
+ # After each spec clean the database.
65
+ config.after :each do
66
+ DatabaseCleaner.clean
67
+ end
68
+
69
+ config.fail_fast = ENV['FAIL_FAST'] || false
70
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: UTF-8
2
+ Gem::Specification.new do |s|
3
+ s.platform = Gem::Platform::RUBY
4
+ s.name = 'spree_zero_stock_products'
5
+ s.version = '2.0.0'
6
+ s.summary = 'Restore show_zero_stock_products functionality in Spree 2.0+'
7
+ s.description = 'Restore show_zero_stock_products functionality in Spree 2.0+'
8
+ s.required_ruby_version = '>= 1.9.3'
9
+
10
+ s.author = 'Stefan Wrobel'
11
+ s.email = 'swrobel@gmail.com'
12
+ s.homepage = 'https://github.com/swrobel/spree_zero_stock_products'
13
+ s.license = 'MIT'
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.require_path = 'lib'
18
+ s.requirements << 'none'
19
+
20
+ s.add_dependency 'spree_core', '~> 2.0.0'
21
+
22
+ s.add_development_dependency 'database_cleaner', '< 1.1.0' # >= 1.1.0 is broken w/ SQLite3 https://github.com/bmabey/database_cleaner/issues/224
23
+ s.add_development_dependency 'factory_girl', '~> 4.2'
24
+ s.add_development_dependency 'ffaker'
25
+ s.add_development_dependency 'rspec-rails', '~> 2.13'
26
+ s.add_development_dependency 'simplecov'
27
+ s.add_development_dependency 'sqlite3'
28
+ end
metadata ADDED
@@ -0,0 +1,185 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spree_zero_stock_products
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 2.0.0
6
+ platform: ruby
7
+ authors:
8
+ - Stefan Wrobel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: spree_core
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 2.0.0
22
+ requirement: !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: 2.0.0
28
+ type: :runtime
29
+ prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: database_cleaner
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - <
36
+ - !ruby/object:Gem::Version
37
+ version: 1.1.0
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - <
42
+ - !ruby/object:Gem::Version
43
+ version: 1.1.0
44
+ type: :development
45
+ prerelease: false
46
+ - !ruby/object:Gem::Dependency
47
+ name: factory_girl
48
+ version_requirements: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '4.2'
54
+ requirement: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ version: '4.2'
60
+ type: :development
61
+ prerelease: false
62
+ - !ruby/object:Gem::Dependency
63
+ name: ffaker
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirement: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec-rails
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '2.13'
86
+ requirement: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ~>
90
+ - !ruby/object:Gem::Version
91
+ version: '2.13'
92
+ type: :development
93
+ prerelease: false
94
+ - !ruby/object:Gem::Dependency
95
+ name: simplecov
96
+ version_requirements: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirement: !ruby/object:Gem::Requirement
103
+ none: false
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ type: :development
109
+ prerelease: false
110
+ - !ruby/object:Gem::Dependency
111
+ name: sqlite3
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirement: !ruby/object:Gem::Requirement
119
+ none: false
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ description: Restore show_zero_stock_products functionality in Spree 2.0+
127
+ email: swrobel@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - .gitignore
133
+ - .rspec
134
+ - .travis.yml
135
+ - Gemfile
136
+ - LICENSE
137
+ - README.md
138
+ - Rakefile
139
+ - Versionfile
140
+ - app/models/product_decorator.rb
141
+ - app/models/search/base_decorator.rb
142
+ - app/models/taxon_decorator.rb
143
+ - config/locales/en.yml
144
+ - config/routes.rb
145
+ - lib/spree_zero_stock_products.rb
146
+ - lib/spree_zero_stock_products/engine.rb
147
+ - lib/spree_zero_stock_products/factories.rb
148
+ - script/rails
149
+ - spec/lib/search/base_spec.rb
150
+ - spec/models/spree/product/scopes_spec.rb
151
+ - spec/models/spree/taxon/taxon_spec.rb
152
+ - spec/spec_helper.rb
153
+ - spree_zero_stock_products.gemspec
154
+ homepage: https://github.com/swrobel/spree_zero_stock_products
155
+ licenses:
156
+ - MIT
157
+ post_install_message:
158
+ rdoc_options: []
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: 1.9.3
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ none: false
169
+ requirements:
170
+ - - ! '>='
171
+ - !ruby/object:Gem::Version
172
+ version: '0'
173
+ requirements:
174
+ - none
175
+ rubyforge_project:
176
+ rubygems_version: 1.8.25
177
+ signing_key:
178
+ specification_version: 3
179
+ summary: Restore show_zero_stock_products functionality in Spree 2.0+
180
+ test_files:
181
+ - spec/lib/search/base_spec.rb
182
+ - spec/models/spree/product/scopes_spec.rb
183
+ - spec/models/spree/taxon/taxon_spec.rb
184
+ - spec/spec_helper.rb
185
+ has_rdoc: