public
Created

Order Item Payment

  • Download Gist
order_item_payment.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
class Order
include DataMapper::Resource
 
property :id, Serial, key: true
property :status, Enum[:new, :ready, :paid, :shipped, :closed, :canceled]
 
belongs_to :buyer, 'User'
has n, :items
has 1, :payment
 
property :shipping_address_country, String
property :shipping_address_city, String
 
def shipping_address= address
self.shipping_address_country = address.country
self.shipping_address_city = address.city
end
 
def shipping_address
Address.new(shipping_address_country, shipping_address_city)
end
 
 
def self.make buyer
create buyer: buyer, shipping_address: buyer.address, status: :new
end
 
def self.get buyer, id
#...
end
 
def self.all_orders buyer
#...
end
 
def self.active_orders buyer
#...
end
 
 
def make_item book, quantity
ensure_status :new
 
amount = book.price * quantity
items.create book: book, quantity: quantity, amount: amount
end
 
def make_payment masked_card
ensure_status :ready
 
self.payment = Payment.new(masked_card: masked_card, amount: total_amount)
self.status = :paid
save
end
 
def mark_as_ready
#...
end
 
def mark_as_shipped
#...
end
 
def total_amount
#...
end
 
private
 
def ensure_status required_status
raise InvalidOrderStatus.new(self) if status != required_status
end
end
 
class Item
include DataMapper::Resource
 
property :id, Serial
belongs_to :book
 
property :quantity, Integer
property :amount, Decimal
end
 
class Payment
include DataMapper::Resource
 
property :id, Serial
property :masked_card, String
property :amount, Decimal
 
property :created_at, DateTime
property :updated_at, DateTime
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.