From: Ira W. Snyder Date: Thu, 22 Nov 2007 23:57:27 +0000 (-0800) Subject: Add the has_many_polymorphs purchaseable model X-Git-Tag: turned-in~60 X-Git-Url: https://www.irasnyder.com/gitweb/?a=commitdiff_plain;h=97d39268c0b348898dca1ec24552eb26a94ae546;p=cs356-p2-videostore.git Add the has_many_polymorphs purchaseable model Signed-off-by: Ira W. Snyder --- diff --git a/app/models/customer.rb b/app/models/customer.rb index 901b9a5..58db47d 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -2,6 +2,8 @@ class Customer < ActiveRecord::Base has_many :coitems has_many :bitems + has_many_polymorphs :purchaseables, :from => [:coitems, :bitems], :through => :purchases + validates_presence_of :name, :email, :phone, :address validates_numericality_of :debt diff --git a/app/models/purchase.rb b/app/models/purchase.rb new file mode 100644 index 0000000..023f423 --- /dev/null +++ b/app/models/purchase.rb @@ -0,0 +1,4 @@ +class Purchase < ActiveRecord::Base + belongs_to :customer + belongs_to :purchaseable, :polymorphic => true +end diff --git a/db/development.sqlite3 b/db/development.sqlite3 index 10e50e7..2867aee 100644 Binary files a/db/development.sqlite3 and b/db/development.sqlite3 differ diff --git a/db/migrate/016_create_purchases.rb b/db/migrate/016_create_purchases.rb new file mode 100644 index 0000000..08bb7aa --- /dev/null +++ b/db/migrate/016_create_purchases.rb @@ -0,0 +1,13 @@ +class CreatePurchases < ActiveRecord::Migration + def self.up + create_table :purchases do |t| + t.column :customer_id, :integer + t.column :purchaseable_id, :integer + t.column :purchaseable_type, :string + end + end + + def self.down + drop_table :purchases + end +end diff --git a/db/schema.rb b/db/schema.rb index dcfe7d9..b349a76 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -2,7 +2,7 @@ # migrations feature of ActiveRecord to incrementally modify your database, and # then regenerate this schema definition. -ActiveRecord::Schema.define(:version => 15) do +ActiveRecord::Schema.define(:version => 16) do create_table "bitems", :force => true do |t| t.column "customer_id", :integer, :null => false @@ -35,6 +35,12 @@ ActiveRecord::Schema.define(:version => 15) do t.column "price", :decimal, :precision => 8, :scale => 2, :default => 0.0 end + create_table "purchases", :force => true do |t| + t.column "customer_id", :integer + t.column "purchaseable_id", :integer + t.column "purchaseable_type", :string + end + create_table "rentables", :force => true do |t| t.column "type", :string t.column "title", :string diff --git a/test/fixtures/purchases.yml b/test/fixtures/purchases.yml new file mode 100644 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/purchases.yml @@ -0,0 +1,5 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +one: + id: 1 +two: + id: 2 diff --git a/test/unit/purchase_test.rb b/test/unit/purchase_test.rb new file mode 100644 index 0000000..f9a5498 --- /dev/null +++ b/test/unit/purchase_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class PurchaseTest < Test::Unit::TestCase + fixtures :purchases + + # Replace this with your real tests. + def test_truth + assert true + end +end