Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Created email address model and relationships

  • Loading branch information...
commit 1fdd0b1f5b82ced01e7209b9ba97c4fec7cb8820 1 parent 66b6012
Jeff Casimir authored April 19, 2011
41  app/controllers/email_addresses_controller.rb
... ...
@@ -0,0 +1,41 @@
  1
+class EmailAddressesController < ApplicationController
  2
+  def index
  3
+    @email_addresses = EmailAddress.all
  4
+  end
  5
+
  6
+  def show
  7
+    @email_address = EmailAddress.find(params[:id])
  8
+  end
  9
+
  10
+  def new
  11
+    @email_address = EmailAddress.new
  12
+  end
  13
+
  14
+  def create
  15
+    @email_address = EmailAddress.new(params[:email_address])
  16
+    if @email_address.save
  17
+      redirect_to @email_address, :notice => "Successfully created email address."
  18
+    else
  19
+      render :action => 'new'
  20
+    end
  21
+  end
  22
+
  23
+  def edit
  24
+    @email_address = EmailAddress.find(params[:id])
  25
+  end
  26
+
  27
+  def update
  28
+    @email_address = EmailAddress.find(params[:id])
  29
+    if @email_address.update_attributes(params[:email_address])
  30
+      redirect_to @email_address, :notice  => "Successfully updated email address."
  31
+    else
  32
+      render :action => 'edit'
  33
+    end
  34
+  end
  35
+
  36
+  def destroy
  37
+    @email_address = EmailAddress.find(params[:id])
  38
+    @email_address.destroy
  39
+    redirect_to email_addresses_url, :notice => "Successfully destroyed email address."
  40
+  end
  41
+end
2  app/helpers/email_addresses_helper.rb
... ...
@@ -0,0 +1,2 @@
  1
+module EmailAddressesHelper
  2
+end
5  app/models/email_address.rb
... ...
@@ -0,0 +1,5 @@
  1
+class EmailAddress < ActiveRecord::Base
  2
+  attr_accessible :address, :person_id
  3
+  
  4
+  belongs_to :person
  5
+end
3  app/models/person.rb
@@ -3,5 +3,6 @@ class Person < ActiveRecord::Base
3 3
   
4 4
   validates_presence_of :first_name, :last_name
5 5
   
6  
-  has_many :phone_numbers
  6
+  has_many :phone_numbers  
  7
+  has_many :email_addresses
7 8
 end
12  app/views/email_addresses/_form.html.erb
... ...
@@ -0,0 +1,12 @@
  1
+<%= form_for @email_address do |f| %>
  2
+  <%= f.error_messages %>
  3
+  <p>
  4
+    <%= f.label :address %><br />
  5
+    <%= f.text_field :address %>
  6
+  </p>
  7
+  <p>
  8
+    <%= f.label :person_id %><br />
  9
+    <%= f.text_field :person_id %>
  10
+  </p>
  11
+  <p><%= f.submit %></p>
  12
+<% end %>
8  app/views/email_addresses/edit.html.erb
... ...
@@ -0,0 +1,8 @@
  1
+<% title "Edit Email Address" %>
  2
+
  3
+<%= render 'form' %>
  4
+
  5
+<p>
  6
+  <%= link_to "Show", @email_address %> |
  7
+  <%= link_to "View All", email_addresses_path %>
  8
+</p>
19  app/views/email_addresses/index.html.erb
... ...
@@ -0,0 +1,19 @@
  1
+<% title "Email Addresses" %>
  2
+
  3
+<table>
  4
+  <tr>
  5
+    <th>Address</th>
  6
+    <th>Person</th>
  7
+  </tr>
  8
+  <% for email_address in @email_addresses %>
  9
+    <tr>
  10
+      <td><%= email_address.address %></td>
  11
+      <td><%= email_address.person_id %></td>
  12
+      <td><%= link_to "Show", email_address %></td>
  13
+      <td><%= link_to "Edit", edit_email_address_path(email_address) %></td>
  14
+      <td><%= link_to "Destroy", email_address, :confirm => 'Are you sure?', :method => :delete %></td>
  15
+    </tr>
  16
+  <% end %>
  17
+</table>
  18
+
  19
+<p><%= link_to "New Email Address", new_email_address_path %></p>
5  app/views/email_addresses/new.html.erb
... ...
@@ -0,0 +1,5 @@
  1
+<% title "New Email Address" %>
  2
+
  3
+<%= render 'form' %>
  4
+
  5
+<p><%= link_to "Back to List", email_addresses_path %></p>
16  app/views/email_addresses/show.html.erb
... ...
@@ -0,0 +1,16 @@
  1
+<% title "Email Address" %>
  2
+
  3
+<p>
  4
+  <strong>Address:</strong>
  5
+  <%= @email_address.address %>
  6
+</p>
  7
+<p>
  8
+  <strong>Person:</strong>
  9
+  <%= @email_address.person_id %>
  10
+</p>
  11
+
  12
+<p>
  13
+  <%= link_to "Edit", edit_email_address_path(@email_address) %> |
  14
+  <%= link_to "Destroy", @email_address, :confirm => 'Are you sure?', :method => :delete %> |
  15
+  <%= link_to "View All", email_addresses_path %>
  16
+</p>
2  config/routes.rb
... ...
@@ -1,4 +1,6 @@
1 1
 JSContact::Application.routes.draw do
  2
+  resources :email_addresses
  3
+
2 4
   resources :phone_numbers
3 5
 
4 6
   resources :people
13  db/migrate/20110419213031_create_email_addresses.rb
... ...
@@ -0,0 +1,13 @@
  1
+class CreateEmailAddresses < ActiveRecord::Migration
  2
+  def self.up
  3
+    create_table :email_addresses do |t|
  4
+      t.string :address
  5
+      t.integer :person_id
  6
+      t.timestamps
  7
+    end
  8
+  end
  9
+
  10
+  def self.down
  11
+    drop_table :email_addresses
  12
+  end
  13
+end
9  db/schema.rb
@@ -10,7 +10,14 @@
10 10
 #
11 11
 # It's strongly recommended to check this file into your version control system.
12 12
 
13  
-ActiveRecord::Schema.define(:version => 20110419134754) do
  13
+ActiveRecord::Schema.define(:version => 20110419213031) do
  14
+
  15
+  create_table "email_addresses", :force => true do |t|
  16
+    t.string   "address"
  17
+    t.integer  "person_id"
  18
+    t.datetime "created_at"
  19
+    t.datetime "updated_at"
  20
+  end
14 21
 
15 22
   create_table "people", :force => true do |t|
16 23
     t.string   "first_name"
57  spec/controllers/email_addresses_controller_spec.rb
... ...
@@ -0,0 +1,57 @@
  1
+# require File.dirname(__FILE__) + '/../spec_helper'
  2
+# 
  3
+# describe EmailAddressesController do
  4
+#   fixtures :all
  5
+#   render_views
  6
+# 
  7
+#   it "index action should render index template" do
  8
+#     get :index
  9
+#     response.should render_template(:index)
  10
+#   end
  11
+# 
  12
+#   it "show action should render show template" do
  13
+#     get :show, :id => EmailAddress.first
  14
+#     response.should render_template(:show)
  15
+#   end
  16
+# 
  17
+#   it "new action should render new template" do
  18
+#     get :new
  19
+#     response.should render_template(:new)
  20
+#   end
  21
+# 
  22
+#   it "create action should render new template when model is invalid" do
  23
+#     EmailAddress.any_instance.stubs(:valid?).returns(false)
  24
+#     post :create
  25
+#     response.should render_template(:new)
  26
+#   end
  27
+# 
  28
+#   it "create action should redirect when model is valid" do
  29
+#     EmailAddress.any_instance.stubs(:valid?).returns(true)
  30
+#     post :create
  31
+#     response.should redirect_to(email_address_url(assigns[:email_address]))
  32
+#   end
  33
+# 
  34
+#   it "edit action should render edit template" do
  35
+#     get :edit, :id => EmailAddress.first
  36
+#     response.should render_template(:edit)
  37
+#   end
  38
+# 
  39
+#   it "update action should render edit template when model is invalid" do
  40
+#     EmailAddress.any_instance.stubs(:valid?).returns(false)
  41
+#     put :update, :id => EmailAddress.first
  42
+#     response.should render_template(:edit)
  43
+#   end
  44
+# 
  45
+#   it "update action should redirect when model is valid" do
  46
+#     EmailAddress.any_instance.stubs(:valid?).returns(true)
  47
+#     put :update, :id => EmailAddress.first
  48
+#     response.should redirect_to(email_address_url(assigns[:email_address]))
  49
+#   end
  50
+# 
  51
+#   it "destroy action should destroy model and redirect to index action" do
  52
+#     email_address = EmailAddress.first
  53
+#     delete :destroy, :id => email_address
  54
+#     response.should redirect_to(email_addresses_url)
  55
+#     EmailAddress.exists?(email_address.id).should be_false
  56
+#   end
  57
+# end
7  spec/fixtures/email_addresses.yml
... ...
@@ -0,0 +1,7 @@
  1
+one:
  2
+  address: MyString
  3
+  person_id: 1
  4
+
  5
+two:
  6
+  address: MyString
  7
+  person_id: 1
7  spec/models/email_address_spec.rb
... ...
@@ -0,0 +1,7 @@
  1
+require File.dirname(__FILE__) + '/../spec_helper'
  2
+
  3
+describe EmailAddress do
  4
+  it "should be valid" do
  5
+    EmailAddress.new.should be_valid
  6
+  end
  7
+end
5  spec/models/person_spec.rb
@@ -23,5 +23,10 @@
23 23
   it "should have an array of phone numbers" do
24 24
     @person.phone_numbers.class.should == Array    
25 25
   end
  26
+
  27
+  it "should have an array of email addresses" do
  28
+    @person.email_addresses.class.should == Array    
  29
+  end
  30
+
26 31
   
27 32
 end

0 notes on commit 1fdd0b1

Please sign in to comment.
Something went wrong with that request. Please try again.