Permalink
Browse files

Created email address model and relationships

  • Loading branch information...
1 parent 66b6012 commit 1fdd0b1f5b82ced01e7209b9ba97c4fec7cb8820 @jcasimir committed Apr 19, 2011
@@ -0,0 +1,41 @@
+class EmailAddressesController < ApplicationController
+ def index
+ @email_addresses = EmailAddress.all
+ end
+
+ def show
+ @email_address = EmailAddress.find(params[:id])
+ end
+
+ def new
+ @email_address = EmailAddress.new
+ end
+
+ def create
+ @email_address = EmailAddress.new(params[:email_address])
+ if @email_address.save
+ redirect_to @email_address, :notice => "Successfully created email address."
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @email_address = EmailAddress.find(params[:id])
+ end
+
+ 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."
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @email_address = EmailAddress.find(params[:id])
+ @email_address.destroy
+ redirect_to email_addresses_url, :notice => "Successfully destroyed email address."
+ end
+end
@@ -0,0 +1,2 @@
+module EmailAddressesHelper
+end
@@ -0,0 +1,5 @@
+class EmailAddress < ActiveRecord::Base
+ attr_accessible :address, :person_id
+
+ belongs_to :person
+end
@@ -3,5 +3,6 @@ class Person < ActiveRecord::Base
validates_presence_of :first_name, :last_name
- has_many :phone_numbers
+ has_many :phone_numbers
+ has_many :email_addresses
end
@@ -0,0 +1,12 @@
+<%= form_for @email_address do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :address %><br />
+ <%= f.text_field :address %>
+ </p>
+ <p>
+ <%= f.label :person_id %><br />
+ <%= f.text_field :person_id %>
+ </p>
+ <p><%= f.submit %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Email Address" %>
+
+<%= render 'form' %>
+
+<p>
+ <%= link_to "Show", @email_address %> |
+ <%= link_to "View All", email_addresses_path %>
+</p>
@@ -0,0 +1,19 @@
+<% title "Email Addresses" %>
+
+<table>
+ <tr>
+ <th>Address</th>
+ <th>Person</th>
+ </tr>
+ <% for email_address in @email_addresses %>
+ <tr>
+ <td><%= email_address.address %></td>
+ <td><%= email_address.person_id %></td>
+ <td><%= link_to "Show", email_address %></td>
+ <td><%= link_to "Edit", edit_email_address_path(email_address) %></td>
+ <td><%= link_to "Destroy", email_address, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New Email Address", new_email_address_path %></p>
@@ -0,0 +1,5 @@
+<% title "New Email Address" %>
+
+<%= render 'form' %>
+
+<p><%= link_to "Back to List", email_addresses_path %></p>
@@ -0,0 +1,16 @@
+<% title "Email Address" %>
+
+<p>
+ <strong>Address:</strong>
+ <%= @email_address.address %>
+</p>
+<p>
+ <strong>Person:</strong>
+ <%= @email_address.person_id %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_email_address_path(@email_address) %> |
+ <%= link_to "Destroy", @email_address, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", email_addresses_path %>
+</p>
View
@@ -1,4 +1,6 @@
JSContact::Application.routes.draw do
+ resources :email_addresses
+
resources :phone_numbers
resources :people
@@ -0,0 +1,13 @@
+class CreateEmailAddresses < ActiveRecord::Migration
+ def self.up
+ create_table :email_addresses do |t|
+ t.string :address
+ t.integer :person_id
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :email_addresses
+ end
+end
View
@@ -10,7 +10,14 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110419134754) do
+ActiveRecord::Schema.define(:version => 20110419213031) do
+
+ create_table "email_addresses", :force => true do |t|
+ t.string "address"
+ t.integer "person_id"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
create_table "people", :force => true do |t|
t.string "first_name"
@@ -0,0 +1,57 @@
+# require File.dirname(__FILE__) + '/../spec_helper'
+#
+# describe EmailAddressesController do
+# fixtures :all
+# render_views
+#
+# it "index action should render index template" do
+# get :index
+# response.should render_template(:index)
+# end
+#
+# it "show action should render show template" do
+# get :show, :id => EmailAddress.first
+# response.should render_template(:show)
+# end
+#
+# it "new action should render new template" do
+# get :new
+# response.should render_template(:new)
+# end
+#
+# it "create action should render new template when model is invalid" do
+# EmailAddress.any_instance.stubs(:valid?).returns(false)
+# post :create
+# response.should render_template(:new)
+# end
+#
+# it "create action should redirect when model is valid" do
+# EmailAddress.any_instance.stubs(:valid?).returns(true)
+# post :create
+# response.should redirect_to(email_address_url(assigns[:email_address]))
+# end
+#
+# it "edit action should render edit template" do
+# get :edit, :id => EmailAddress.first
+# response.should render_template(:edit)
+# end
+#
+# it "update action should render edit template when model is invalid" do
+# EmailAddress.any_instance.stubs(:valid?).returns(false)
+# put :update, :id => EmailAddress.first
+# response.should render_template(:edit)
+# end
+#
+# it "update action should redirect when model is valid" do
+# EmailAddress.any_instance.stubs(:valid?).returns(true)
+# put :update, :id => EmailAddress.first
+# response.should redirect_to(email_address_url(assigns[:email_address]))
+# end
+#
+# it "destroy action should destroy model and redirect to index action" do
+# email_address = EmailAddress.first
+# delete :destroy, :id => email_address
+# response.should redirect_to(email_addresses_url)
+# EmailAddress.exists?(email_address.id).should be_false
+# end
+# end
@@ -0,0 +1,7 @@
+one:
+ address: MyString
+ person_id: 1
+
+two:
+ address: MyString
+ person_id: 1
@@ -0,0 +1,7 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe EmailAddress do
+ it "should be valid" do
+ EmailAddress.new.should be_valid
+ end
+end
@@ -23,5 +23,10 @@
it "should have an array of phone numbers" do
@person.phone_numbers.class.should == Array
end
+
+ it "should have an array of email addresses" do
+ @person.email_addresses.class.should == Array
+ end
+
end

0 comments on commit 1fdd0b1

Please sign in to comment.