Permalink
Browse files

Changed phone_numbers to belong to a polymorphic Contact

  • Loading branch information...
1 parent 4b554ec commit e5df9dcd8a0162f1b70b5aaae5b4fd3d8d5bf176 @jcasimir committed Apr 20, 2011
@@ -36,6 +36,6 @@ def update
def destroy
@phone_number = PhoneNumber.find(params[:id])
@phone_number.destroy
- redirect_to @phone_number.person, :notice => "Successfully destroyed phone number."
+ redirect_to @phone_number.contact, :notice => "Successfully destroyed phone number."
end
end
View
@@ -1,3 +1,7 @@
class Company < ActiveRecord::Base
attr_accessible :name
+
+ validates_presence_of :name
+
+ has_many :phone_numbers, :as => :contact
end
View
@@ -3,6 +3,6 @@ class Person < ActiveRecord::Base
validates_presence_of :first_name, :last_name
- has_many :phone_numbers
+ has_many :phone_numbers, :as => :contact
has_many :email_addresses
end
@@ -1,6 +1,6 @@
class PhoneNumber < ActiveRecord::Base
attr_accessible :number, :person_id
- belongs_to :person
+ belongs_to :contact, :polymorphic => true
- validates_presence_of :number, :person
+ validates_presence_of :number, :contact
end
@@ -0,0 +1,12 @@
+class ChangePhoneNumbersToRelateToPolymorphicContacts < ActiveRecord::Migration
+ def self.up
+ PhoneNumber.destroy_all
+ remove_column :phone_numbers, :person_id
+ add_column :phone_numbers, :contact_id, :integer
+ add_column :phone_numbers, :contact_type, :string
+ end
+
+ def self.down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
View
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110420135545) do
+ActiveRecord::Schema.define(:version => 20110420143503) do
create_table "companies", :force => true do |t|
t.string "name"
@@ -34,9 +34,10 @@
create_table "phone_numbers", :force => true do |t|
t.string "number"
- t.integer "person_id"
t.datetime "created_at"
t.datetime "updated_at"
+ t.integer "contact_id"
+ t.string "contact_type"
end
end
@@ -1,7 +1,26 @@
require File.dirname(__FILE__) + '/../spec_helper'
describe Company do
+ before(:each) do
+ @company = Company.new(:name => "Sample Corp")
+ end
+
it "should be valid" do
- Company.new.should be_valid
+ @company.should be_valid
+ end
+
+ it "should not be valid without a name" do
+ @company.name = nil
+ @company.should_not be_valid
+ end
+
+ it "should have an array of phone numbers" do
+ @company.phone_numbers.class.should == Array
+ end
+
+ it "should respond with its phone numbers after they're created" do
+ phone_number = @company.phone_numbers.build(:number => "2223334444")
+ @company.phone_numbers.should include(phone_number)
end
+
end
@@ -15,8 +15,8 @@
@phone_number.should_not be_valid
end
- it "should not be valid without a person" do
- @phone_number.person = nil
+ it "should not be valid without a contact" do
+ @phone_number.contact = nil
@phone_number.should_not be_valid
end
end

0 comments on commit e5df9dc

Please sign in to comment.