Skip to content

Instantly share code, notes, and snippets.

@vsavkin
Created April 21, 2012 00:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsavkin/2432874 to your computer and use it in GitHub Desktop.
Save vsavkin/2432874 to your computer and use it in GitHub Desktop.
Order Item Payment
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment