spree_additional_calculators 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.md +6 -1
- data/Rakefile +3 -1
- data/Versionfile +1 -0
- data/app/models/additional_calculator/weight_and_quantity.rb +19 -14
- data/app/models/additional_calculator_rate.rb +7 -3
- data/spec.opts +1 -0
- data/spec/app/models/additional_calculator/weight_and_quantity_spec.rb +299 -0
- data/spec/app/models/additional_calculator_rate_spec.rb +146 -1
- data/spec/factories.rb +180 -0
- data/spec/spec_helper.rb +1 -0
- data/spree_additional_calculators.gemspec +1 -1
- metadata +34 -34
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -49,10 +49,15 @@ You can test the extension by executing following commands:
|
|
49
49
|
$ rake
|
50
50
|
|
51
51
|
|
52
|
+
Screenshots
|
53
|
+
-----------
|
54
|
+
|
55
|
+
There are some [screenshots](https://github.com/jurgis/spree-additional-calculators/wiki/Screenshots) in the wiki.
|
56
|
+
|
57
|
+
|
52
58
|
TODO
|
53
59
|
----
|
54
60
|
|
55
|
-
* Add RSpec tests
|
56
61
|
* Possibly add some cucumber scenarios
|
57
62
|
* Possibly use unobtrusive javascript
|
58
63
|
|
data/Rakefile
CHANGED
@@ -12,7 +12,9 @@ if File.exists?(gemfile) && (%w(spec cucumber).include?(ARGV.first.to_s) || ARGV
|
|
12
12
|
|
13
13
|
require 'rspec'
|
14
14
|
require 'rspec/core/rake_task'
|
15
|
-
RSpec::Core::RakeTask.new
|
15
|
+
RSpec::Core::RakeTask.new do |t|
|
16
|
+
t.rspec_opts = %w{--color --format doc}
|
17
|
+
end
|
16
18
|
|
17
19
|
require 'cucumber/rake/task'
|
18
20
|
Cucumber::Rake::Task.new do |t|
|
data/Versionfile
CHANGED
@@ -13,31 +13,36 @@ class AdditionalCalculator::WeightAndQuantity < AdditionalCalculator::Base
|
|
13
13
|
return nil if line_items.nil?
|
14
14
|
|
15
15
|
total_qnty = get_total_qnty(line_items)
|
16
|
-
|
16
|
+
total_weight = get_total_weight(line_items)
|
17
|
+
weight_rate = get_rate(total_weight, AdditionalCalculatorRate::WEIGHT)
|
17
18
|
|
18
|
-
#
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
# end
|
19
|
+
# sometimes the compute method is called without checking if the calculator is available
|
20
|
+
if weight_rate.nil?
|
21
|
+
logger.warn("The calculator's #{name} weight_rate is nil - returning. Availability is not checked!")
|
22
|
+
return nil
|
23
|
+
end
|
24
24
|
|
25
25
|
# quantity rate might be nil if the specified range is not available
|
26
26
|
qnty_rate = get_rate(total_qnty, AdditionalCalculatorRate::QNTY)
|
27
27
|
# find the previous qnty rate or set it to 0 if not found
|
28
|
-
|
28
|
+
previous_qnty_rate = get_previous_rate(total_qnty, AdditionalCalculatorRate::QNTY)
|
29
29
|
|
30
30
|
# the total rate is sum of weight and quantity rates
|
31
|
-
|
31
|
+
# the rate is available if
|
32
|
+
# 1) qnty_rate is not nil
|
33
|
+
# 2) qnty_rate is nil AND previous_qnty_rate is nil (quantity configuration not defined at all)
|
34
|
+
if !qnty_rate.nil?
|
35
|
+
weight_rate + qnty_rate
|
36
|
+
elsif previous_qnty_rate.nil?
|
37
|
+
weight_rate
|
38
|
+
else
|
39
|
+
nil
|
40
|
+
end
|
32
41
|
end
|
33
42
|
|
34
43
|
# check if this calculator is available for the Order
|
35
44
|
def available?(object)
|
36
|
-
|
37
|
-
return false if line_items.nil?
|
38
|
-
|
39
|
-
weight_rate = get_rate(get_total_weight(line_items), AdditionalCalculatorRate::WEIGHT)
|
40
|
-
!weight_rate.nil? # available only if the weight rate is not nil
|
45
|
+
!self.compute(object).nil?
|
41
46
|
end
|
42
47
|
|
43
48
|
protected
|
@@ -12,8 +12,12 @@ class AdditionalCalculatorRate < ActiveRecord::Base
|
|
12
12
|
|
13
13
|
validates :calculator_id, :rate_type, :from_value, :to_value, :rate, :presence => true
|
14
14
|
validates :from_value, :to_value, :rate, :numericality => true, :allow_blank => true
|
15
|
+
validate :validate_from_value_smaller_than_to_value
|
16
|
+
|
17
|
+
def validate_from_value_smaller_than_to_value
|
18
|
+
# ignore following cases
|
19
|
+
return if from_value.nil? || to_value.nil?
|
15
20
|
|
16
|
-
def validate
|
17
21
|
errors.add(:base, I18n.t('errors.from_value_greater_than_to_value')) if from_value > to_value
|
18
22
|
end
|
19
23
|
|
@@ -24,14 +28,14 @@ class AdditionalCalculatorRate < ActiveRecord::Base
|
|
24
28
|
|
25
29
|
# Find the rate for the specified value
|
26
30
|
def self.find_rate(calculator_id, rate_type, value)
|
27
|
-
# get the lowes rate if multiple rates are defined (
|
31
|
+
# get the lowes rate if multiple rates are defined (overlap)
|
28
32
|
rate = for_calculator(calculator_id).for_type(rate_type).for_value(value).order("rate").first()
|
29
33
|
rate.nil? ? nil : rate.rate
|
30
34
|
end
|
31
35
|
|
32
36
|
# Find the previous rate for the specified value
|
33
37
|
def self.find_previous_rate(calculator_id, rate_type, value)
|
34
|
-
rate = for_calculator(calculator_id).for_type(rate_type).where("to_value
|
38
|
+
rate = for_calculator(calculator_id).for_type(rate_type).where("to_value < ?", value).order("rate DESC").first()
|
35
39
|
rate.nil? ? nil : rate.rate
|
36
40
|
end
|
37
41
|
end
|
data/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
@@ -0,0 +1,299 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe AdditionalCalculator::WeightAndQuantity do
|
4
|
+
before(:all) do
|
5
|
+
@shipping_method = Factory(:shipping_method)
|
6
|
+
@calculator = Factory(:weight_and_quantity_calculator)
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@weight_rate1 = Factory.build(:additional_calculator_rate_for_weight, :from_value => 0, :to_value => 100, :rate => 100)
|
11
|
+
@weight_rate2 = Factory.build(:additional_calculator_rate_for_weight, :from_value => 100.1, :to_value => 500, :rate => 500)
|
12
|
+
@qnty_rate1 = Factory.build(:additional_calculator_rate_for_qnty, :from_value => 1, :to_value => 10, :rate => 10)
|
13
|
+
@qnty_rate2 = Factory.build(:additional_calculator_rate_for_qnty, :from_value => 11, :to_value => 20, :rate => 20)
|
14
|
+
@qnty_rate3 = Factory.build(:additional_calculator_rate_for_qnty, :from_value => 21, :to_value => 50, :rate => 50)
|
15
|
+
@empty_order = Factory(:order)
|
16
|
+
@order_with_one_item = Factory(:order_with_one_item)
|
17
|
+
@order_with_items_without_weight = Factory(:order_with_items_without_weight)
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
context "there are no items in the order" do
|
22
|
+
before(:each) do
|
23
|
+
@empty_order.item_count.should == 0
|
24
|
+
end
|
25
|
+
|
26
|
+
it "the rate should be nil" do
|
27
|
+
rate = @calculator.compute(@empty_order)
|
28
|
+
rate.should be_nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it "the calculator should not be available" do
|
32
|
+
@calculator.available?(@empty_order).should be_false
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
context "the weight table is empty" do
|
38
|
+
before(:each) do
|
39
|
+
@order_with_one_item.item_count.should_not == 0
|
40
|
+
end
|
41
|
+
|
42
|
+
it "the calculator should not be available" do
|
43
|
+
@calculator.available?(@order_with_one_item).should be_false
|
44
|
+
end
|
45
|
+
|
46
|
+
it "the rate should not be valid" do
|
47
|
+
rate = @calculator.compute(@order_with_one_item)
|
48
|
+
rate.should be_nil
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
context "just weight" do
|
54
|
+
before(:each) do
|
55
|
+
@weight_rate1.save!
|
56
|
+
@weight_rate2.save!
|
57
|
+
# default_item_weight
|
58
|
+
end
|
59
|
+
|
60
|
+
after(:each) do
|
61
|
+
# reset the default item weight
|
62
|
+
@calculator.preferred_default_item_weight = 0
|
63
|
+
@calculator.save!
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should use the default item weight 0" do
|
67
|
+
@calculator.preferred_default_item_weight = 0
|
68
|
+
@calculator.save!
|
69
|
+
|
70
|
+
rate = @calculator.compute(@order_with_items_without_weight)
|
71
|
+
rate.should_not be_nil
|
72
|
+
rate.should == 100 # 0..100 => 100
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should use the default item weight 50" do
|
76
|
+
@calculator.preferred_default_item_weight = 50
|
77
|
+
@calculator.save!
|
78
|
+
|
79
|
+
rate = @calculator.compute(@order_with_items_without_weight)
|
80
|
+
rate.should_not be_nil
|
81
|
+
rate.should == 100 # 0..100 => 100
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should use the default item weight 99" do
|
85
|
+
@calculator.preferred_default_item_weight = 99
|
86
|
+
@calculator.save!
|
87
|
+
|
88
|
+
rate = @calculator.compute(@order_with_items_without_weight)
|
89
|
+
rate.should_not be_nil
|
90
|
+
rate.should == 100 # 0..100 => 100
|
91
|
+
end
|
92
|
+
|
93
|
+
it "should use the default item weight 100" do
|
94
|
+
@calculator.preferred_default_item_weight = 100
|
95
|
+
@calculator.save!
|
96
|
+
|
97
|
+
rate = @calculator.compute(@order_with_items_without_weight)
|
98
|
+
rate.should_not be_nil
|
99
|
+
rate.should == 100 # 0..100 => 100
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should use the default item weight 101" do
|
103
|
+
@calculator.preferred_default_item_weight = 101
|
104
|
+
@calculator.save!
|
105
|
+
|
106
|
+
rate = @calculator.compute(@order_with_items_without_weight)
|
107
|
+
rate.should_not be_nil
|
108
|
+
rate.should == 500 # 100.1..500 => 500
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should use the default item weight 499" do
|
112
|
+
@calculator.preferred_default_item_weight = 499
|
113
|
+
@calculator.save!
|
114
|
+
|
115
|
+
rate = @calculator.compute(@order_with_items_without_weight)
|
116
|
+
rate.should_not be_nil
|
117
|
+
rate.should == 500 # 100.1..500 => 500
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should use the default item weight 500" do
|
121
|
+
@calculator.preferred_default_item_weight = 500
|
122
|
+
@calculator.save!
|
123
|
+
|
124
|
+
rate = @calculator.compute(@order_with_items_without_weight)
|
125
|
+
rate.should_not be_nil
|
126
|
+
rate.should == 500 # 100.1..500 => 500
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should use the default item weight 501" do
|
130
|
+
@calculator.preferred_default_item_weight = 501
|
131
|
+
@calculator.save!
|
132
|
+
|
133
|
+
rate = @calculator.compute(@order_with_items_without_weight)
|
134
|
+
rate.should be_nil
|
135
|
+
end
|
136
|
+
|
137
|
+
|
138
|
+
context "items with specific weight" do
|
139
|
+
before(:each) do
|
140
|
+
@order = Factory(:order_with_one_item)
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should calculate correct rate for 1 item with weight 0" do
|
144
|
+
@order.line_items[0].variant.weight = 0
|
145
|
+
@order.save!
|
146
|
+
|
147
|
+
rate = @calculator.compute(@order)
|
148
|
+
rate.should_not be_nil
|
149
|
+
rate.should == 100 # 0..100 => 100
|
150
|
+
end
|
151
|
+
|
152
|
+
it "should calculate correct rate for 1 item with weight 50" do
|
153
|
+
@order.line_items[0].variant.weight = 50
|
154
|
+
@order.save!
|
155
|
+
|
156
|
+
rate = @calculator.compute(@order)
|
157
|
+
rate.should_not be_nil
|
158
|
+
rate.should == 100 # 0..100 => 100
|
159
|
+
end
|
160
|
+
|
161
|
+
it "should calculate correct rate for 1 item with weight 99" do
|
162
|
+
@order.line_items[0].variant.weight = 99
|
163
|
+
@order.save!
|
164
|
+
|
165
|
+
rate = @calculator.compute(@order)
|
166
|
+
rate.should_not be_nil
|
167
|
+
rate.should == 100 # 0..100 => 100
|
168
|
+
end
|
169
|
+
|
170
|
+
it "should calculate correct rate for 1 item with weight 100" do
|
171
|
+
@order.line_items[0].variant.weight = 100
|
172
|
+
@order.save!
|
173
|
+
|
174
|
+
rate = @calculator.compute(@order)
|
175
|
+
rate.should_not be_nil
|
176
|
+
rate.should == 100 # 0..100 => 100
|
177
|
+
end
|
178
|
+
|
179
|
+
it "should calculate correct rate for 1 item with weight 101" do
|
180
|
+
@order.line_items[0].variant.weight = 101
|
181
|
+
@order.save!
|
182
|
+
|
183
|
+
rate = @calculator.compute(@order)
|
184
|
+
rate.should_not be_nil
|
185
|
+
rate.should == 500 # 100.1..500 => 500
|
186
|
+
end
|
187
|
+
|
188
|
+
it "should calculate correct rate for 1 item with weight 499" do
|
189
|
+
@order.line_items[0].variant.weight = 499
|
190
|
+
@order.save!
|
191
|
+
|
192
|
+
rate = @calculator.compute(@order)
|
193
|
+
rate.should_not be_nil
|
194
|
+
rate.should == 500 # 100.1..500 => 500
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should calculate correct rate for 1 item with weight 500" do
|
198
|
+
@order.line_items[0].variant.weight = 500
|
199
|
+
@order.save!
|
200
|
+
|
201
|
+
rate = @calculator.compute(@order)
|
202
|
+
rate.should_not be_nil
|
203
|
+
rate.should == 500 # 100.1..500 => 500
|
204
|
+
end
|
205
|
+
|
206
|
+
it "should calculate correct rate for 1 item with weight 501" do
|
207
|
+
@order.line_items[0].variant.weight = 501
|
208
|
+
@order.save!
|
209
|
+
|
210
|
+
rate = @calculator.compute(@order)
|
211
|
+
rate.should be_nil
|
212
|
+
end
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
|
217
|
+
context "weight and qnty" do
|
218
|
+
before(:each) do
|
219
|
+
@weight_rate1.save!
|
220
|
+
@weight_rate2.save!
|
221
|
+
@qnty_rate1.save!
|
222
|
+
@qnty_rate2.save!
|
223
|
+
@qnty_rate3.save!
|
224
|
+
@order1 = Factory(:order_with_one_item)
|
225
|
+
@order2 = Factory(:order_with_two_items)
|
226
|
+
end
|
227
|
+
|
228
|
+
it "should calculate correct rate for weight 10 and quantity 1" do
|
229
|
+
@order1.line_items[0].variant.weight = 10
|
230
|
+
@order1.line_items[0].quantity = 1
|
231
|
+
@order1.save!
|
232
|
+
|
233
|
+
rate = @calculator.compute(@order1)
|
234
|
+
rate.should_not be_nil
|
235
|
+
rate.should == 100 + 10 # weight: 0..100 => 100, qnty: 1..10 => 10
|
236
|
+
end
|
237
|
+
|
238
|
+
it "should calculate correct rate for weight 10 and quantity 5" do
|
239
|
+
@order1.line_items[0].variant.weight = 10
|
240
|
+
@order1.line_items[0].quantity = 5
|
241
|
+
@order1.save!
|
242
|
+
|
243
|
+
rate = @calculator.compute(@order1)
|
244
|
+
rate.should_not be_nil
|
245
|
+
rate.should == 100 + 10 # weight: 0..100 => 100, qnty: 1..10 => 10
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should calculate correct rate for weight 10 and quantity 10" do
|
249
|
+
@order1.line_items[0].variant.weight = 10
|
250
|
+
@order1.line_items[0].quantity = 10
|
251
|
+
@order1.save!
|
252
|
+
|
253
|
+
rate = @calculator.compute(@order1)
|
254
|
+
rate.should_not be_nil
|
255
|
+
rate.should == 100 + 10 # weight: 0..100 => 100, qnty: 1..10 => 10
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should calculate correct rate for weight 10 and quantity 11" do
|
259
|
+
@order1.line_items[0].variant.weight = 10
|
260
|
+
@order1.line_items[0].quantity = 11
|
261
|
+
@order1.save!
|
262
|
+
|
263
|
+
rate = @calculator.compute(@order1)
|
264
|
+
rate.should_not be_nil
|
265
|
+
rate.should == 500 + 20 # weight: 100.1..500 => 500, qnty: 11..20 => 20
|
266
|
+
end
|
267
|
+
|
268
|
+
it "should calculate correct rate for weight 10 and quantity 20" do
|
269
|
+
@order1.line_items[0].variant.weight = 10
|
270
|
+
@order1.line_items[0].quantity = 20
|
271
|
+
@order1.save!
|
272
|
+
|
273
|
+
rate = @calculator.compute(@order1)
|
274
|
+
rate.should_not be_nil
|
275
|
+
rate.should == 500 + 20 # weight: 100.1..500 => 500, qnty: 11..20 => 20
|
276
|
+
end
|
277
|
+
|
278
|
+
it "should calculate correct rate for weight 10 and quantity 21" do
|
279
|
+
@order1.line_items[0].variant.weight = 10
|
280
|
+
@order1.line_items[0].quantity = 21
|
281
|
+
@order1.save!
|
282
|
+
|
283
|
+
rate = @calculator.compute(@order1)
|
284
|
+
rate.should_not be_nil
|
285
|
+
rate.should == 500 + 50 # weight: 100.1..500 => 500, qnty: 21..50 => 50
|
286
|
+
end
|
287
|
+
|
288
|
+
it "should calculate nil rate and not be available when quantity is exceeded" do
|
289
|
+
@order1.line_items[0].variant.weight = 1
|
290
|
+
@order1.line_items[0].quantity = 100
|
291
|
+
@order1.save!
|
292
|
+
|
293
|
+
@calculator.available?(@order1).should be_false
|
294
|
+
rate = @calculator.compute(@order1)
|
295
|
+
rate.should be_nil # weight: 0..100 => 100, qnty: 21..50 => 50 (when qnty is exceeded and there are qnty rates, return nil)
|
296
|
+
end
|
297
|
+
|
298
|
+
end
|
299
|
+
end
|
@@ -1,7 +1,152 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe AdditionalCalculatorRate do
|
4
|
-
|
4
|
+
before(:all) do
|
5
|
+
@shipping_method = Factory(:shipping_method)
|
6
|
+
@calculator = Factory(:weight_and_quantity_calculator)
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@rate = Factory.build(:additional_calculator_rate_for_weight, :from_value => 0, :to_value => 100, :rate => 100)
|
11
|
+
@rate2 = Factory.build(:additional_calculator_rate_for_weight, :from_value => 100.1, :to_value => 500, :rate => 500)
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
context "without validation errors" do
|
16
|
+
it "should not have validation errors with valid attributes" do
|
17
|
+
@rate.should be_valid
|
18
|
+
@rate.save!
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should allow equal from_value and to_value" do
|
22
|
+
@rate.from_value = @rate.to_value = 30.1
|
23
|
+
@rate.should be_valid
|
24
|
+
@rate.save!
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "with validation errors" do
|
29
|
+
context "from_value" do
|
30
|
+
it "should be entered" do
|
31
|
+
@rate.from_value = nil
|
32
|
+
@rate.should_not be_valid
|
33
|
+
@rate.errors_on(:from_value).should_not be_blank
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should be numeric" do
|
37
|
+
@rate.from_value = 'foo'
|
38
|
+
@rate.should_not be_valid
|
39
|
+
@rate.errors_on(:from_value).should_not be_blank
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should be less or equal than to_value" do
|
43
|
+
@rate.from_value = 21.1
|
44
|
+
@rate.to_value = 20.2
|
45
|
+
@rate.should_not be_valid
|
46
|
+
@rate.errors_on(:base).should_not be_blank
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "to_value" do
|
51
|
+
it "should be entered" do
|
52
|
+
@rate.to_value = nil
|
53
|
+
@rate.should_not be_valid
|
54
|
+
@rate.errors_on(:to_value).should_not be_blank
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should be numeric" do
|
58
|
+
@rate.to_value = 'foo'
|
59
|
+
@rate.should_not be_valid
|
60
|
+
@rate.errors_on(:to_value).should_not be_blank
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "rate" do
|
65
|
+
it "should be entered" do
|
66
|
+
@rate.rate = nil
|
67
|
+
@rate.should_not be_valid
|
68
|
+
@rate.errors_on(:rate).should_not be_blank
|
69
|
+
end
|
5
70
|
|
71
|
+
it "should be numeric" do
|
72
|
+
@rate.rate = 'foo'
|
73
|
+
@rate.should_not be_valid
|
74
|
+
@rate.errors_on(:rate).should_not be_blank
|
75
|
+
end
|
76
|
+
end
|
6
77
|
end
|
78
|
+
|
79
|
+
context "find_rate" do
|
80
|
+
it "should find nothing when there are no rates" do
|
81
|
+
AdditionalCalculatorRate.all.should have(0).records
|
82
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 0).should be_nil
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should find a valid rate (from a single entry)" do
|
86
|
+
@rate.save!
|
87
|
+
|
88
|
+
AdditionalCalculatorRate.all.should have(1).records
|
89
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 0).should == 100
|
90
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 0.1).should == 100
|
91
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 1).should == 100
|
92
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 50).should == 100
|
93
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 99).should == 100
|
94
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 99.9).should == 100
|
95
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 100).should == 100
|
96
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 100.1).should be_nil
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should find a valid rate (from two entries)" do
|
100
|
+
@rate.save!
|
101
|
+
@rate2.save!
|
102
|
+
|
103
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 0).should == 100
|
104
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 0.1).should == 100
|
105
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 1).should == 100
|
106
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 50).should == 100
|
107
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 99).should == 100
|
108
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 99.9).should == 100
|
109
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 100).should == 100
|
110
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 100.1).should == 500
|
111
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 101).should == 500
|
112
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 499).should == 500
|
113
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 499.9).should == 500
|
114
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 500).should == 500
|
115
|
+
AdditionalCalculatorRate.find_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 500.1).should be_nil
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
context "find_previous_rate" do
|
120
|
+
it "should find nothing when there are no rates" do
|
121
|
+
AdditionalCalculatorRate.all.should have(0).records
|
122
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 0).should be_nil
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should find nothing when there is a single rate entry" do
|
126
|
+
@rate.save!
|
127
|
+
|
128
|
+
AdditionalCalculatorRate.all.should have(1).records
|
129
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 0).should be_nil
|
130
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 50).should be_nil
|
131
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 99.9).should be_nil
|
132
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 100).should be_nil
|
133
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 100.1).should == 100
|
134
|
+
end
|
135
|
+
|
136
|
+
it "should find a valid rate (from two entries)" do
|
137
|
+
@rate.save!
|
138
|
+
@rate2.save!
|
139
|
+
|
140
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 0).should be_nil
|
141
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 99.9).should be_nil
|
142
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 100).should be_nil
|
143
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 100.1).should == 100
|
144
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 101).should == 100
|
145
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 499).should == 100
|
146
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 499.9).should == 100
|
147
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 500).should == 100
|
148
|
+
AdditionalCalculatorRate.find_previous_rate(@calculator.id, AdditionalCalculatorRate::WEIGHT, 500.1).should == 500
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
7
152
|
end
|
data/spec/factories.rb
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
# -------------------------------------- sequences --------------------------------------
|
2
|
+
|
3
|
+
|
4
|
+
Factory.sequence(:email_sequence) {|n| "user#{n}@example.com"}
|
5
|
+
Factory.sequence(:sku_sequence) {|n| n}
|
6
|
+
Factory.sequence(:product_sequence) {|n| "Product ##{n} - #{rand(9999)}"}
|
7
|
+
|
8
|
+
|
9
|
+
# -------------------------------------- shipping_method --------------------------------------
|
10
|
+
|
11
|
+
|
12
|
+
Factory.define :shipping_method do |f|
|
13
|
+
f.name 'Test Shipping Method'
|
14
|
+
f.zone { Zone.first }
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
# -------------------------------------- calculators --------------------------------------
|
19
|
+
|
20
|
+
|
21
|
+
Factory.define :weight_and_quantity_calculator, :class => AdditionalCalculator::WeightAndQuantity do |f|
|
22
|
+
f.calculable { ShippingMethod.first }
|
23
|
+
f.is_additional_calculator true
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
# -------------------------------------- additional_calculator_rate --------------------------------------
|
28
|
+
|
29
|
+
|
30
|
+
Factory.define :additional_calculator_rate_for_weight, :class => AdditionalCalculatorRate, do |f|
|
31
|
+
f.calculator { Calculator.where(:is_additional_calculator => true).order(:id).last }
|
32
|
+
f.calculator_type 'Calculator'
|
33
|
+
f.rate_type AdditionalCalculatorRate::WEIGHT
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
Factory.define :additional_calculator_rate_for_qnty, :class => AdditionalCalculatorRate, do |f|
|
38
|
+
f.calculator { Calculator.where(:is_additional_calculator => true).order(:id).last }
|
39
|
+
f.calculator_type 'Calculator'
|
40
|
+
f.rate_type AdditionalCalculatorRate::QNTY
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
# -------------------------------------- orders --------------------------------------
|
45
|
+
|
46
|
+
|
47
|
+
Factory.define :order do |f|
|
48
|
+
f.association(:user, :factory => :user)
|
49
|
+
f.association(:bill_address, :factory => :address)
|
50
|
+
f.completed_at nil
|
51
|
+
f.bill_address_id nil
|
52
|
+
f.ship_address_id nil
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
Factory.define :order_with_one_item, :parent => :order do |f|
|
57
|
+
f.after_create { |order| Factory(:line_item, :order => order) }
|
58
|
+
end
|
59
|
+
|
60
|
+
Factory.define :order_with_two_items, :parent => :order do |f|
|
61
|
+
f.after_create do |order|
|
62
|
+
Factory(:line_item, :order => order)
|
63
|
+
Factory(:line_item, :order => order)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
Factory.define :order_with_items_without_weight, :parent => :order do |f|
|
68
|
+
f.after_create { |order| Factory(:line_item_without_weight, :order => order) }
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
# -------------------------------------- line_items --------------------------------------
|
73
|
+
|
74
|
+
|
75
|
+
Factory.define(:line_item) do |f|
|
76
|
+
f.quantity 1
|
77
|
+
f.price { BigDecimal.new("10.00") }
|
78
|
+
|
79
|
+
# associations:
|
80
|
+
f.association(:order, :factory => :order)
|
81
|
+
f.association(:variant, :factory => :variant)
|
82
|
+
end
|
83
|
+
|
84
|
+
Factory.define(:line_item_without_weight, :parent => :line_item) do |f|
|
85
|
+
# associations:
|
86
|
+
f.association(:order, :factory => :order)
|
87
|
+
f.association(:variant, :factory => :variant_without_weight)
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
# -------------------------------------- variants --------------------------------------
|
92
|
+
|
93
|
+
|
94
|
+
Factory.define(:variant) do |f|
|
95
|
+
f.price 19.99
|
96
|
+
f.cost_price 17.00
|
97
|
+
f.sku { Factory.next(:sku_sequence) }
|
98
|
+
f.weight { BigDecimal.new("#{rand(200)}.#{rand(99)}") }
|
99
|
+
# f.height { BigDecimal.new("#{rand(200)}.#{rand(99)}") }
|
100
|
+
# f.width { BigDecimal.new("#{rand(200)}.#{rand(99)}") }
|
101
|
+
# f.depth { BigDecimal.new("#{rand(200)}.#{rand(99)}") }
|
102
|
+
|
103
|
+
# associations:
|
104
|
+
f.product { |p| p.association(:product) }
|
105
|
+
# f.option_values { [Factory(:option_value)] }
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
Factory.define(:variant_without_weight, :parent => :variant) do |f|
|
110
|
+
f.weight nil
|
111
|
+
end
|
112
|
+
|
113
|
+
|
114
|
+
# -------------------------------------- products --------------------------------------
|
115
|
+
|
116
|
+
|
117
|
+
Factory.define :product do |f|
|
118
|
+
f.name { Factory.next(:product_sequence) }
|
119
|
+
f.description {|p| p.name }
|
120
|
+
|
121
|
+
# associations:
|
122
|
+
# f.tax_category {|r| TaxCategory.find(:first) || r.association(:tax_category)}
|
123
|
+
# f.shipping_category {|r| ShippingCategory.find(:first) || r.association(:shipping_category)}
|
124
|
+
|
125
|
+
f.price 19.99
|
126
|
+
f.cost_price 17.00
|
127
|
+
f.sku "ABC"
|
128
|
+
f.available_on { 1.year.ago }
|
129
|
+
f.deleted_at nil
|
130
|
+
end
|
131
|
+
|
132
|
+
|
133
|
+
# -------------------------------------- users --------------------------------------
|
134
|
+
|
135
|
+
|
136
|
+
Factory.define(:user) do |f|
|
137
|
+
f.email { Factory.next(:email_sequence) }
|
138
|
+
f.password 'password'
|
139
|
+
f.password_confirmation 'password'
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
# -------------------------------------- addresses --------------------------------------
|
144
|
+
|
145
|
+
|
146
|
+
Factory.define :address do |f|
|
147
|
+
f.firstname 'John'
|
148
|
+
f.lastname 'Doe'
|
149
|
+
f.address1 '10 Lovely Street'
|
150
|
+
f.address2 'Northwest'
|
151
|
+
f.city "Herndon"
|
152
|
+
f.zipcode '20170'
|
153
|
+
f.phone '123-456-7890'
|
154
|
+
f.alternative_phone "123-456-7899"
|
155
|
+
|
156
|
+
f.state { |address| address.association(:state) }
|
157
|
+
f.country do |address|
|
158
|
+
if address.state
|
159
|
+
address.state.country
|
160
|
+
else
|
161
|
+
address.association(:country)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
|
167
|
+
# -------------------------------------- states --------------------------------------
|
168
|
+
|
169
|
+
|
170
|
+
Factory.define :state do |f|
|
171
|
+
f.name 'ALABAMA'
|
172
|
+
f.abbr 'AL'
|
173
|
+
f.country do |country|
|
174
|
+
if usa = Country.find_by_numcode(840)
|
175
|
+
country = usa
|
176
|
+
else
|
177
|
+
country.association(:country)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.platform = Gem::Platform::RUBY
|
3
3
|
s.name = 'spree_additional_calculators'
|
4
|
-
s.version = '0.1.
|
4
|
+
s.version = '0.1.1'
|
5
5
|
s.summary = 'Additional calculators for spree'
|
6
6
|
s.description = 'Allows to calculate shipping costs based on total item weigh and quantity in the order'
|
7
7
|
s.required_ruby_version = '>= 1.8.7'
|
metadata
CHANGED
@@ -1,37 +1,34 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: spree_additional_calculators
|
3
|
-
version: !ruby/object:Gem::Version
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
4
5
|
prerelease:
|
5
|
-
version: 0.1.0
|
6
6
|
platform: ruby
|
7
|
-
authors:
|
7
|
+
authors:
|
8
8
|
- Jurgis Jurksta
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2011-06-17 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
16
15
|
name: spree_core
|
17
|
-
|
18
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: &8498870 !ruby/object:Gem::Requirement
|
19
17
|
none: false
|
20
|
-
requirements:
|
21
|
-
- -
|
22
|
-
- !ruby/object:Gem::Version
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
23
21
|
version: 0.50.2
|
24
22
|
type: :runtime
|
25
|
-
|
26
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *8498870
|
25
|
+
description: Allows to calculate shipping costs based on total item weigh and quantity
|
26
|
+
in the order
|
27
27
|
email: jurgis@emails.lv
|
28
28
|
executables: []
|
29
|
-
|
30
29
|
extensions: []
|
31
|
-
|
32
30
|
extra_rdoc_files: []
|
33
|
-
|
34
|
-
files:
|
31
|
+
files:
|
35
32
|
- .gitignore
|
36
33
|
- LICENSE
|
37
34
|
- README.md
|
@@ -56,37 +53,40 @@ files:
|
|
56
53
|
- lib/tasks/install.rake
|
57
54
|
- lib/tasks/spree_additional_calculators.rake
|
58
55
|
- public/javascripts/additional_calculators.js
|
56
|
+
- spec.opts
|
57
|
+
- spec/app/models/additional_calculator/weight_and_quantity_spec.rb
|
59
58
|
- spec/app/models/additional_calculator_rate_spec.rb
|
59
|
+
- spec/factories.rb
|
60
60
|
- spec/spec_helper.rb
|
61
61
|
- spree_additional_calculators.gemspec
|
62
62
|
- test_app.sh
|
63
63
|
homepage: https://github.com/jurgis/spree-additional-calculators
|
64
64
|
licenses: []
|
65
|
-
|
66
65
|
post_install_message:
|
67
66
|
rdoc_options: []
|
68
|
-
|
69
|
-
require_paths:
|
67
|
+
require_paths:
|
70
68
|
- lib
|
71
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
72
70
|
none: false
|
73
|
-
requirements:
|
74
|
-
- -
|
75
|
-
- !ruby/object:Gem::Version
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
76
74
|
version: 1.8.7
|
77
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
76
|
none: false
|
79
|
-
requirements:
|
80
|
-
- -
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version:
|
83
|
-
requirements:
|
77
|
+
requirements:
|
78
|
+
- - ! '>='
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: '0'
|
81
|
+
requirements:
|
84
82
|
- none
|
85
83
|
rubyforge_project:
|
86
|
-
rubygems_version: 1.
|
84
|
+
rubygems_version: 1.8.5
|
87
85
|
signing_key:
|
88
86
|
specification_version: 3
|
89
87
|
summary: Additional calculators for spree
|
90
|
-
test_files:
|
88
|
+
test_files:
|
89
|
+
- spec/app/models/additional_calculator/weight_and_quantity_spec.rb
|
91
90
|
- spec/app/models/additional_calculator_rate_spec.rb
|
91
|
+
- spec/factories.rb
|
92
92
|
- spec/spec_helper.rb
|