Class: Order

Inherits:
ActiveRecord::Base show all
Includes:
PaymentTransitions
Defined in:
app/models/order.rb

Defined Under Namespace

Modules: PaymentTransitions

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PaymentTransitions

#after_authorize, #after_kapture, #after_pending, #after_purchase, #after_refund, #after_void

Instance Attribute Details

#billing_address_same_as_shippingObject

Returns the value of attribute billing_address_same_as_shipping.



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

def billing_address_same_as_shipping
  @billing_address_same_as_shipping
end

#validate_emailObject

Returns the value of attribute validate_email.



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

def validate_email
  @validate_email
end

Instance Method Details

#add(product) ⇒ Object



81
82
83
84
85
86
# File 'app/models/order.rb', line 81

def add(product)
  options = { product_id: product.id }
  if line_items.where(options).empty?
    line_items.create(options.merge(quantity: 1))
  end
end

#available_shipping_methodsObject



73
74
75
# File 'app/models/order.rb', line 73

def available_shipping_methods
  ShippingMethod.available_for(line_items_total, shipping_address)
end

#billing_address_same_as_shipping?Boolean

Returns:

  • (Boolean)


124
125
126
# File 'app/models/order.rb', line 124

def billing_address_same_as_shipping?
  billing_address_same_as_shipping
end

#initialize_addressesObject



119
120
121
122
# File 'app/models/order.rb', line 119

def initialize_addresses
  shipping_address || build_shipping_address(country_code: "US", use_for_billing: true)
  billing_address || build_billing_address(country_code: "US", use_for_billing: false)
end

#item_countObject



77
78
79
# File 'app/models/order.rb', line 77

def item_count
  line_items.count
end

#line_item_for(product_id) ⇒ Object



135
136
137
# File 'app/models/order.rb', line 135

def line_item_for(product_id)
  line_items.find_by_product_id(product_id)
end

#line_items_totalObject

Returns the total price of all line items in float.



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

def line_items_total
  line_items.reduce(BigDecimal('0')) do |sum, line_item|
    sum + BigDecimal(line_item.price.to_s)
  end.round(2).to_f
end

#mark_as_purchased!Object



69
70
71
# File 'app/models/order.rb', line 69

def mark_as_purchased!
  touch(:purchased_at) unless purchased_at
end

#real_billing_addressObject Also known as: final_billing_address

If billing_address is same as shipping_address then order.billing_address returns nil . order.real_billing_address will always returns a valid address



130
131
132
# File 'app/models/order.rb', line 130

def real_billing_address
  (shipping_address && !shipping_address.use_for_billing) ? billing_address : shipping_address
end

#remove(product) ⇒ Object



96
97
98
# File 'app/models/order.rb', line 96

def remove(product)
  update_quantity({product.id => 0})
end

#shippable_countriesObject



139
140
141
# File 'app/models/order.rb', line 139

def shippable_countries
  ShippingMethod.available_for_countries(line_items_total)
end

#to_paramObject



115
116
117
# File 'app/models/order.rb', line 115

def to_param
  number
end

#total_amountObject



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

def total_amount
  line_items_total + shipping_cost + tax
end

#total_amount_in_centsObject



111
112
113
# File 'app/models/order.rb', line 111

def total_amount_in_cents
  Nimbleshop::Util.in_cents(total_amount)
end

#update_quantity(data = {}) ⇒ Object



88
89
90
91
92
93
94
# File 'app/models/order.rb', line 88

def update_quantity(data = {})
  data.each do |product_id, quantity|
    if line_item = line_item_for(product_id)
      line_item.update_quantity(quantity)
    end
  end
end