Created
May 28, 2012 10:55
-
-
Save alininja/2818498 to your computer and use it in GitHub Desktop.
Spree Calculator Exception Handling
This file contains hidden or 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
# app/models/spree/calculator_decorator.rb | |
Spree::Calculator.class_eval do | |
class Spree::Calculator::CouldNotCalculateShipping < StandardError | |
end | |
class Spree::Calculator::CouldNotValidateOrder < StandardError | |
end | |
end |
This file contains hidden or 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
# app/controllers/spree/checkout_controller_decorator.rb | |
Spree::CheckoutController.class_eval do | |
rescue_from Spree::Calculator::CouldNotCalculateShipping do |exception| | |
flash[:error] = 'Please verify your shipping address' | |
redirect_to checkout_path | |
end | |
rescue_from Spree::Calculator::CouldNotValidateOrder do |exception| | |
flash[:error] = 'Please verify your order' | |
redirect_to cart_path | |
end | |
end |
This file contains hidden or 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
# app/models/spree/calculator/table_rate.rb | |
module Spree | |
class Calculator::TableRate < Calculator | |
def self.description | |
"Table Rate" | |
end | |
def compute(object=nil) | |
raise CouldNotValidateOrder, 'There was an error with your order.' unless object.present? and object.line_items.present? | |
case object | |
when Spree::Order | |
# get the appropriate table based on the zone | |
table = ShippingTable.find_by_zone_id(object.ship_address.zone.id) | |
raise CouldNotCalculateShipping, 'No shipping table available.' if table == nil | |
return table.compute_shipping_cost(object) | |
when Spree::LineItem | |
raise CouldNotValidateOrder, 'There was an error with your order.' | |
when Spree::Shipment | |
return 0.0 | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment