Class: Bodega::Order

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/bodega/order.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#checking_outObject

Returns the value of attribute checking_out.



7
8
9
# File 'app/models/bodega/order.rb', line 7

def checking_out
  @checking_out
end

Instance Method Details

#finalize!(options) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/bodega/order.rb', line 39

def finalize!(options)
  self.class.transaction do
    self.status = :complete
    self.save!
    begin
      self.payment_id = payment_method.complete!(options)
      self.save!
    rescue Exception
      raise ActiveRecord::Rollback
    end
  end
end

#new_shipping_rates?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/bodega/order.rb', line 52

def new_shipping_rates?
  @new_shipping_rates
end

#payment_methodObject



56
57
58
59
# File 'app/models/bodega/order.rb', line 56

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

#productsObject



61
62
63
# File 'app/models/bodega/order.rb', line 61

def products
  order_products.map(&:product)
end

#ready?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'app/models/bodega/order.rb', line 65

def ready?
  shipping_method.nil? || shipping_rates.present?
end

#remove_product(item) ⇒ Object



69
70
71
72
73
74
75
# File 'app/models/bodega/order.rb', line 69

def remove_product(item)
  unless item.is_a?(Bodega::OrderProduct)
    item = order_product(item)
  end
  item.destroy
  order_products.delete(item)
end

#shipping_methodObject



77
78
79
80
81
82
# File 'app/models/bodega/order.rb', line 77

def shipping_method
  case Bodega.config.shipping_method
  when :ups
    Bodega::ShippingMethod::UPS.new(self)
  end
end

#shipping_rate_optionsObject



84
85
86
87
88
89
90
91
92
# File 'app/models/bodega/order.rb', line 84

def shipping_rate_options
  @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

#state_optionsObject



94
95
96
# File 'app/models/bodega/order.rb', line 94

def state_options
  US_STATES.values
end

#subtotalObject



98
99
100
# File 'app/models/bodega/order.rb', line 98

def subtotal
  order_products.inject(Money.new(0)) {|sum, order_product| sum += order_product.subtotal }
end

#summaryObject



102
103
104
# File 'app/models/bodega/order.rb', line 102

def summary
  order_products.map(&:quantity_and_name).to_sentence
end

#to_paramObject



106
107
108
# File 'app/models/bodega/order.rb', line 106

def to_param
  identifier
end

#update_product(item) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/bodega/order.rb', line 110

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