Permalink
Browse files

Implemented polymorphic connection between companies/people and email…

…_addresses/phone_numbers
  • Loading branch information...
1 parent e5df9dc commit 6062f11c61b2e14e96bb2caed4ac3349ea4b3475 @jcasimir committed Apr 20, 2011
View
13 app/controllers/email_addresses_controller.rb
@@ -8,13 +8,18 @@ def show
end
def new
- @email_address = EmailAddress.new(:person_id => params[:person_id])
+ if params[:person_id]
+ contact = Person.find params[:person_id]
+ else
+ contact = Company.find params[:company_id]
+ end
+ @email_address = contact.email_addresses.new
end
def create
@email_address = EmailAddress.new(params[:email_address])
if @email_address.save
- redirect_to @email_address.person, :notice => "Successfully created email address."
+ redirect_to @email_address.contact, :notice => "Successfully created email address."
else
render :action => 'new'
end
@@ -27,7 +32,7 @@ def edit
def update
@email_address = EmailAddress.find(params[:id])
if @email_address.update_attributes(params[:email_address])
- redirect_to @email_address.person, :notice => "Successfully updated email address."
+ redirect_to @email_address.contact, :notice => "Successfully updated email address."
else
render :action => 'edit'
end
@@ -36,6 +41,6 @@ def update
def destroy
@email_address = EmailAddress.find(params[:id])
@email_address.destroy
- redirect_to @email_address.person, :notice => "Successfully destroyed email address."
+ redirect_to @email_address.contact, :notice => "Successfully destroyed email address."
end
end
View
11 app/controllers/phone_numbers_controller.rb
@@ -8,13 +8,18 @@ def show
end
def new
- @phone_number = PhoneNumber.new(:person_id => params[:person_id])
+ if params[:person_id]
+ contact = Person.find params[:person_id]
+ else
+ contact = Company.find params[:company_id]
+ end
+ @phone_number = contact.phone_numbers.new
end
def create
@phone_number = PhoneNumber.new(params[:phone_number])
if @phone_number.save
- redirect_to @phone_number.person, :notice => "Successfully created phone number."
+ redirect_to @phone_number.contact, :notice => "Successfully created phone number."
else
render :action => 'new'
end
@@ -27,7 +32,7 @@ def edit
def update
@phone_number = PhoneNumber.find(params[:id])
if @phone_number.update_attributes(params[:phone_number])
- redirect_to @phone_number.person, :notice => "Successfully updated phone number."
+ redirect_to @phone_number.contact, :notice => "Successfully updated phone number."
else
render :action => 'edit'
end
View
1 app/models/company.rb
@@ -4,4 +4,5 @@ class Company < ActiveRecord::Base
validates_presence_of :name
has_many :phone_numbers, :as => :contact
+ has_many :email_addresses, :as => :contact
end
View
6 app/models/email_address.rb
@@ -1,7 +1,7 @@
class EmailAddress < ActiveRecord::Base
- attr_accessible :address, :person_id
+ attr_accessible :address, :contact_id, :contact_type
- belongs_to :person
+ belongs_to :contact, :polymorphic => true
- validates_presence_of :address, :person
+ validates_presence_of :address, :contact
end
View
2 app/models/person.rb
@@ -4,5 +4,5 @@ class Person < ActiveRecord::Base
validates_presence_of :first_name, :last_name
has_many :phone_numbers, :as => :contact
- has_many :email_addresses
+ has_many :email_addresses, :as => :contact
end
View
2 app/models/phone_number.rb
@@ -1,5 +1,5 @@
class PhoneNumber < ActiveRecord::Base
- attr_accessible :number, :person_id
+ attr_accessible :number, :contact_id, :contact_type
belongs_to :contact, :polymorphic => true
validates_presence_of :number, :contact
View
9 app/views/companies/_phone_numbers.html.erb
@@ -0,0 +1,9 @@
+<ul>
+ <% phone_numbers.each do |phone_number| %>
+ <li>
+ <%= phone_number.number%>
+ <%= link_to "edit", edit_phone_number_path(phone_number), :id => "edit_phone_number_#{phone_number.id}" %>
+ <%= link_to "delete", phone_number_path(phone_number), :method => :delete, :id => "delete_phone_number_#{phone_number.id}" %>
+ </li>
+ <% end %>
+</ul>
View
7 app/views/companies/show.html.erb
@@ -6,6 +6,13 @@
</p>
<p>
+ <strong>Phone Numbers:</strong>
+ <%= render :partial => 'phone_numbers', :object => @company.phone_numbers %>
+</p>
+
+<%= link_to "Add a New Phone Number", new_phone_number_path(:company_id => @company), :id => "new_phone_number" %>
+
+<p>
<%= link_to "Edit", edit_company_path(@company) %> |
<%= link_to "Destroy", @company, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", companies_path %>
View
3 app/views/email_addresses/_form.html.erb
@@ -4,6 +4,7 @@
<%= f.label :address %><br />
<%= f.text_field :address %>
</p>
- <%= f.hidden_field :person_id %>
+ <%= f.hidden_field :contact_id %>
+ <%= f.hidden_field :contact_type %>
<p><%= f.submit %></p>
<% end %>
View
3 app/views/phone_numbers/_form.html.erb
@@ -4,6 +4,7 @@
<%= f.label :number %><br />
<%= f.text_field :number %>
</p>
- <%= f.hidden_field :person_id %>
+ <%= f.hidden_field :contact_id %>
+ <%= f.hidden_field :contact_type %>
<p><%= f.submit %></p>
<% end %>
View
12 db/migrate/20110420161636_change_email_addresses_to_relate_to_polymorphic_contacts.rb
@@ -0,0 +1,12 @@
+class ChangeEmailAddressesToRelateToPolymorphicContacts < ActiveRecord::Migration
+ def self.up
+ EmailAddress.destroy_all
+ remove_column :email_addresses, :person_id
+ add_column :email_addresses, :contact_id, :integer
+ add_column :email_addresses, :contact_type, :string
+ end
+
+ def self.down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
View
5 db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110420143503) do
+ActiveRecord::Schema.define(:version => 20110420161636) do
create_table "companies", :force => true do |t|
t.string "name"
@@ -20,9 +20,10 @@
create_table "email_addresses", :force => true do |t|
t.string "address"
- t.integer "person_id"
t.datetime "created_at"
t.datetime "updated_at"
+ t.integer "contact_id"
+ t.string "contact_type"
end
create_table "people", :force => true do |t|
View
109 spec/integration/companies_views_spec.rb
@@ -0,0 +1,109 @@
+require 'spec_helper'
+require 'capybara/rspec'
+
+describe "the views for companies", :type => :request do
+ before(:all) do
+ @company = Company.create(:name => "Sample Corp")
+ number_a = @company.phone_numbers.create(:number => "1234567")
+ number_b = @company.phone_numbers.create(:number => "7654321")
+ #@person.email_addresses.create(:address => "john@doe.com")
+ #@person.email_addresses.create(:address => "doe@john.com")
+ end
+
+ describe "when looking at the list of companies" do
+ before(:all) do
+ visit companies_path
+ end
+
+ # it "should display email addresses" do
+ # @person.email_addresses.each do |email_address|
+ # page.should have_selector('li', :text => email_address.address)
+ # end
+ # end
+ end
+
+ describe "when looking at a single company" do
+ before(:each) do
+ visit company_path(@company)
+ end
+
+ # it "should have delete links for each email address" do
+ # @person.email_addresses.each do |email_address|
+ # page.should have_link("delete_email_address_#{email_address.id}")
+ # end
+ # end
+ #
+ # it "should have an add email address link" do
+ # page.should have_link("new_email_address")
+ # end
+ #
+ # it "should go to the new email address form when the link is clicked" do
+ # click_link "new_email_address"
+ # current_path.should == new_email_address_path
+ # end
+ #
+ # it "should display each of the email addresses" do
+ # @person.email_addresses.each do |email_address|
+ # page.should have_selector('li', :text => email_address.address)
+ # end
+ # end
+
+ it "should have edit links for each phone number" do
+ @company.phone_numbers.each do |phone_number|
+ page.should have_link("edit", :href => edit_phone_number_path(phone_number))
+ end
+ end
+
+ it "should have an add phone number link" do
+ page.should have_link("new_phone_number")
+ end
+
+ it "should display each of the phone numbers" do
+ @company.phone_numbers.each do |phone_number|
+ page.should have_selector('li', :text => phone_number.number)
+ end
+ end
+
+ it "should have delete links for each phone number" do
+ @company.phone_numbers.each do |phone_number|
+ page.should have_link("delete", :href => phone_number_path(phone_number))
+ end
+ end
+
+ it "should show the person after deleting a phone number" do
+ target = @company.phone_numbers.first
+ page.click_link("delete_phone_number_#{target.id}")
+ current_path.should == company_path(@company)
+ page.should_not have_link("delete", :href => phone_number_path(target))
+ end
+
+ it "should show the person including the new number after creating a phone number" do
+ page.click_link("new_phone_number")
+ sample_number = "3334445555"
+ page.fill_in("phone_number_number", :with => sample_number)
+ page.click_button("phone_number_submit")
+ current_path.should == company_path(@company)
+ page.should have_selector('li', :text => sample_number)
+ end
+
+ it "should show the person including the updated number after editing a phone number" do
+ target = @company.phone_numbers.first
+ original_number = target.number
+ new_number = original_number.reverse
+ page.click_link("edit_phone_number_#{target.id}")
+ current_path.should == edit_phone_number_path(target)
+ page.fill_in("phone_number_number", :with => new_number)
+ page.click_button("phone_number_submit")
+ current_path.should == company_path(@company)
+ page.should have_selector('li', :text => new_number)
+ end
+
+
+ # it "should show the person after deleting an email address" do
+ # target = @person.email_addresses.first
+ # page.click_link("delete_email_address_#{target.id}")
+ # current_path.should == person_path(@person)
+ # page.should_not have_link("delete_email_address_#{target.id}")
+ # end
+ end
+end
View
11 spec/models/company_spec.rb
@@ -2,7 +2,7 @@
describe Company do
before(:each) do
- @company = Company.new(:name => "Sample Corp")
+ @company = Company.create(:name => "Sample Corp")
end
it "should be valid" do
@@ -23,4 +23,13 @@
@company.phone_numbers.should include(phone_number)
end
+ it "should have an array of email addresses" do
+ @company.email_addresses.should be_an_instance_of(Array)
+ end
+
+ it "should respond with its email addresses after they're created" do
+ email_address = @company.email_addresses.build(:address => "john@doe.com")
+ @company.email_addresses.should include(email_address)
+ @company.should be_valid
+ end
end
View
4 spec/models/email_address_spec.rb
@@ -15,8 +15,8 @@
@email_address.should_not be_valid
end
- it "should not be valid without a person" do
- @email_address.person_id = nil
+ it "should not be valid without a contact" do
+ @email_address.contact = nil
@email_address.should_not be_valid
end
end

0 comments on commit 6062f11

Please sign in to comment.