Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finished workflow for email addresses

  • Loading branch information...
commit 8def1f220260508f2872dc5063b2650a670922e4 1 parent e848059
@jcasimir jcasimir authored
View
8 app/controllers/email_addresses_controller.rb
@@ -8,13 +8,13 @@ def show
end
def new
- @email_address = EmailAddress.new
+ @email_address = EmailAddress.new(:person_id => params[:person_id])
end
def create
@email_address = EmailAddress.new(params[:email_address])
if @email_address.save
- redirect_to @email_address, :notice => "Successfully created email address."
+ redirect_to @email_address.person, :notice => "Successfully created email address."
else
render :action => 'new'
end
@@ -27,7 +27,7 @@ def edit
def update
@email_address = EmailAddress.find(params[:id])
if @email_address.update_attributes(params[:email_address])
- redirect_to @email_address, :notice => "Successfully updated email address."
+ redirect_to @email_address.person, :notice => "Successfully updated email address."
else
render :action => 'edit'
end
@@ -36,6 +36,6 @@ def update
def destroy
@email_address = EmailAddress.find(params[:id])
@email_address.destroy
- redirect_to email_addresses_url, :notice => "Successfully destroyed email address."
+ redirect_to @email_address.person, :notice => "Successfully destroyed email address."
end
end
View
5 app/views/email_addresses/_form.html.erb
@@ -4,9 +4,6 @@
<%= f.label :address %><br />
<%= f.text_field :address %>
</p>
- <p>
- <%= f.label :person_id %><br />
- <%= f.text_field :person_id %>
- </p>
+ <%= f.hidden_field :person_id %>
<p><%= f.submit %></p>
<% end %>
View
9 app/views/people/_email_addresses.html.erb
@@ -0,0 +1,9 @@
+<ul>
+ <% email_addresses.each do |email_address| %>
+ <li>
+ <%= email_address.address%>
+ <%= link_to "edit", edit_email_address_path(email_address) %>
+ <%= link_to "delete", email_address_path(email_address), :method => :delete, :id => "delete_email_address_#{email_address.id}" %>
+ </li>
+ <% end %>
+</ul>
View
4 app/views/people/index.html.erb
@@ -5,12 +5,14 @@
<th>First Name</th>
<th>Last Name</th>
<th>Phone Numbers</th>
+ <th>Email Addresses</th>
</tr>
<% for person in @people %>
<tr>
<td><%= person.first_name %></td>
<td><%= person.last_name %></td>
- <td><%= render :partial => 'phone_numbers', :object => person.phone_numbers %>
+ <td><%= render :partial => 'phone_numbers', :object => person.phone_numbers %></td>
+ <td><%= render :partial => 'email_addresses', :object => person.email_addresses %>
<td><%= link_to "Show", person %></td>
<td><%= link_to "Edit", edit_person_path(person) %></td>
<td><%= link_to "Destroy", person, :confirm => 'Are you sure?', :method => :delete %></td>
View
7 app/views/people/show.html.erb
@@ -16,6 +16,13 @@
<%= link_to "Add a New Phone Number", new_phone_number_path(:person_id => @person.id ) %>
<p>
+ <strong>Email Addresses:</strong>
+ <%= render :partial => 'email_addresses', :object => @person.email_addresses %>
+</p>
+
+<%= link_to "Add a New Email Address", new_email_address_path(:person_id => @person.id ), :id => "new_email_address" %>
+
+<p>
<%= link_to "Edit", edit_person_path(@person) %> |
<%= link_to "Destroy", @person, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", people_path %>
View
38 spec/integration/email_addresses_views_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+require 'capybara/rspec'
+
+describe "the views for email addresses", :type => :request do
+ before(:all) do
+ @person = Person.create(:first_name => "John", :last_name => "Doe")
+ end
+
+
+ describe "when looking at the new email address form" do
+ before(:all) do
+ visit new_email_address_path(:person_id => @person.id)
+ end
+
+ it "should redirect to the person's page after successful submission" do
+ sample_address = "tester@email.com"
+ page.fill_in("email_address_address", :with => sample_address)
+ page.click_button("email_address_submit")
+ current_path.should == person_path(@person)
+ page.should have_selector('li', :text => sample_address)
+ end
+ end
+
+ describe "when looking at the edit email address form" do
+ before(:all) do
+ @person.email_addresses.create(:address => "doe@john.com")
+ visit edit_email_address_path(@person.email_addresses.first)
+ end
+
+ it "should redirect to the person's page and show the edited data" do
+ new_address = "john@doe.com"
+ page.fill_in("email_address_address", :with => new_address)
+ page.click_button("email_address_submit")
+ current_path.should == person_path(@person)
+ page.should have_selector('li', :text => new_address)
+ end
+ end
+end
View
44 spec/integration/people_views_spec.rb
@@ -6,13 +6,48 @@
@person = Person.create(:first_name => "John", :last_name => "Doe")
number_a = @person.phone_numbers.create(:number => "1234567")
number_b = @person.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 a single person" do
+ describe "when looking at the list of people" do
before(:all) do
+ visit people_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 person" do
+ before(:each) do
visit person_path(@person)
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
@person.phone_numbers.each do |phone_number|
page.should have_link("edit", :href => edit_phone_number_path(phone_number))
@@ -31,5 +66,12 @@
current_path.should == person_path(@person)
page.should_not have_link("delete", :href => phone_number_path(target))
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
Please sign in to comment.
Something went wrong with that request. Please try again.