Class: Bodega::Order
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Bodega::Order
- Defined in:
- app/models/bodega/order.rb
Instance Method Summary collapse
- #finalize!(options) ⇒ Object
- #new_shipping_rates? ⇒ Boolean
- #payment_method ⇒ Object
- #products ⇒ Object
- #ready? ⇒ Boolean
- #remove_product(item) ⇒ Object
- #shipping_method ⇒ Object
- #shipping_rate_options ⇒ Object
- #subtotal ⇒ Object
- #summary ⇒ Object
- #to_param ⇒ Object
- #update_product(item) ⇒ Object
Instance Method Details
#finalize!(options) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/bodega/order.rb', line 33 def finalize!() self.class.transaction do self.status = :complete self.save! begin self.payment_id = payment_method.complete!() self.save rescue Exception raise ActiveRecord::Rollback end end end |
#new_shipping_rates? ⇒ Boolean
46 47 48 |
# File 'app/models/bodega/order.rb', line 46 def new_shipping_rates? @new_shipping_rates end |
#payment_method ⇒ Object
50 51 52 53 |
# File 'app/models/bodega/order.rb', line 50 def payment_method return nil unless Bodega.config.payment_method @payment_method ||= "Bodega::PaymentMethod::#{Bodega.config.payment_method.to_s.camelize}".constantize.new(self) end |
#products ⇒ Object
55 56 57 |
# File 'app/models/bodega/order.rb', line 55 def products order_products.map(&:product) end |
#ready? ⇒ Boolean
59 60 61 |
# File 'app/models/bodega/order.rb', line 59 def ready? shipping_method.nil? || shipping_rates.present? end |
#remove_product(item) ⇒ Object
63 64 65 66 67 68 69 |
# File 'app/models/bodega/order.rb', line 63 def remove_product(item) unless item.is_a?(Bodega::OrderProduct) item = order_product(item) end item.destroy order_products.delete(item) end |
#shipping_method ⇒ Object
71 72 73 74 75 76 |
# File 'app/models/bodega/order.rb', line 71 def shipping_method case Bodega.config.shipping_method when :ups Bodega::ShippingMethod::UPS.new(self) end end |
#shipping_rate_options ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'app/models/bodega/order.rb', line 78 def @shipping_rate_options ||= ActiveSupport::OrderedHash.new.tap do |rates| shipping_rates.sort_by {|code, rate| rate[:price] }.each do |code, rate| name = rate[:name] price = Money.new(rate[:price]) rates["#{name}: #{price.format}"] = code end end end |
#subtotal ⇒ Object
88 89 90 |
# File 'app/models/bodega/order.rb', line 88 def subtotal order_products.inject(Money.new(0)) {|sum, order_product| sum += order_product.subtotal } end |
#summary ⇒ Object
92 93 94 |
# File 'app/models/bodega/order.rb', line 92 def summary order_products.map(&:quantity_and_name).to_sentence end |
#to_param ⇒ Object
96 97 98 |
# File 'app/models/bodega/order.rb', line 96 def to_param identifier end |
#update_product(item) ⇒ Object
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'app/models/bodega/order.rb', line 100 def update_product(item) if order_product = order_product(item) if item[:remove] remove_product(order_product) else current_quantity = order_product.quantity new_quantity = item[:quantity] ? item[:quantity].to_i : current_quantity + 1 order_product.update_attributes(quantity: new_quantity) end else order_product = order_products.build({quantity: 1}.merge(item)) end save unless empty? end |