jimi
16 years ago
committed by
jimi
12 changed files with 264 additions and 1 deletions
-
44app/controllers/employees_controller.rb
-
2app/helpers/employees_helper.rb
-
2app/models/employee.rb
-
40app/views/employees/_form.html.erb
-
8app/views/employees/edit.html.erb
-
31app/views/employees/index.html.erb
-
5app/views/employees/new.html.erb
-
44app/views/employees/show.html.erb
-
2config/routes.rb
-
23db/migrate/20090310055631_add_employee_details_to_person.rb
-
10db/schema.rb
-
54test/functional/employees_controller_test.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 |
@ -0,0 +1,2 @@ |
|||
module EmployeesHelper |
|||
end |
@ -0,0 +1,2 @@ |
|||
class Employee < Person |
|||
end |
@ -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 %> |
@ -0,0 +1,8 @@ |
|||
<% title "Edit Employee" %> |
|||
|
|||
<%= render :partial => 'form' %> |
|||
|
|||
<p> |
|||
<%= link_to "Show", @employee %> | |
|||
<%= link_to "View All", employees_path %> |
|||
</p> |
@ -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> |
@ -0,0 +1,5 @@ |
|||
<% title "New Employee" %> |
|||
|
|||
<%= render :partial => 'form' %> |
|||
|
|||
<p><%= link_to "Back to List", employees_path %></p> |
@ -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> |
@ -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 |
@ -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 |
Reference in new issue