Browse Source

Build employee scaffolding

master
jimi 16 years ago
committed by jimi
parent
commit
4f1adbc805
  1. 44
      app/controllers/employees_controller.rb
  2. 2
      app/helpers/employees_helper.rb
  3. 2
      app/models/employee.rb
  4. 40
      app/views/employees/_form.html.erb
  5. 8
      app/views/employees/edit.html.erb
  6. 31
      app/views/employees/index.html.erb
  7. 5
      app/views/employees/new.html.erb
  8. 44
      app/views/employees/show.html.erb
  9. 2
      config/routes.rb
  10. 23
      db/migrate/20090310055631_add_employee_details_to_person.rb
  11. 10
      db/schema.rb
  12. 54
      test/functional/employees_controller_test.rb

44
app/controllers/employees_controller.rb

@ -0,0 +1,44 @@
class EmployeesController < ApplicationController
def index
@employees = Employee.all
end
def show
@employee = Employee.find(params[:id])
end
def new
@employee = Employee.new
end
def create
@employee = Employee.new(params[:employee])
if @employee.save
flash[:notice] = "Successfully created employee."
redirect_to @employee
else
render :action => 'new'
end
end
def edit
@employee = Employee.find(params[:id])
end
def update
@employee = Employee.find(params[:id])
if @employee.update_attributes(params[:employee])
flash[:notice] = "Successfully updated employee."
redirect_to @employee
else
render :action => 'edit'
end
end
def destroy
@employee = Employee.find(params[:id])
@employee.destroy
flash[:notice] = "Successfully destroyed employee."
redirect_to employees_url
end
end

2
app/helpers/employees_helper.rb

@ -0,0 +1,2 @@
module EmployeesHelper
end

2
app/models/employee.rb

@ -0,0 +1,2 @@
class Employee < Person
end

40
app/views/employees/_form.html.erb

@ -0,0 +1,40 @@
<% form_for @employee do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :number %><br />
<%= f.text_field :number %>
</p>
<p>
<%= f.label :firstname %><br />
<%= f.text_field :firstname %>
</p>
<p>
<%= f.label :lastname %><br />
<%= f.text_field :lastname %>
</p>
<p>
<%= f.label :ssn %><br />
<%= f.text_field :ssn %>
</p>
<p>
<%= f.label :dob %><br />
<%= f.date_select :dob %>
</p>
<p>
<%= f.label :hired %><br />
<%= f.date_select :hired %>
</p>
<p>
<%= f.label :tax_status %><br />
<%= f.text_field :tax_status %>
</p>
<p>
<%= f.label :dependents %><br />
<%= f.text_field :dependents %>
</p>
<p>
<%= f.label :payrate %><br />
<%= f.text_field :payrate %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>

8
app/views/employees/edit.html.erb

@ -0,0 +1,8 @@
<% title "Edit Employee" %>
<%= render :partial => 'form' %>
<p>
<%= link_to "Show", @employee %> |
<%= link_to "View All", employees_path %>
</p>

31
app/views/employees/index.html.erb

@ -0,0 +1,31 @@
<% title "Employees" %>
<table>
<tr>
<th>Number</th>
<th>First Name</th>
<th>Last Name</th>
<th>SSN</th>
<th>Birth Date</th>
<th>Hire Date</th>
<th>S</th>
<th>D</th>
<th>Payrate</th>
</tr>
<% for employee in @employees %>
<tr>
<td><%= link_to employee.number, employee %></td>
<td><%=h employee.firstname %></td>
<td><%=h employee.lastname %></td>
<td><%=h employee.ssn %></td>
<td><%= employee.dob %></td>
<td><%= employee.hired %></td>
<td><%=h employee.tax_status %></td>
<td><%= employee.dependents %></td>
<td><%= employee.payrate %></td>
<td><%= link_to "Edit", edit_employee_path(employee) %></td>
</tr>
<% end %>
</table>
<p><%= link_to "New Employee", new_employee_path %></p>

5
app/views/employees/new.html.erb

@ -0,0 +1,5 @@
<% title "New Employee" %>
<%= render :partial => 'form' %>
<p><%= link_to "Back to List", employees_path %></p>

44
app/views/employees/show.html.erb

@ -0,0 +1,44 @@
<% title "Employee" %>
<p>
<strong>Number:</strong>
<%=h @employee.number %>
</p>
<p>
<strong>Firstname:</strong>
<%=h @employee.firstname %>
</p>
<p>
<strong>Lastname:</strong>
<%=h @employee.lastname %>
</p>
<p>
<strong>SSN:</strong>
<%=h @employee.ssn %>
</p>
<p>
<strong>DOB:</strong>
<%=h @employee.dob %>
</p>
<p>
<strong>Hire Date:</strong>
<%=h @employee.hired %>
</p>
<p>
<strong>Tax Status:</strong>
<%=h @employee.tax_status %>
</p>
<p>
<strong>Dependents:</strong>
<%=h @employee.dependents %>
</p>
<p>
<strong>Pay Rate:</strong>
<%=h @employee.payrate %>
</p>
<p>
<%= link_to "Edit", edit_employee_path(@employee) %> |
<%= link_to "Destroy", @employee, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", employees_path %>
</p>

2
config/routes.rb

@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.resources :employees
map.resources :people map.resources :people
# The priority is based upon order of creation: first created -> highest priority. # The priority is based upon order of creation: first created -> highest priority.

23
db/migrate/20090310055631_add_employee_details_to_person.rb

@ -0,0 +1,23 @@
class AddEmployeeDetailsToPerson < ActiveRecord::Migration
def self.up
add_column :people, :type, :string
add_column :people, :number, :string
add_column :people, :ssn, :string
add_column :people, :dob, :date
add_column :people, :hired, :date
add_column :people, :tax_status, :string
add_column :people, :dependents, :integer, :default => 0
add_column :people, :payrate, :decimal, :precision => 4, :scale => 2, :default => 0.25
end
def self.down
remove_column :people, :payrate
remove_column :people, :dependents
remove_column :people, :tax_status
remove_column :people, :hired
remove_column :people, :dob
remove_column :people, :ssn
remove_column :people, :number
remove_column :people, :type
end
end

10
db/schema.rb

@ -9,13 +9,21 @@
# #
# It's strongly recommended to check this file into your version control system. # It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20090306144503) do
ActiveRecord::Schema.define(:version => 20090310055631) do
create_table "people", :force => true do |t| create_table "people", :force => true do |t|
t.string "firstname" t.string "firstname"
t.string "lastname" t.string "lastname"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
t.string "type"
t.string "number"
t.string "ssn"
t.date "dob"
t.date "hired"
t.string "tax_status"
t.integer "dependents", :default => 0
t.decimal "payrate", :precision => 4, :scale => 2, :default => 0.25
end end
end end

54
test/functional/employees_controller_test.rb

@ -0,0 +1,54 @@
require 'test_helper'
class EmployeesControllerTest < ActionController::TestCase
def test_index
get :index
assert_template 'index'
end
def test_show
get :show, :id => Employee.first
assert_template 'show'
end
def test_new
get :new
assert_template 'new'
end
def test_create_invalid
Employee.any_instance.stubs(:valid?).returns(false)
post :create
assert_template 'new'
end
def test_create_valid
Employee.any_instance.stubs(:valid?).returns(true)
post :create
assert_redirected_to employee_url(assigns(:employee))
end
def test_edit
get :edit, :id => Employee.first
assert_template 'edit'
end
def test_update_invalid
Employee.any_instance.stubs(:valid?).returns(false)
put :update, :id => Employee.first
assert_template 'edit'
end
def test_update_valid
Employee.any_instance.stubs(:valid?).returns(true)
put :update, :id => Employee.first
assert_redirected_to employee_url(assigns(:employee))
end
def test_destroy
employee = Employee.first
delete :destroy, :id => employee
assert_redirected_to employees_url
assert !Employee.exists?(employee.id)
end
end