Skip to content
Browse files

orders now have a restaurant

  • Loading branch information...
1 parent 89d1fa6 commit 7a4b84eaaccb29c195f9e9455a2b46b61634585c John Hyland committed Mar 15, 2012
View
11 app/models/order.rb
@@ -1,28 +1,35 @@
class Order < ActiveRecord::Base
belongs_to :orderer, :class_name => "User"
belongs_to :accepter, :class_name => "User"
+ belongs_to :restaurant
has_many :ordered_items, :dependent => :destroy
has_many :items, :through => :ordered_items
accepts_nested_attributes_for :items
scope :accepted, where('accepter_id is not null')
validates_presence_of :orderer
+ validates_presence_of :restaurant
validate :must_have_items
+ validate :items_must_be_from_restaurant
validate :must_have_valid_date
- before_save :initialize_cost
+ before_save :initialize_cost, :if => :accepted?
def must_have_items
errors.add :base, "Must include at least one item" if self.items.empty?
end
+ def items_must_be_from_restaurant
+ errors.add :items, "Must all come from the order's restaurant" if self.items.any? { |i| i.restaurant_id != self.restaurant_id }
+ end
+
def must_have_valid_date
errors.add :date, 'must be a valid date' unless self.date.kind_of? Time
end
def initialize_cost
- self.cost = self.expected_cost if self.accepted? and self.cost.nil?
+ self.cost ||= self.expected_cost
end
def expected_cost
View
5 db/migrate/20120316022855_add_restaurant_id_to_orders.rb
@@ -0,0 +1,5 @@
+class AddRestaurantIdToOrders < ActiveRecord::Migration
+ def change
+ add_column :orders, :restaurant_id, :integer
+ end
+end
View
4 db/schema.rb
@@ -1,3 +1,4 @@
+# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
@@ -10,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20100920160349) do
+ActiveRecord::Schema.define(:version => 20120316022855) do
create_table "items", :force => true do |t|
t.string "name"
@@ -35,6 +36,7 @@
t.datetime "created_at"
t.datetime "updated_at"
t.float "cost"
+ t.integer "restaurant_id"
end
create_table "payments", :force => true do |t|
View
3 spec/factories.rb
@@ -14,10 +14,11 @@
end
Order.blueprint do
+ restaurant { Restaurant.make }
orderer { User.make }
date { Time.now }
- after_build { |order| order.items = [ Item.make, Item.make ] }
+ after_build { |order| order.items = [Item.make(:restaurant => order.restaurant)] }
end
Sham.cost { |n| n / 2.0 }
View
12 spec/models/order_spec.rb
@@ -29,8 +29,8 @@
it "should return the sum of the cost of its ordered items" do
order = create :order
order.items.clear
- order.items << create(:item, :cost => 1.5)
- order.items << create(:item, :cost => 2)
+ order.items << create(:item, :cost => 1.5, :restaurant => order.restaurant)
+ order.items << create(:item, :cost => 2, :restaurant => order.restaurant)
order.expected_cost.should == 3.5
end
end
@@ -39,9 +39,9 @@
before do
@order = create :order
@order.items.clear
- @item = create :item, :cost => 2
+ @item = create :item, :cost => 2, :restaurant => @order.restaurant
@order.items << @item
- @order.save
+ @order.save!
end
context "before the order is accepted" do
@@ -53,7 +53,7 @@
context "after the order is accepted" do
before do
@order.accepter = create :user
- @order.save
+ @order.save!
end
it "should be set" do
@@ -64,7 +64,7 @@
new_cost = @item.cost + 5
@order.cost = new_cost
- @order.save
+ @order.save!
@order.reload
@order.cost.should == new_cost
end

0 comments on commit 7a4b84e

Please sign in to comment.
Something went wrong with that request. Please try again.