From 60cd0d40cf0b69126f76d5e257d03265dae1a245 Mon Sep 17 00:00:00 2001 From: Tobias Bohwalli Date: Sat, 5 Apr 2014 14:56:08 +0200 Subject: [PATCH] Refactor for Spree v2.1 Rails 4 - Spree i18n namespace. - Changed to hash syntax. - Now it uses Spree::ShippingCalculator - Updated spec syntax. --- Guardfile | 2 +- README.md | 4 +- Rakefile | 8 +- {script => bin}/rails | 0 config/locales/en.yml | 22 +- config/locales/pt.yml | 22 +- config/locales/sv.yml | 22 +- .../{ => shipping}/postal_service.rb | 42 ++-- lib/spree_postal_service.rb | 2 +- lib/spree_postal_service/engine.rb | 6 +- lib/spree_postal_service/version.rb | 2 +- spec/lib/postal_service_spec.rb | 219 +++++++++--------- spree_postal_service.gemspec | 18 +- 13 files changed, 185 insertions(+), 184 deletions(-) rename {script => bin}/rails (100%) rename lib/spree/calculator/{ => shipping}/postal_service.rb (58%) diff --git a/Guardfile b/Guardfile index 5b8e9eb..88fb704 100644 --- a/Guardfile +++ b/Guardfile @@ -1,4 +1,4 @@ -guard 'rspec', :cmd => 'bundle exec rspec', :all_on_start => true do +guard 'rspec', cmd: 'bundle exec rspec', all_on_start: true do watch('spec/spec_helper.rb') { 'spec' } watch(%r{^spec/(.+)_spec\.rb$}) { |m| "spec/#{m[1]}_spec.rb"} watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } diff --git a/README.md b/README.md index 886f69b..ede1b71 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Spree Postal Service -[![Build Status](https://api.travis-ci.org/futhr/spree-postal-service.png?branch=2-0-stable)](https://travis-ci.org/futhr/spree-postal-service) +[![Build Status](https://api.travis-ci.org/futhr/spree-postal-service.png?branch=2-1-stable)](https://travis-ci.org/futhr/spree-postal-service) [![Code Climate](https://codeclimate.com/github/futhr/spree-postal-service.png)](https://codeclimate.com/github/futhr/spree-postal-service) [![Gem Version](https://badge.fury.io/rb/spree_postal_service.png)](http://badge.fury.io/rb/spree_postal_service) @@ -25,7 +25,7 @@ Use the same measurements as in the product info page. Add to your `Gemfile`: ```ruby -gem 'spree_postal_service', '~> 2.0.0' +gem 'spree_postal_service', '~> 2.1.0' ``` Go to admin interface diff --git a/Rakefile b/Rakefile index 575e9f0..229cad8 100644 --- a/Rakefile +++ b/Rakefile @@ -1,15 +1,13 @@ -# encoding: utf-8 require 'bundler' Bundler::GemHelper.install_tasks Bundler.setup require 'rspec/core/rake_task' -RSpec::Core::RakeTask.new - require 'spree/testing_support/common_rake' -desc 'Default Task' -task :default => [:spec] +RSpec::Core::RakeTask.new + +task default: [:spec] desc 'Generates a dummy app for testing' task :test_app do diff --git a/script/rails b/bin/rails similarity index 100% rename from script/rails rename to bin/rails diff --git a/config/locales/en.yml b/config/locales/en.yml index f47d9d7..f5075fd 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1,12 +1,14 @@ --- en: - price_table: Prices (space seperated) - weight_table: Weights (space seperated) - postal_service: Postal Service - max_item_weight: Max weight of one item - max_item_width: Max width of one item - max_item_length: Max length of one item - handling_fee: Handling fee - handling_max: "Amount, over which handling fee won't be applied" - min_price: Minimum total of the order - default_weight: Default weight \ No newline at end of file + spree: + price_table: Prices (space seperated) + weight_table: Weights (space seperated) + postal_service: Postal Service + max_item_weight: Max weight of one item + max_item_width: Max width of one item + max_item_length: Max length of one item + handling_fee: Handling fee + handling_max: "Amount, over which handling fee won't be applied" + min_price: Minimum price + default_weight: Default weight + max_price: Maximum price \ No newline at end of file diff --git a/config/locales/pt.yml b/config/locales/pt.yml index e797794..741edcb 100644 --- a/config/locales/pt.yml +++ b/config/locales/pt.yml @@ -1,12 +1,14 @@ --- pt: - price_table: Preços (separados por espaços) - weight_table: Pesos (separados por espaços) - postal_service: Serviço Postal - max_item_weight: Peso máximo por item - max_item_width: Peso máxima por item - max_item_length: Comprimento máximo por item - handling_fee: Taxa de tratamento - handling_max: Valor a partir do qual a taxa de tratamento não é aplicada - min_price: Preço mínimo - default_weight: Peso por omissão \ No newline at end of file + spree: + price_table: Preços (separados por espaços) + weight_table: Pesos (separados por espaços) + postal_service: Serviço Postal + max_item_weight: Peso máximo por item + max_item_width: Peso máxima por item + max_item_length: Comprimento máximo por item + handling_fee: Taxa de tratamento + handling_max: Valor a partir do qual a taxa de tratamento não é aplicada + min_price: Preço mínimo + default_weight: Peso por omissão + max_price: Preço máximo \ No newline at end of file diff --git a/config/locales/sv.yml b/config/locales/sv.yml index 1c0c731..6bc1be0 100644 --- a/config/locales/sv.yml +++ b/config/locales/sv.yml @@ -1,12 +1,14 @@ --- sv: - price_table: Priser (mellanrums separerade) - weight_table: Vikter (mellanrums separerade) - postal_service: Post Service - max_item_weight: Max vikt på ett objekt - max_item_width: Max bredd på ett objekt - max_item_length: Max längd på ett objekt - handling_fee: Hantering avgift - handling_max: "Belopp, över vilken expeditionsavgift inte skall tillämpas" - min_price: Minsta belopp - default_weight: Förvald vikt + spree: + price_table: Priser (mellanrums separerade) + weight_table: Vikter (mellanrums separerade) + postal_service: Post Service + max_item_weight: Max vikt på ett objekt + max_item_width: Max bredd på ett objekt + max_item_length: Max längd på ett objekt + handling_fee: Hantering avgift + handling_max: "Belopp, över vilken expeditionsavgift inte skall tillämpas" + min_price: Minsta belopp + default_weight: Förvald vikt + max_price: Största belopp \ No newline at end of file diff --git a/lib/spree/calculator/postal_service.rb b/lib/spree/calculator/shipping/postal_service.rb similarity index 58% rename from lib/spree/calculator/postal_service.rb rename to lib/spree/calculator/shipping/postal_service.rb index 50df16a..0019595 100644 --- a/lib/spree/calculator/postal_service.rb +++ b/lib/spree/calculator/shipping/postal_service.rb @@ -1,26 +1,16 @@ -class Spree::Calculator::PostalService < Spree::Calculator - preference :weight_table, :string, :default => '1 2 5 10 20' - preference :price_table, :string, :default => '6 9 12 15 18' - preference :max_item_weight, :decimal, :default => 18 - preference :max_item_width, :decimal, :default => 60 - preference :max_item_length, :decimal, :default => 120 - preference :max_price, :decimal, :default => 120 - preference :handling_max, :decimal, :default => 50 - preference :handling_fee, :decimal, :default => 10 - preference :default_weight, :decimal, :default => 1 - - attr_accessible :preferred_weight_table, - :preferred_price_table, - :preferred_max_item_weight, - :preferred_max_item_width, - :preferred_max_item_length, - :preferred_max_price, - :preferred_handling_max, - :preferred_handling_fee, - :preferred_default_weight +class Spree::Calculator::Shipping::PostalService < Spree::ShippingCalculator + preference :weight_table, :string, default: '1 2 5 10 20' + preference :price_table, :string, default: '6 9 12 15 18' + preference :max_item_weight, :decimal, default: 18 + preference :max_item_width, :decimal, default: 60 + preference :max_item_length, :decimal, default: 120 + preference :max_price, :decimal, default: 120 + preference :handling_max, :decimal, default: 50 + preference :handling_fee, :decimal, default: 10 + preference :default_weight, :decimal, default: 1 def self.description - I18n.t(:postal_service) + Spree.t(:postal_service) end def self.register @@ -35,12 +25,12 @@ def item_oversized?(variant) ].sort.reverse return true if sizes[0] > self.preferred_max_item_length # longest side - return true if sizes[0] > self.preferred_max_item_width # second longest side + return true if sizes[1] > self.preferred_max_item_width # second longest side return false end - def available?(shipment) - variants = shipment.line_items.map(&:variant) + def available?(package) + variants = package.contents.map(&:variant) variants.each do |variant| # determine if weight or size goes over bounds return false if variant.weight && variant.weight > self.preferred_max_item_weight # 18 return false if item_oversized? variant @@ -49,8 +39,8 @@ def available?(shipment) end # as order_or_line_items we always get line items, as calculable we have Coupon, ShippingMethod or ShippingRate - def compute(shipment) - order = shipment.order + def compute(package) + order = package.order total_price, total_weight, shipping = 0, 0, 0 prices = self.preferred_price_table.split.map { |price| price.to_f } diff --git a/lib/spree_postal_service.rb b/lib/spree_postal_service.rb index c6159e7..cb5a9e4 100644 --- a/lib/spree_postal_service.rb +++ b/lib/spree_postal_service.rb @@ -1,3 +1,3 @@ -require 'spree/core' +require 'spree_core' require 'spree_postal_service/engine' require 'spree_postal_service/version' \ No newline at end of file diff --git a/lib/spree_postal_service/engine.rb b/lib/spree_postal_service/engine.rb index 41be5b8..a63c7f5 100644 --- a/lib/spree_postal_service/engine.rb +++ b/lib/spree_postal_service/engine.rb @@ -1,13 +1,13 @@ module SpreePostalService class Engine < Rails::Engine + isolate_namespace Spree engine_name 'spree_postal_service' config.autoload_paths += %W(#{config.root}/lib) initializer 'spree.register.calculators' do |app| - require 'spree/calculator/postal_service' - - app.config.spree.calculators.shipping_methods << Spree::Calculator::PostalService + require 'spree/calculator/shipping/postal_service' + app.config.spree.calculators.shipping_methods << Spree::Calculator::Shipping::PostalService end end end \ No newline at end of file diff --git a/lib/spree_postal_service/version.rb b/lib/spree_postal_service/version.rb index e029ad6..f512b15 100644 --- a/lib/spree_postal_service/version.rb +++ b/lib/spree_postal_service/version.rb @@ -7,7 +7,7 @@ def self.version module VERSION MAJOR = 2 - MINOR = 0 + MINOR = 1 TINY = 0 PRE = nil diff --git a/spec/lib/postal_service_spec.rb b/spec/lib/postal_service_spec.rb index 14cab13..f83357d 100644 --- a/spec/lib/postal_service_spec.rb +++ b/spec/lib/postal_service_spec.rb @@ -1,124 +1,131 @@ require 'spec_helper' -describe Spree::Calculator::PostalService do +describe Spree::Calculator::Shipping::PostalService do - let(:postal_service_calculator) { described_class.new } + let(:postal_service_calculator) { described_class.new } describe 'return description' do context 'in english' do before { I18n.locale = :en } specify do - postal_service_calculator.description.should == 'Postal Service' + expect(postal_service_calculator.description).to eq 'Postal Service' end end context 'in any language' do before { I18n.locale = :pt } specify do - postal_service_calculator.description.should == I18n.t(:postal_service) + expect(postal_service_calculator.description).to eq Spree.t(:postal_service) end end end - describe ':using the default weight-price table: [1 2 5 10 20] => [6 9 12 15 18]' do - context '#compute:' do - it 'gives 15.0 when total price is 100 and weight is 10kg' do - create_our_package(:weight => 10.0, :price => 100.0, :quantity => 1) - postal_service_calculator.compute(@shipment).should == 15.0 - end - - it 'gives 25.0 when total price is 40 and weight is 10kg' do - create_our_package(:weight => 10.0, :price => 40.0, :quantity => 1) - postal_service_calculator.compute(@shipment).should == 25.0 - end - - it 'gives 6 when total price is 60 and weight is less than 1kg' do - create_our_package(:weight => 0.5, :price => 60.0, :quantity => 1) - postal_service_calculator.compute(@shipment).should == 6.0 - end - - it 'gives 16 when total price is 40 and weight is less than 1kg' do - create_our_package(:weight => 0.5, :price => 40.0, :quantity => 1) - postal_service_calculator.compute(@shipment).should == 16.0 - end - - it 'gives 30 when total price is 200 and weight is 25kg (split into two)' do - create_our_package(:weight => 25.0, :price => 200.0, :quantity => 1) - postal_service_calculator.preferred_max_price = 250 - postal_service_calculator.compute(@shipment).should == 30.0 - end - - it 'gives 12 when total price is 100, there are three items and their weight is unknown' do - order = create(:order) - [30.0, 40.0, 30.0].each do |price| - create(:line_item, - :price => price, - :quantity => 1, - :order => order, - :variant => create(:variant, :weight => nil)) - end - order.line_items.reload - shipment = create(:shipment, :order => order) - - postal_service_calculator.compute(shipment).should == 12.0 - end - - it 'gives 0 when total price is more than the MAX, for any number of items' do - create_our_package(:weight => 25.0, :price => 350.0, :quantity => 1) - postal_service_calculator.preferred_max_price = 300 - postal_service_calculator.compute(@shipment).should == 0.0 - end - end - end - - describe 'when preferred max weight, length and width are 18 kg, 120 cm and 60 cm' do - context '#available?' do - it 'is false when item weighs more than 18kg' do - create_our_package(:weight => 20, :height => 70, :width => 30, :depth => 30) - postal_service_calculator.available?(@shipment).should be_false - end - - it 'is false when item is longer than 120cm' do - create_our_package(:weight => 10, :height => 130, :width => 30, :depth => 30) - postal_service_calculator.available?(@shipment).should be_false - end - - it 'is false when item is wider than 60cm' do - create_our_package(:weight => 10, :height => 80, :width => 70, :depth => 30) - postal_service_calculator.available?(@shipment).should be_false - end - end - - context '#item_oversized?' do - it 'is true if the longest side is more than 120cm' do - create_our_package(:weight => 10, :height => 130, :width => 40, :depth => 30) - postal_service_calculator.item_oversized?(@variant).should be_true - end - - it 'is true if the second longest side is more than 60cm' do - create_our_package(:weight => 10, :height => 80, :width => 70, :depth => 30) - postal_service_calculator.item_oversized?(@variant).should be_true - end - end - end - - def create_our_package(args={}) - order = create(:order) - - params = {} - params.merge!(:weight => args[:weight]) if args[:weight] - params.merge!(:height => args[:height]) if args[:height] - params.merge!(:width => args[:width]) if args[:width] - params.merge!(:depth => args[:depth]) if args[:depth] - @variant = create(:variant, params) - - params = { :variant => @variant, :order => order } - params.merge!(:price => args[:price]) if args[:price] - params.merge!(:quantity => args[:quantity]) if args[:quantity] - line_item = create(:line_item, params) - - order = line_item.order - order.line_items.reload - @shipment = create(:shipment, :order => order) - end -end + describe ':using the default weight-price table: [1 2 5 10 20] => [6 9 12 15 18]' do + context '#compute:' do + it 'gives 15.0 when total price is 100 and weight is 10kg' do + create_our_package(weight: 10.0, price: 100.0, quantity: 1) + result = postal_service_calculator.compute(@package) + expect(result).to eq(15.0) + end + + it 'gives 25.0 when total price is 40 and weight is 10kg' do + create_our_package(weight: 10.0, price: 40.0, quantity: 1) + result = postal_service_calculator.compute(@package) + expect(result).to eq(25.0) + end + + it 'gives 6 when total price is 60 and weight is less than 1kg' do + create_our_package(weight: 0.5, price: 60.0, quantity: 1) + result = postal_service_calculator.compute(@package) + expect(result).to eq(6.0) + end + + it 'gives 16 when total price is 40 and weight is less than 1kg' do + create_our_package(weight: 0.5, price: 40.0, quantity: 1) + result = postal_service_calculator.compute(@package) + expect(result).to eq(16.0) + end + + it 'gives 30 when total price is 200 and weight is 25kg (split into two)' do + create_our_package(weight: 25.0, price: 200.0, quantity: 1) + postal_service_calculator.preferred_max_price = 250 + result = postal_service_calculator.compute(@package) + expect(result).to eq(30.0) + end + + it 'gives 12 when total price is 100, there are three items and their weight is unknown' do + order = create(:order) + [30.0, 40.0, 30.0].each do |price| + create(:line_item, + price: price, + quantity: 1, + order: order, + variant: create(:base_variant, weight: nil)) + end + order.line_items.reload + shipment = create(:shipment, order: order) + package = shipment.to_package + + result = postal_service_calculator.compute(shipment) + expect(result).to eq(12.0) + end + + it 'gives 0 when total price is more than the MAX, for any number of items' do + create_our_package(weight: 25.0, price: 350.0, quantity: 1) + postal_service_calculator.preferred_max_price = 300 + result = postal_service_calculator.compute(@package) + expect(result).to eq(0.0) + end + end + end + + describe 'when preferred max weight, length and width are 18 kg, 120 cm and 60 cm' do + context '#available?' do + it 'is false when item weighs more than 18kg' do + create_our_package(weight: 20, height: 70, width: 30, depth: 30) + expect(postal_service_calculator.available?(@package)).to be_false + end + + it 'is false when item is longer than 120cm' do + create_our_package(weight: 10, height: 130, width: 30, depth: 30) + expect(postal_service_calculator.available?(@package)).to be_false + end + + it 'is false when item is wider than 60cm' do + create_our_package(weight: 10, height: 80, width: 70, depth: 30) + expect(postal_service_calculator.available?(@package)).to be_false + end + end + + context '#item_oversized?' do + it 'is true if the longest side is more than 120cm' do + create_our_package(weight: 10, height: 130, width: 40, depth: 30) + expect(postal_service_calculator.item_oversized?(@variant)).to be_true + end + + it 'is true if the second longest side is more than 60cm' do + create_our_package(weight: 10, height: 80, width: 70, depth: 30) + expect(postal_service_calculator.item_oversized?(@variant)).to be_true + end + end + end + + def create_our_package(args={}) + params = {} + params.merge!(weight: args[:weight]) if args[:weight] + params.merge!(height: args[:height]) if args[:height] + params.merge!(width: args[:width]) if args[:width] + params.merge!(depth: args[:depth]) if args[:depth] + @variant = create(:base_variant, params) + + params = { variant: @variant } + params.merge!(price: args[:price]) if args[:price] + params.merge!(quantity: args[:quantity]) if args[:quantity] + @line_item = create(:line_item, params) + + @order = @line_item.order + @order.line_items.reload + @shipment = create(:shipment, order: @order) + @package = @shipment.to_package + end +end \ No newline at end of file diff --git a/spree_postal_service.gemspec b/spree_postal_service.gemspec index c926a69..a427ae1 100644 --- a/spree_postal_service.gemspec +++ b/spree_postal_service.gemspec @@ -24,15 +24,15 @@ Gem::Specification.new do |s| s.has_rdoc = false - s.add_dependency 'spree_core', '~> 2.0.0' + s.add_dependency 'spree_core', '~> 2.1.0' s.add_development_dependency 'rspec', '~> 2.14.0' s.add_development_dependency 'rspec-rails', '~> 2.14.0' - s.add_development_dependency 'factory_girl', '~> 4.2' - s.add_development_dependency 'sqlite3' - s.add_development_dependency 'simplecov' - s.add_development_dependency 'i18n-spec' - s.add_development_dependency 'ffaker' - s.add_development_dependency 'guard-rspec' - s.add_development_dependency 'pry-rails' -end + s.add_development_dependency 'factory_girl', '~> 4.4' + s.add_development_dependency 'sqlite3', '>= 1.3.9' + s.add_development_dependency 'simplecov', '>= 0.7.1' + s.add_development_dependency 'i18n-spec', '>= 0.4.1' + s.add_development_dependency 'ffaker', '>= 1.24.0' + s.add_development_dependency 'guard-rspec', '>= 4.2.8' + s.add_development_dependency 'pry-rails', '>= 0.3.2' +end \ No newline at end of file