solidus_simple_weight_calculator 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.rspec +1 -0
- data/Gemfile +2 -0
- data/LICENSE +20 -0
- data/README.md +56 -0
- data/Rakefile +15 -0
- data/Versionfile +2 -0
- data/app/models/spree/calculator/shipping/item_weight.rb +27 -0
- data/app/models/spree/calculator/shipping/simple_weight.rb +92 -0
- data/app/models/spree/variant_decorator.rb +8 -0
- data/config/locales/en.yml +8 -0
- data/config/locales/it.yml +9 -0
- data/config/routes.rb +3 -0
- data/lib/solidus_simple_weight_calculator.rb +2 -0
- data/lib/solidus_simple_weight_calculator/engine.rb +27 -0
- data/lib/solidus_simple_weight_calculator/factories.rb +6 -0
- data/script/rails +7 -0
- data/solidus_simple_weight_calculator.gemspec +31 -0
- data/spec/models/spree/calculator/shipping/item_weight_spec.rb +93 -0
- data/spec/models/spree/calculator/shipping/simple_weight_spec.rb +91 -0
- data/spec/spec_helper.rb +79 -0
- metadata +237 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 47efc76214bf56a754223f51767b539397d6a8d1
|
4
|
+
data.tar.gz: 569e0332cd9e2280b9cdfaf0254dbb152d3a01c6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d36f63549cc81552e0472dcb2edbd5c4141088cd6f0c219401d34c8262dfc0a936ddf55a97a5cd561ff37b463eaef0b7950e5bac8f3081b46709b51b1aedc8df
|
7
|
+
data.tar.gz: 9ba7755c66408b94e3160ef0968436c184ed8f29edc8b2268346ca190a68f37414e697aeb276c7f251aecb0c9a5a6bf9f71f88a48cdf8159cee05621fbd9749b
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2013 Alessandro Lepore
|
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,56 @@
|
|
1
|
+
Solidus Simple Weight Calculator
|
2
|
+
==============================
|
3
|
+
|
4
|
+
Includes some shipping costs calculator for Solidus Commerce.
|
5
|
+
|
6
|
+
SimpleWeight
|
7
|
+
============
|
8
|
+
It's based on the total weight of the package.
|
9
|
+
You simply specify a weight/price table in the textarea like this:
|
10
|
+
|
11
|
+
```
|
12
|
+
5:10
|
13
|
+
10.5:15
|
14
|
+
100:50.5
|
15
|
+
```
|
16
|
+
|
17
|
+
This means:
|
18
|
+
- up to 5kg cost is 10
|
19
|
+
- up to 10.5kg cost is 15
|
20
|
+
- up to 100kg cost is 50.5
|
21
|
+
- over 100kg this shipping method doesn't apply
|
22
|
+
|
23
|
+
An optional handling fee can also be added.
|
24
|
+
|
25
|
+
ItemWeight
|
26
|
+
==========
|
27
|
+
Similar to `SimpleWeight`, but shipping cost is calculated on *each single item* of the package, and them summed up.
|
28
|
+
Useful if each item is shipped in different pack.
|
29
|
+
|
30
|
+
|
31
|
+
Usage
|
32
|
+
=====
|
33
|
+
|
34
|
+
Add to your Gemfile
|
35
|
+
```ruby
|
36
|
+
gem 'solidus_simple_weight_calculator', github: 'dfranciosi/solidus_simple_weight_calculator', branch: 'master'
|
37
|
+
```
|
38
|
+
|
39
|
+
Create a shipping method from the admin interface and choose the right calculator.
|
40
|
+
|
41
|
+
Testing
|
42
|
+
-------
|
43
|
+
|
44
|
+
Be sure to bundle your dependencies and then create a dummy test app for the specs to run against.
|
45
|
+
|
46
|
+
$ bundle
|
47
|
+
$ bundle exec rake test_app
|
48
|
+
$ bundle exec rspec spec
|
49
|
+
|
50
|
+
Credits
|
51
|
+
=======
|
52
|
+
|
53
|
+
Inspired by https://github.com/dancinglightning/spree-postal-service .
|
54
|
+
|
55
|
+
|
56
|
+
Copyright (c) 2015 Alessandro Lepore, Diego Franciosi - Released under the MIT License
|
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
|
8
|
+
|
9
|
+
task :default => [:spec]
|
10
|
+
|
11
|
+
desc 'Generates a dummy app for testing'
|
12
|
+
task :test_app do
|
13
|
+
ENV['LIB_NAME'] = 'solidus_simple_weight_calculator'
|
14
|
+
Rake::Task['common:test_app'].invoke
|
15
|
+
end
|
data/Versionfile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module Spree
|
2
|
+
module Calculator::Shipping
|
3
|
+
class ItemWeight < SimpleWeight
|
4
|
+
preference :costs_string, :text, default: "25:7\n50:15\n100:25\n9999:45"
|
5
|
+
|
6
|
+
def self.description
|
7
|
+
Spree.t(:item_weight)
|
8
|
+
end
|
9
|
+
|
10
|
+
def compute_package(package)
|
11
|
+
content_items = package.contents
|
12
|
+
line_items_total = total(content_items)
|
13
|
+
handling_fee = preferred_handling_max > line_items_total ? preferred_handling_fee : 0.0
|
14
|
+
costs = costs_string_to_hash(clean_costs_string)
|
15
|
+
shipping_costs = 0.0
|
16
|
+
|
17
|
+
content_items.each do |item|
|
18
|
+
item_weight = item.variant.calculator_weight > 0.0 ? item.variant.calculator_weight : preferred_default_weight
|
19
|
+
weight_class = costs.keys.select { |w| item_weight <= w }.min
|
20
|
+
shipping_costs += costs[weight_class] * item.quantity
|
21
|
+
end
|
22
|
+
|
23
|
+
shipping_costs + handling_fee
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module Spree
|
2
|
+
module Calculator::Shipping
|
3
|
+
class SimpleWeight < ShippingCalculator
|
4
|
+
preference :costs_string, :text, default: "1:5\n2:7\n5:10\n10:15\n100:50"
|
5
|
+
preference :default_weight, :decimal, default: 1
|
6
|
+
preference :max_item_size, :decimal, default: 0
|
7
|
+
preference :handling_fee, :decimal, default: 0
|
8
|
+
preference :handling_max, :decimal, default: 0
|
9
|
+
|
10
|
+
def self.description
|
11
|
+
Spree.t(:simple_weight)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.register
|
15
|
+
super
|
16
|
+
end
|
17
|
+
|
18
|
+
def available?(package)
|
19
|
+
return false if !costs_string_valid? || order_overweight?(package.contents)
|
20
|
+
|
21
|
+
if preferred_max_item_size > 0
|
22
|
+
package.contents.each do |item|
|
23
|
+
return false if item_oversized?(item)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
true
|
28
|
+
end
|
29
|
+
|
30
|
+
def compute_package(package)
|
31
|
+
content_items = package.contents
|
32
|
+
line_items_total = total(content_items)
|
33
|
+
handling_fee = preferred_handling_max > line_items_total ? preferred_handling_fee : 0
|
34
|
+
|
35
|
+
total_weight = total_weight(content_items)
|
36
|
+
costs = costs_string_to_hash(clean_costs_string)
|
37
|
+
weight_class = costs.keys.select { |w| total_weight <= w }.min
|
38
|
+
shipping_costs = costs[weight_class]
|
39
|
+
|
40
|
+
shipping_costs ? shipping_costs + handling_fee : 0
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
def clean_costs_string
|
45
|
+
preferred_costs_string.strip
|
46
|
+
end
|
47
|
+
|
48
|
+
def costs_string_valid?
|
49
|
+
!clean_costs_string.empty? &&
|
50
|
+
clean_costs_string.count(':') > 0 &&
|
51
|
+
clean_costs_string.split(/\:|\n/).size.even? &&
|
52
|
+
clean_costs_string.split(/\:|\n/).all? { |s | s.strip.match(/^\d|\.+$/) }
|
53
|
+
end
|
54
|
+
|
55
|
+
def item_oversized?(item)
|
56
|
+
return false if preferred_max_item_size == 0
|
57
|
+
|
58
|
+
variant = item.variant
|
59
|
+
sizes = [ variant.width || 0, variant.depth || 0, variant.height || 0 ]
|
60
|
+
|
61
|
+
sizes.max > preferred_max_item_size
|
62
|
+
end
|
63
|
+
|
64
|
+
def order_overweight?(content_items)
|
65
|
+
total_weight = total_weight(content_items)
|
66
|
+
hash = costs_string_to_hash(clean_costs_string)
|
67
|
+
|
68
|
+
total_weight > hash.keys.max
|
69
|
+
end
|
70
|
+
|
71
|
+
def costs_string_to_hash(costs_string)
|
72
|
+
costs = {}
|
73
|
+
costs_string.split.each do |cost_string|
|
74
|
+
values = cost_string.strip.split(':')
|
75
|
+
costs[values[0].strip.to_f] = values[1].strip.to_f
|
76
|
+
end
|
77
|
+
|
78
|
+
costs
|
79
|
+
end
|
80
|
+
|
81
|
+
def total_weight(contents)
|
82
|
+
weight = 0
|
83
|
+
contents.each do |item|
|
84
|
+
item_weight = item.variant.calculator_weight > 0.0 ? item.variant.calculator_weight : preferred_default_weight
|
85
|
+
weight += item.quantity * item_weight
|
86
|
+
end
|
87
|
+
|
88
|
+
weight
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
en:
|
2
|
+
solidus:
|
3
|
+
simple_weight: Simple Weight
|
4
|
+
costs_string: Prices (max weight:costs, one per line)
|
5
|
+
max_item_size: Max size of one item (longest side)
|
6
|
+
handling_fee: Handling fee
|
7
|
+
handling_max: Amount, over which handling fee won't be applied
|
8
|
+
default_weight: Default weight, applied to products without weight
|
@@ -0,0 +1,9 @@
|
|
1
|
+
it:
|
2
|
+
solidus:
|
3
|
+
simple_weight: Peso Semplice
|
4
|
+
costs_string: Tabella dei costi (peso massimo:costo, uno per riga)
|
5
|
+
max_item_size: Dimensione massima accettata di un prodotto (lato più lungo)
|
6
|
+
handling_fee: Costi di confezionamento
|
7
|
+
handling_max: Spesa sopra la quale non si applicano i costi di confezionamento
|
8
|
+
default_weight: Peso di default (applicato ai prodotti senza peso)
|
9
|
+
item_weight: Peso Singolo Articolo
|
data/config/routes.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module SolidusSimpleWeightCalculator
|
2
|
+
class Engine < Rails::Engine
|
3
|
+
require 'spree/core'
|
4
|
+
isolate_namespace Spree
|
5
|
+
engine_name 'solidus_simple_weight_calculator'
|
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
|
+
def self.activate
|
15
|
+
Dir.glob(File.join(File.dirname(__FILE__), '../../app/**/*_decorator*.rb')) do |c|
|
16
|
+
Rails.configuration.cache_classes ? require(c) : load(c)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
initializer 'spree.register.calculators.simple_weight', after: 'spree.register.calculators' do |app|
|
21
|
+
app.config.spree.calculators.shipping_methods << Spree::Calculator::Shipping::SimpleWeight
|
22
|
+
app.config.spree.calculators.shipping_methods << Spree::Calculator::Shipping::ItemWeight
|
23
|
+
end
|
24
|
+
|
25
|
+
config.to_prepare &method(:activate).to_proc
|
26
|
+
end
|
27
|
+
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 'solidus_simple_weight_calculator/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_simple_weight_calculator/engine', __FILE__)
|
5
|
+
|
6
|
+
require 'rails/all'
|
7
|
+
require 'rails/engine/commands'
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
Gem::Specification.new do |s|
|
3
|
+
s.platform = Gem::Platform::RUBY
|
4
|
+
s.name = 'solidus_simple_weight_calculator'
|
5
|
+
s.version = '0.1.0'
|
6
|
+
s.summary = 'A Solidus shipping costs calculator based on total order weight'
|
7
|
+
s.required_ruby_version = '>= 1.9.3'
|
8
|
+
s.description = 'A Solidus shipping costs calculator based on total order weight'
|
9
|
+
s.authors = ['Alessandro Lepore', 'Diego Franciosi']
|
10
|
+
s.email = 'info@diegofranciosi.com'
|
11
|
+
s.license = 'MIT'
|
12
|
+
|
13
|
+
s.files = `git ls-files`.split("\n")
|
14
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
15
|
+
s.require_path = 'lib'
|
16
|
+
s.requirements << 'none'
|
17
|
+
|
18
|
+
s.add_dependency 'solidus_core', '~> 1.2'
|
19
|
+
|
20
|
+
s.add_development_dependency 'capybara', '~> 2.5.0'
|
21
|
+
s.add_development_dependency 'coffee-rails'
|
22
|
+
s.add_development_dependency 'database_cleaner', '<= 1.0.1'
|
23
|
+
s.add_development_dependency 'factory_girl', '~> 4.2'
|
24
|
+
s.add_development_dependency 'ffaker'
|
25
|
+
s.add_development_dependency 'rspec-rails', '~> 3.0'
|
26
|
+
s.add_development_dependency 'rspec-activemodel-mocks'
|
27
|
+
s.add_development_dependency 'sass-rails'
|
28
|
+
s.add_development_dependency 'selenium-webdriver'
|
29
|
+
s.add_development_dependency 'simplecov'
|
30
|
+
s.add_development_dependency 'sqlite3'
|
31
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
module Calculator::Shipping
|
5
|
+
describe ItemWeight, :type => :model do
|
6
|
+
|
7
|
+
options = { preferred_costs_string: "0.5:5\n1:10\n50:20\n100:50.3",
|
8
|
+
preferred_handling_max: 120,
|
9
|
+
preferred_handling_fee: 21.5,
|
10
|
+
preferred_max_item_size: 35,
|
11
|
+
preferred_default_weight: 1 }
|
12
|
+
|
13
|
+
let(:variant1) { build(:variant, weight: 5,
|
14
|
+
width: 1,
|
15
|
+
depth: 1,
|
16
|
+
height: 1,
|
17
|
+
price: 4) }
|
18
|
+
let(:variant2) { build(:variant, weight: 10,
|
19
|
+
width: 1,
|
20
|
+
depth: 1,
|
21
|
+
height: 1,
|
22
|
+
price: 6) }
|
23
|
+
let(:variant3) { build(:variant, weight: 0.0,
|
24
|
+
width: 1,
|
25
|
+
depth: 1,
|
26
|
+
height: 1,
|
27
|
+
price: 10) }
|
28
|
+
|
29
|
+
let(:package) do
|
30
|
+
build(:stock_package, variants_contents: { variant1 => 4, variant2 => 6 })
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:package2) do
|
34
|
+
build(:stock_package, variants_contents: { variant3 => 1 })
|
35
|
+
end
|
36
|
+
|
37
|
+
subject { ItemWeight.new(options) }
|
38
|
+
|
39
|
+
it "correctly select the default weight shipping price when no weight on the variant", :focus => true do
|
40
|
+
expect(subject.available?(package2)).to be true
|
41
|
+
expect(subject.compute_package(package2)).to eq 31.5 # 10 shipping + 21.5 handling
|
42
|
+
end
|
43
|
+
|
44
|
+
it "correctly calculates shipping when handling fee applies" do
|
45
|
+
expect(subject.available?(package)).to eq true
|
46
|
+
expect(subject.compute_package(package)).to eq 221.5 # 200 cost + 21.5 handling
|
47
|
+
end
|
48
|
+
|
49
|
+
it "correctly calculates shipping when handling fee does not apply" do
|
50
|
+
allow(subject).to receive(:preferred_handling_max).and_return(10)
|
51
|
+
|
52
|
+
expect(subject.available?(package)).to be true
|
53
|
+
expect(subject.compute_package(package)).to eq 200
|
54
|
+
end
|
55
|
+
|
56
|
+
it "does not apply to overweight order" do
|
57
|
+
allow(variant1).to receive(:weight).and_return(100)
|
58
|
+
|
59
|
+
expect(subject.available?(package)).to be false
|
60
|
+
end
|
61
|
+
|
62
|
+
it "does not apply to order with oversize items" do
|
63
|
+
allow(variant1).to receive(:depth).and_return(100)
|
64
|
+
|
65
|
+
expect(subject.available?(package)).to be false
|
66
|
+
end
|
67
|
+
|
68
|
+
it "does not apply with invalid costs string" do
|
69
|
+
allow(subject).to receive(:preferred_costs_string).and_return("")
|
70
|
+
expect(subject.available?(package)).to be false
|
71
|
+
|
72
|
+
allow(subject).to receive(:preferred_costs_string).and_return("20:")
|
73
|
+
expect(subject.available?(package)).to be false
|
74
|
+
|
75
|
+
allow(subject).to receive(:preferred_costs_string).and_return("50=20")
|
76
|
+
expect(subject.available?(package)).to be false
|
77
|
+
|
78
|
+
allow(subject).to receive(:preferred_costs_string).and_return("abc:dfg\nerge:67")
|
79
|
+
expect(subject.available?(package)).to be false
|
80
|
+
end
|
81
|
+
|
82
|
+
it "correctly calculates shipping when costs string has useless spaces and newlines" do
|
83
|
+
allow(subject).to receive(:preferred_costs_string).and_return(%q{50:20
|
84
|
+
100:50.3
|
85
|
+
})
|
86
|
+
expect(subject.available?(package)).to be true
|
87
|
+
expect(subject.compute_package(package)).to eq 221.5
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Spree
|
4
|
+
module Calculator::Shipping
|
5
|
+
describe SimpleWeight, :type => :model do
|
6
|
+
|
7
|
+
options = { preferred_costs_string: "0.5:5\n1:10\n50:20\n100:50.3",
|
8
|
+
preferred_handling_max: 120,
|
9
|
+
preferred_handling_fee: 21.5,
|
10
|
+
preferred_max_item_size: 35,
|
11
|
+
preferred_default_weight: 1 }
|
12
|
+
|
13
|
+
let(:variant1) { build(:variant, weight: 5,
|
14
|
+
width: 1,
|
15
|
+
depth: 1,
|
16
|
+
height: 1,
|
17
|
+
price: 4) }
|
18
|
+
let(:variant2) { build(:variant, weight: 10,
|
19
|
+
width: 1,
|
20
|
+
depth: 1,
|
21
|
+
height: 1,
|
22
|
+
price: 6) }
|
23
|
+
let(:variant3) { build(:variant, weight: 0.0,
|
24
|
+
width: 1,
|
25
|
+
depth: 1,
|
26
|
+
height: 1,
|
27
|
+
price: 10) }
|
28
|
+
|
29
|
+
let(:package) do
|
30
|
+
build(:stock_package, variants_contents: { variant1 => 4, variant2 => 6 })
|
31
|
+
end
|
32
|
+
|
33
|
+
let(:package2) do
|
34
|
+
build(:stock_package, variants_contents: { variant3 => 1 })
|
35
|
+
end
|
36
|
+
|
37
|
+
subject { SimpleWeight.new(options) }
|
38
|
+
|
39
|
+
it "correctly select the default weight shipping price when no weight on the variant", :focus => true do
|
40
|
+
expect(subject.available?(package2)).to be true
|
41
|
+
expect(subject.compute_package(package2)).to eq 31.5 # 10 shipping + 21.5 handling
|
42
|
+
end
|
43
|
+
|
44
|
+
it "correctly calculates shipping when handling fee applies" do
|
45
|
+
expect(subject.available?(package)).to be true
|
46
|
+
expect(subject.compute_package(package)).to eq 71.8 # 50.3 cost + 21.5 handling
|
47
|
+
end
|
48
|
+
|
49
|
+
it "correctly calculates shipping when handling fee does not apply" do
|
50
|
+
allow(subject).to receive(:preferred_handling_max).and_return(10)
|
51
|
+
|
52
|
+
expect(subject.available?(package)).to be true
|
53
|
+
expect(subject.compute_package(package)).to eq 50.3
|
54
|
+
end
|
55
|
+
|
56
|
+
it "does not apply to overweight order" do
|
57
|
+
allow(variant1).to receive(:calculator_weight).and_return(100)
|
58
|
+
|
59
|
+
expect(subject.available?(package)).to be false
|
60
|
+
end
|
61
|
+
|
62
|
+
it "does not apply to order with oversize items" do
|
63
|
+
allow(variant1).to receive(:depth).and_return(100)
|
64
|
+
|
65
|
+
expect(subject.available?(package)).to be false
|
66
|
+
end
|
67
|
+
|
68
|
+
it "does not apply with invalid costs string" do
|
69
|
+
allow(subject).to receive(:preferred_costs_string).and_return("")
|
70
|
+
expect(subject.available?(package)).to be false
|
71
|
+
|
72
|
+
allow(subject).to receive(:preferred_costs_string).and_return("20:")
|
73
|
+
expect(subject.available?(package)).to be false
|
74
|
+
|
75
|
+
allow(subject).to receive(:preferred_costs_string).and_return("50=20")
|
76
|
+
expect(subject.available?(package)).to be false
|
77
|
+
|
78
|
+
allow(subject).to receive(:preferred_costs_string).and_return("abc:dfg\nerge:67")
|
79
|
+
expect(subject.available?(package)).to be false
|
80
|
+
end
|
81
|
+
|
82
|
+
it "correctly calculates shipping when costs string has useless spaces and newlines" do
|
83
|
+
allow(subject).to receive(:preferred_costs_string).and_return(%q{50:20
|
84
|
+
100:50.3
|
85
|
+
})
|
86
|
+
expect(subject.available?(package)).to be true
|
87
|
+
expect(subject.compute_package(package)).to eq 71.8
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,79 @@
|
|
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 'rspec-activemodel-mocks'
|
19
|
+
require 'database_cleaner'
|
20
|
+
require 'ffaker'
|
21
|
+
|
22
|
+
# Requires supporting ruby files with custom matchers and macros, etc,
|
23
|
+
# in spec/support/ and its subdirectories.
|
24
|
+
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each { |f| require f }
|
25
|
+
|
26
|
+
# Requires factories defined in spree_core
|
27
|
+
require 'spree/testing_support/factories'
|
28
|
+
require 'spree/testing_support/controller_requests'
|
29
|
+
require 'spree/testing_support/authorization_helpers'
|
30
|
+
require 'spree/testing_support/url_helpers'
|
31
|
+
|
32
|
+
require 'solidus_simple_weight_calculator/factories'
|
33
|
+
|
34
|
+
RSpec.configure do |config|
|
35
|
+
config.include FactoryGirl::Syntax::Methods
|
36
|
+
|
37
|
+
# == URL Helpers
|
38
|
+
#
|
39
|
+
# Allows access to Spree's routes in specs:
|
40
|
+
#
|
41
|
+
# visit spree.admin_path
|
42
|
+
# current_path.should eql(spree.products_path)
|
43
|
+
config.include Spree::TestingSupport::UrlHelpers
|
44
|
+
|
45
|
+
# == Mock Framework
|
46
|
+
#
|
47
|
+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
|
48
|
+
#
|
49
|
+
# config.mock_with :mocha
|
50
|
+
# config.mock_with :flexmock
|
51
|
+
# config.mock_with :rr
|
52
|
+
config.mock_with :rspec
|
53
|
+
config.color = true
|
54
|
+
|
55
|
+
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
|
56
|
+
config.fixture_path = "#{::Rails.root}/spec/fixtures"
|
57
|
+
|
58
|
+
# Capybara javascript drivers require transactional fixtures set to false, and we use DatabaseCleaner
|
59
|
+
# to cleanup after each test instead. Without transactional fixtures set to false the records created
|
60
|
+
# to setup a test will be unavailable to the browser, which runs under a seperate server instance.
|
61
|
+
config.use_transactional_fixtures = false
|
62
|
+
|
63
|
+
# Ensure Suite is set to use transactions for speed.
|
64
|
+
config.before :suite do
|
65
|
+
DatabaseCleaner.strategy = :transaction
|
66
|
+
DatabaseCleaner.clean_with :truncation
|
67
|
+
end
|
68
|
+
|
69
|
+
config.before(:each) do |example|
|
70
|
+
DatabaseCleaner.strategy = example.metadata[:js] ? :deletion : :transaction
|
71
|
+
DatabaseCleaner.start
|
72
|
+
end
|
73
|
+
|
74
|
+
config.after(:each) do
|
75
|
+
DatabaseCleaner.clean
|
76
|
+
end
|
77
|
+
|
78
|
+
config.fail_fast = ENV['FAIL_FAST'] || false
|
79
|
+
end
|
metadata
ADDED
@@ -0,0 +1,237 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: solidus_simple_weight_calculator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alessandro Lepore
|
8
|
+
- Diego Franciosi
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-01-31 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: solidus_core
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '1.2'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '1.2'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: capybara
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.5.0
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 2.5.0
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: coffee-rails
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: database_cleaner
|
58
|
+
requirement: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - "<="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 1.0.1
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "<="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 1.0.1
|
70
|
+
- !ruby/object:Gem::Dependency
|
71
|
+
name: factory_girl
|
72
|
+
requirement: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - "~>"
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '4.2'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - "~>"
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '4.2'
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: ffaker
|
86
|
+
requirement: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
type: :development
|
92
|
+
prerelease: false
|
93
|
+
version_requirements: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
- !ruby/object:Gem::Dependency
|
99
|
+
name: rspec-rails
|
100
|
+
requirement: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - "~>"
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '3.0'
|
105
|
+
type: :development
|
106
|
+
prerelease: false
|
107
|
+
version_requirements: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - "~>"
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '3.0'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: rspec-activemodel-mocks
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
type: :development
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - ">="
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0'
|
126
|
+
- !ruby/object:Gem::Dependency
|
127
|
+
name: sass-rails
|
128
|
+
requirement: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
type: :development
|
134
|
+
prerelease: false
|
135
|
+
version_requirements: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: '0'
|
140
|
+
- !ruby/object:Gem::Dependency
|
141
|
+
name: selenium-webdriver
|
142
|
+
requirement: !ruby/object:Gem::Requirement
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
version: '0'
|
147
|
+
type: :development
|
148
|
+
prerelease: false
|
149
|
+
version_requirements: !ruby/object:Gem::Requirement
|
150
|
+
requirements:
|
151
|
+
- - ">="
|
152
|
+
- !ruby/object:Gem::Version
|
153
|
+
version: '0'
|
154
|
+
- !ruby/object:Gem::Dependency
|
155
|
+
name: simplecov
|
156
|
+
requirement: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
type: :development
|
162
|
+
prerelease: false
|
163
|
+
version_requirements: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: '0'
|
168
|
+
- !ruby/object:Gem::Dependency
|
169
|
+
name: sqlite3
|
170
|
+
requirement: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
type: :development
|
176
|
+
prerelease: false
|
177
|
+
version_requirements: !ruby/object:Gem::Requirement
|
178
|
+
requirements:
|
179
|
+
- - ">="
|
180
|
+
- !ruby/object:Gem::Version
|
181
|
+
version: '0'
|
182
|
+
description: A Solidus shipping costs calculator based on total order weight
|
183
|
+
email: info@diegofranciosi.com
|
184
|
+
executables: []
|
185
|
+
extensions: []
|
186
|
+
extra_rdoc_files: []
|
187
|
+
files:
|
188
|
+
- ".gitignore"
|
189
|
+
- ".rspec"
|
190
|
+
- Gemfile
|
191
|
+
- LICENSE
|
192
|
+
- README.md
|
193
|
+
- Rakefile
|
194
|
+
- Versionfile
|
195
|
+
- app/models/spree/calculator/shipping/item_weight.rb
|
196
|
+
- app/models/spree/calculator/shipping/simple_weight.rb
|
197
|
+
- app/models/spree/variant_decorator.rb
|
198
|
+
- config/locales/en.yml
|
199
|
+
- config/locales/it.yml
|
200
|
+
- config/routes.rb
|
201
|
+
- lib/solidus_simple_weight_calculator.rb
|
202
|
+
- lib/solidus_simple_weight_calculator/engine.rb
|
203
|
+
- lib/solidus_simple_weight_calculator/factories.rb
|
204
|
+
- script/rails
|
205
|
+
- solidus_simple_weight_calculator.gemspec
|
206
|
+
- spec/models/spree/calculator/shipping/item_weight_spec.rb
|
207
|
+
- spec/models/spree/calculator/shipping/simple_weight_spec.rb
|
208
|
+
- spec/spec_helper.rb
|
209
|
+
homepage:
|
210
|
+
licenses:
|
211
|
+
- MIT
|
212
|
+
metadata: {}
|
213
|
+
post_install_message:
|
214
|
+
rdoc_options: []
|
215
|
+
require_paths:
|
216
|
+
- lib
|
217
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
218
|
+
requirements:
|
219
|
+
- - ">="
|
220
|
+
- !ruby/object:Gem::Version
|
221
|
+
version: 1.9.3
|
222
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
223
|
+
requirements:
|
224
|
+
- - ">="
|
225
|
+
- !ruby/object:Gem::Version
|
226
|
+
version: '0'
|
227
|
+
requirements:
|
228
|
+
- none
|
229
|
+
rubyforge_project:
|
230
|
+
rubygems_version: 2.4.6
|
231
|
+
signing_key:
|
232
|
+
specification_version: 4
|
233
|
+
summary: A Solidus shipping costs calculator based on total order weight
|
234
|
+
test_files:
|
235
|
+
- spec/models/spree/calculator/shipping/item_weight_spec.rb
|
236
|
+
- spec/models/spree/calculator/shipping/simple_weight_spec.rb
|
237
|
+
- spec/spec_helper.rb
|