1 #---
2 # Excerpted from "Agile Web Development with Rails, 3rd Ed.",
3 # published by The Pragmatic Bookshelf.
4 # Copyrights apply to this code. It may not be used to create training material,
5 # courses, books, articles, and the like. Contact us if you are in doubt.
6 # We make no guarantees that this code is fit for any purpose.
7 # Visit http://www.pragmaticprogrammer.com/titles/rails3 for more book information.
8 #---
9
10
11
12 class Order < ActiveRecord::Base
13
14 PAYMENT_TYPES = [
15 # Displayed stored in db
16 [ "Check", "check" ],
17 [ "Credit card", "cc" ],
18 [ "Purchase order", "po" ]
19 ]
20
21
22 # ...
23
24
25 validates_presence_of :name, :address, :email, :pay_type
26 validates_inclusion_of :pay_type, :in =>
27 PAYMENT_TYPES.map {|disp, value| value}
28
29 # ...
30
31
32
33
34 has_many :line_items
35
36
37
38 def add_line_items_from_cart(cart)
39 cart.items.each do |item|
40 li = LineItem.from_cart_item(item)
41 line_items << li
42 end
43 end
44
45
46 end
47