app/models/bodega/order.rb in bodega-0.4.9 vs app/models/bodega/order.rb in bodega-0.4.12
- old
+ new
@@ -2,11 +2,15 @@
module Bodega
class Order < ActiveRecord::Base
self.table_name = :bodega_orders
+ attr_accessor :checking_out
+
+ attr_accessible :email, :checking_out
attr_accessible :order_products_attributes, :postal_code, :shipping_rate_code
+ attr_accessible :street_1, :street_2, :city, :state
before_create :set_identifier
before_save :set_shipping_rates, if: :set_shipping_rates?
before_save :calculate_shipping, if: :shipping_rate_code_changed?
before_save :set_total
@@ -28,17 +32,19 @@
monetize :tax_cents
monetize :total_cents
serialize :shipping_rates
+ validates_presence_of :email, if: :require_email?
+
def finalize!(options)
self.class.transaction do
self.status = :complete
self.save!
begin
self.payment_id = payment_method.complete!(options)
- self.save
+ self.save!
rescue Exception
raise ActiveRecord::Rollback
end
end
end
@@ -83,10 +89,14 @@
rates["#{name}: #{price.format}"] = code
end
end
end
+ def state_options
+ US_STATES.values
+ end
+
def subtotal
order_products.inject(Money.new(0)) {|sum, order_product| sum += order_product.subtotal }
end
def summary
@@ -136,10 +146,14 @@
else
order_products.detect {|order_product| order_product.identifier == item }
end
end
+ def require_email?
+ Bodega.config.collect_email && persisted? && (postal_code_changed? || checking_out)
+ end
+
def set_identifier
self.identifier = self.class.count.succ.to_s(36)
end
def set_shipping_rates
@@ -158,7 +172,64 @@
end
def set_total
self.total = subtotal + tax + shipping
end
+ end
+
+
+ US_STATES = ActiveSupport::OrderedHash.new
+ Hash['Alabama', 'AL',
+ 'Alaska', 'AK',
+ 'Arizona', 'AZ',
+ 'Arkansas', 'AR',
+ 'California', 'CA',
+ 'Colorado', 'CO',
+ 'Connecticut', 'CT',
+ 'Delaware', 'DE',
+ 'District Of Columbia', 'DC',
+ 'Florida', 'FL',
+ 'Georgia', 'GA',
+ 'Hawaii', 'HI',
+ 'Idaho', 'ID',
+ 'Illinois', 'IL',
+ 'Indiana', 'IN',
+ 'Iowa', 'IA',
+ 'Kansas', 'KS',
+ 'Kentucky', 'KY',
+ 'Louisiana', 'LA',
+ 'Maine', 'ME',
+ 'Maryland', 'MD',
+ 'Massachusetts', 'MA',
+ 'Michigan', 'MI',
+ 'Minnesota', 'MN',
+ 'Mississippi', 'MS',
+ 'Missouri', 'MO',
+ 'Montana', 'MT',
+ 'Nebraska', 'NE',
+ 'Nevada', 'NV',
+ 'New Hampshire', 'NH',
+ 'New Jersey', 'NJ',
+ 'New Mexico', 'NM',
+ 'New York', 'NY',
+ 'North Carolina', 'NC',
+ 'North Dakota', 'ND',
+ 'Ohio', 'OH',
+ 'Oklahoma', 'OK',
+ 'Oregon', 'OR',
+ 'Pennsylvania', 'PA',
+ 'Rhode Island', 'RI',
+ 'South Carolina', 'SC',
+ 'South Dakota', 'SD',
+ 'Tennessee', 'TN',
+ 'Texas', 'TX',
+ 'Utah', 'UT',
+ 'Vermont', 'VT',
+ 'Virginia', 'VA',
+ 'Washington', 'WA',
+ 'West Virginia', 'WV',
+ 'Wisconsin', 'WI',
+ 'Wyoming', 'WY'
+ ].sort.each do |key, value|
+ US_STATES[key] = value
end
end