-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Backported engine and spec for Spree v1.1
- Loading branch information
Showing
11 changed files
with
184 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,3 +9,6 @@ nbproject | |
*.swp | ||
Gemfile.lock | ||
.rvmrc | ||
.localeapp | ||
coverage | ||
spec/dummy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
source 'https://rubygems.org/' | ||
|
||
gem 'spree_core', github: 'spree/spree', branch: '1-1-stable' | ||
|
||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,83 @@ | ||
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 :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_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 | ||
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 | ||
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 | ||
|
||
def self.description | ||
'Postal' | ||
I18n.t(:postal_service) | ||
end | ||
|
||
def self.register | ||
super | ||
end | ||
|
||
def item_oversized? item | ||
variant = item.variant | ||
sizes = [ variant.width ? variant.width : 0 , variant.depth ? variant.depth : 0 , variant.height ? variant.height : 0 ].sort! | ||
return true if sizes[0] > self.preferred_max_item_length | ||
return true if sizes[0] > self.preferred_max_item_width | ||
def item_oversized?(variant) | ||
sizes = [ | ||
variant.width ? variant.width : 0, | ||
variant.depth ? variant.depth : 0, | ||
variant.height ? variant.height : 0 | ||
].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 false | ||
end | ||
|
||
def available?(order) | ||
order.line_items.each do |item| # determine if weight or size goes over bounds | ||
return false if item.variant.weight and item.variant.weight > self.preferred_max_item_weight | ||
return false if item_oversized?( item ) | ||
def available?(shipment) | ||
variants = shipment.line_items.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 | ||
end | ||
return true | ||
end | ||
|
||
# as order_or_line_items we always get line items, as calculable we have Coupon, ShippingMethod or ShippingRate | ||
def compute(order) | ||
debug = false | ||
puts order if debug | ||
def compute(shipment) | ||
order = shipment.order | ||
|
||
total_price , total_weight , shipping = 0 , 0 , 0 | ||
prices = self.preferred_price_table.split.map {|price| price.to_f } | ||
puts prices.join(' ') if debug | ||
total_price, total_weight, shipping = 0, 0, 0 | ||
prices = self.preferred_price_table.split.map { |price| price.to_f } | ||
|
||
order.line_items.each do |item| # determine total price and weight | ||
total_weight += item.quantity * (item.variant.weight || self.preferred_default_weight) | ||
total_price += item.price * item.quantity | ||
total_weight += item.quantity * (item.variant.weight || self.preferred_default_weight) | ||
total_price += item.price * item.quantity | ||
end | ||
puts 'Weight ' + total_weight.to_s if debug | ||
puts 'Price ' + total_price.to_s if debug | ||
|
||
return 0.0 if total_price > self.preferred_max_price | ||
|
||
# determine handling fee | ||
puts 'Handling max ' + self.preferred_handling_max.to_s if debug | ||
handling_fee = self.preferred_handling_max < total_price ? 0 : self.preferred_handling_fee | ||
puts 'Handling ' + handling_fee.to_s if debug | ||
weights = self.preferred_weight_table.split.map {|weight| weight.to_f} | ||
puts weights.join(' ') if debug | ||
while total_weight > weights.last # in several packages if need be | ||
handling_fee = self.preferred_handling_max < total_price ? 0 : self.preferred_handling_fee | ||
weights = self.preferred_weight_table.split.map { |weight| weight.to_f } | ||
|
||
while total_weight > weights.last # in several packages if need be | ||
total_weight -= weights.last | ||
shipping += prices.last | ||
end | ||
puts 'Shipping ' + shipping.to_s if debug | ||
|
||
index = weights.length - 2 | ||
while index >= 0 | ||
break if total_weight > weights[index] | ||
index -= 1 | ||
end | ||
shipping += prices[index + 1] | ||
puts 'Shipping ' + shipping.to_s if debug | ||
|
||
shipping += prices[index + 1] | ||
return shipping + handling_fee | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,3 @@ | ||
class SpreePostalService < Rails::Engine | ||
|
||
config.autoload_paths += %W(#{config.root}/lib) | ||
|
||
def self.activate | ||
Dir.glob(File.join(File.dirname(__FILE__), '../app/**/*_decorator*.rb')) do |c| | ||
Rails.env.production? ? require(c) : load(c) | ||
end | ||
end | ||
|
||
config.to_prepare &method(:activate).to_proc | ||
|
||
initializer 'spree.register.calculators' do |app| | ||
require 'spree/calculator/postal_service' | ||
|
||
app.config.spree.calculators.shipping_methods << Spree::Calculator::PostalService | ||
end | ||
end | ||
require 'spree/core' | ||
require 'spree_postal_service/engine' | ||
require 'spree_postal_service/version' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module SpreePostalService | ||
class Engine < Rails::Engine | ||
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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module SpreePostalService | ||
|
||
# Returns the version of the currently loaded SpreePostalService as a <tt>Gem::Version</tt> | ||
def self.version | ||
Gem::Version.new VERSION::STRING | ||
end | ||
|
||
module VERSION | ||
MAJOR = 1 | ||
MINOR = 1 | ||
TINY = 1 | ||
PRE = nil | ||
|
||
STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.') | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
#!/usr/bin/env ruby | ||
|
||
ENGINE_ROOT = File.expand_path('../..', __FILE__) | ||
ENGINE_PATH = File.expand_path('../../lib/spree_postal_service/engine', __FILE__) | ||
|
||
require 'rails/all' | ||
require 'rails/engine/commands' |
Oops, something went wrong.