Add the has_many_polymorphs purchaseable model
authorIra W. Snyder <devel@irasnyder.com>
Thu, 22 Nov 2007 23:57:27 +0000 (15:57 -0800)
committerIra W. Snyder <devel@irasnyder.com>
Thu, 22 Nov 2007 23:57:27 +0000 (15:57 -0800)
Signed-off-by: Ira W. Snyder <devel@irasnyder.com>
app/models/customer.rb
app/models/purchase.rb [new file with mode: 0644]
db/development.sqlite3
db/migrate/016_create_purchases.rb [new file with mode: 0644]
db/schema.rb
test/fixtures/purchases.yml [new file with mode: 0644]
test/unit/purchase_test.rb [new file with mode: 0644]

index 901b9a5..58db47d 100644 (file)
@@ -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 (file)
index 0000000..023f423
--- /dev/null
@@ -0,0 +1,4 @@
+class Purchase < ActiveRecord::Base
+  belongs_to :customer
+  belongs_to :purchaseable, :polymorphic => true
+end
index 10e50e7..2867aee 100644 (file)
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 (file)
index 0000000..08bb7aa
--- /dev/null
@@ -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
index dcfe7d9..b349a76 100644 (file)
@@ -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 (file)
index 0000000..b49c4eb
--- /dev/null
@@ -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 (file)
index 0000000..f9a5498
--- /dev/null
@@ -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