Skip to content

Commit

Permalink
Refactor for Spree v2.1 Rails 4
Browse files Browse the repository at this point in the history
- Spree i18n namespace.
- Changed to hash syntax.
- Now it uses Spree::ShippingCalculator
- Updated spec syntax.
  • Loading branch information
futhr committed Apr 5, 2014
1 parent 3bbfec3 commit 60cd0d4
Show file tree
Hide file tree
Showing 13 changed files with 185 additions and 184 deletions.
2 changes: 1 addition & 1 deletion Guardfile
Original file line number Diff line number Diff line change
@@ -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" }
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -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
Expand Down
File renamed without changes.
22 changes: 12 additions & 10 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -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
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
22 changes: 12 additions & 10 deletions config/locales/pt.yml
Original file line number Diff line number Diff line change
@@ -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
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
22 changes: 12 additions & 10 deletions config/locales/sv.yml
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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 }
Expand Down
2 changes: 1 addition & 1 deletion lib/spree_postal_service.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
require 'spree/core'
require 'spree_core'
require 'spree_postal_service/engine'
require 'spree_postal_service/version'
6 changes: 3 additions & 3 deletions lib/spree_postal_service/engine.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/spree_postal_service/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def self.version

module VERSION
MAJOR = 2
MINOR = 0
MINOR = 1
TINY = 0
PRE = nil

Expand Down
Loading

0 comments on commit 60cd0d4

Please sign in to comment.