app/models/carts/cart.rb in artfully_ose-1.0.0.rc4 vs app/models/carts/cart.rb in artfully_ose-1.1.0.rc1
- old
+ new
@@ -1,19 +1,21 @@
class Cart < ActiveRecord::Base
include ActiveRecord::Transitions
has_many :donations, :dependent => :destroy
has_many :tickets, :after_add => :set_timeout
- after_destroy :release_tickets
+ after_destroy :clear!
attr_accessor :special_instructions
+ belongs_to :discount
+
state_machine do
state :started
state :approved
state :rejected
- event(:approve) { transitions :from => [ :started, :rejected ], :to => :approved }
+ event(:approve, :success => :record_sold_price) { transitions :from => [ :started, :rejected ], :to => :approved }
event(:reject) { transitions :from => [ :started, :rejected ], :to => :rejected }
end
delegate :empty?, :to => :items
def items
@@ -23,10 +25,11 @@
def checkout_class
Checkout
end
def clear!
+ reset_prices_on_tickets
clear_tickets
clear_donations
end
def as_json(options = {})
@@ -37,11 +40,11 @@
release_tickets
self.tickets = []
end
def release_tickets
- tickets.each { |ticket| ticket.update_attribute(:cart, nil) }
+ tickets.each { |ticket| ticket.remove_from_cart }
end
def set_timeout(ticket)
save if new_record?
@@ -49,10 +52,11 @@
self.delay(:run_at => Time.now + 10.minutes).expire_ticket(ticket)
end
end
def expire_ticket(ticket)
+ ticket.reset_price!
tickets.delete(ticket)
end
def items_subject_to_fee
self.tickets.reject{|t| t.price == 0}
@@ -74,14 +78,26 @@
def <<(tkts)
self.tickets << tkts
end
- def total
+ def subtotal
+ items.sum(&:price)
+ end
+
+ def total_before_discount
items.sum(&:price) + fee_in_cents
end
+ def total
+ items.sum(&:cart_price) + fee_in_cents
+ end
+
+ def discount_amount
+ total_before_discount - total
+ end
+
def unfinished?
started? or rejected?
end
def completed?
@@ -91,19 +107,18 @@
def pay_with(payment, options = {})
@payment = payment
#TODO: Move the requires_authorization? check into the payments classes. Cart shouldn't care
if payment.requires_authorization?
+ options[:service_fee] = fee_in_cents
pay_with_authorization(payment, options)
else
approve!
end
end
- def finish(person, order_timestamp)
- metric_sale_total
- tickets.each { |ticket| ticket.sell_to(person, order_timestamp) }
+ def finish
end
def generate_donations
organizations_from_tickets.collect do |organization|
if organization.can?(:receive, Donation)
@@ -132,29 +147,25 @@
def reseller_is?(reseller)
reseller == nil
end
+ def reset_prices_on_tickets
+ transaction do
+ tickets.each {|ticket| ticket.reset_price! }
+ end
+ end
+
private
- def pay_with_authorization(payment, options)
- response = payment.purchase(options)
- response.success? ? approve! : reject!
+ def record_sold_price
+ self.tickets.each do |ticket|
+ ticket.sold_price = ticket.cart_price || ticket.price
+ ticket.save
+ end
end
- def metric_sale_total
- bracket =
- case self.total
- when 0 then "$0.00"
- when (1 ... 1000) then "$0.01 - $9.99"
- when (1000 ... 2000) then "$10 - $19.99"
- when (2000 ... 5000) then "$20 - $49.99"
- when (5000 ... 10000) then "$50 - $99.99"
- when (10000 ... 25000) then "$100 - $249.99"
- when (25000 ... 50000) then "$250 - $499.99"
- else "$500 or more"
- end
-
- RestfulMetrics::Client.add_compound_metric(ENV["RESTFUL_METRICS_APP"], "sale_complete", [ bracket ])
+ def pay_with_authorization(payment, options)
+ payment.purchase(options) ? approve! : reject!
end
end