From 4f1adbc805b6e7755901a0b29e15f536367b7ac3 Mon Sep 17 00:00:00 2001 From: jimi Date: Tue, 10 Mar 2009 13:48:54 -0500 Subject: [PATCH] Build employee scaffolding --- app/controllers/employees_controller.rb | 44 +++++++++++++++ app/helpers/employees_helper.rb | 2 + app/models/employee.rb | 2 + app/views/employees/_form.html.erb | 40 ++++++++++++++ app/views/employees/edit.html.erb | 8 +++ app/views/employees/index.html.erb | 31 +++++++++++ app/views/employees/new.html.erb | 5 ++ app/views/employees/show.html.erb | 44 +++++++++++++++ config/routes.rb | 2 + ...10055631_add_employee_details_to_person.rb | 23 ++++++++ db/schema.rb | 10 +++- test/functional/employees_controller_test.rb | 54 +++++++++++++++++++ 12 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 app/controllers/employees_controller.rb create mode 100644 app/helpers/employees_helper.rb create mode 100644 app/models/employee.rb create mode 100644 app/views/employees/_form.html.erb create mode 100644 app/views/employees/edit.html.erb create mode 100644 app/views/employees/index.html.erb create mode 100644 app/views/employees/new.html.erb create mode 100644 app/views/employees/show.html.erb create mode 100644 db/migrate/20090310055631_add_employee_details_to_person.rb create mode 100644 test/functional/employees_controller_test.rb diff --git a/app/controllers/employees_controller.rb b/app/controllers/employees_controller.rb new file mode 100644 index 0000000..de2359f --- /dev/null +++ b/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 diff --git a/app/helpers/employees_helper.rb b/app/helpers/employees_helper.rb new file mode 100644 index 0000000..2455a97 --- /dev/null +++ b/app/helpers/employees_helper.rb @@ -0,0 +1,2 @@ +module EmployeesHelper +end diff --git a/app/models/employee.rb b/app/models/employee.rb new file mode 100644 index 0000000..5060a98 --- /dev/null +++ b/app/models/employee.rb @@ -0,0 +1,2 @@ +class Employee < Person +end diff --git a/app/views/employees/_form.html.erb b/app/views/employees/_form.html.erb new file mode 100644 index 0000000..e8f71b7 --- /dev/null +++ b/app/views/employees/_form.html.erb @@ -0,0 +1,40 @@ +<% form_for @employee do |f| %> + <%= f.error_messages %> +

+ <%= f.label :number %>
+ <%= f.text_field :number %> +

+

+ <%= f.label :firstname %>
+ <%= f.text_field :firstname %> +

+

+ <%= f.label :lastname %>
+ <%= f.text_field :lastname %> +

+

+ <%= f.label :ssn %>
+ <%= f.text_field :ssn %> +

+

+ <%= f.label :dob %>
+ <%= f.date_select :dob %> +

+

+ <%= f.label :hired %>
+ <%= f.date_select :hired %> +

+

+ <%= f.label :tax_status %>
+ <%= f.text_field :tax_status %> +

+

+ <%= f.label :dependents %>
+ <%= f.text_field :dependents %> +

+

+ <%= f.label :payrate %>
+ <%= f.text_field :payrate %> +

+

<%= f.submit "Submit" %>

+<% end %> diff --git a/app/views/employees/edit.html.erb b/app/views/employees/edit.html.erb new file mode 100644 index 0000000..aef5e76 --- /dev/null +++ b/app/views/employees/edit.html.erb @@ -0,0 +1,8 @@ +<% title "Edit Employee" %> + +<%= render :partial => 'form' %> + +

+ <%= link_to "Show", @employee %> | + <%= link_to "View All", employees_path %> +

diff --git a/app/views/employees/index.html.erb b/app/views/employees/index.html.erb new file mode 100644 index 0000000..a5d1ad8 --- /dev/null +++ b/app/views/employees/index.html.erb @@ -0,0 +1,31 @@ +<% title "Employees" %> + + + + + + + + + + + + + + <% for employee in @employees %> + + + + + + + + + + + + + <% end %> +
NumberFirst NameLast NameSSNBirth DateHire DateSDPayrate
<%= link_to employee.number, employee %><%=h employee.firstname %><%=h employee.lastname %><%=h employee.ssn %><%= employee.dob %><%= employee.hired %><%=h employee.tax_status %><%= employee.dependents %><%= employee.payrate %><%= link_to "Edit", edit_employee_path(employee) %>
+ +

<%= link_to "New Employee", new_employee_path %>

diff --git a/app/views/employees/new.html.erb b/app/views/employees/new.html.erb new file mode 100644 index 0000000..3e770d6 --- /dev/null +++ b/app/views/employees/new.html.erb @@ -0,0 +1,5 @@ +<% title "New Employee" %> + +<%= render :partial => 'form' %> + +

<%= link_to "Back to List", employees_path %>

diff --git a/app/views/employees/show.html.erb b/app/views/employees/show.html.erb new file mode 100644 index 0000000..fcb5a7c --- /dev/null +++ b/app/views/employees/show.html.erb @@ -0,0 +1,44 @@ +<% title "Employee" %> + +

+ Number: + <%=h @employee.number %> +

+

+ Firstname: + <%=h @employee.firstname %> +

+

+ Lastname: + <%=h @employee.lastname %> +

+

+ SSN: + <%=h @employee.ssn %> +

+

+ DOB: + <%=h @employee.dob %> +

+

+ Hire Date: + <%=h @employee.hired %> +

+

+ Tax Status: + <%=h @employee.tax_status %> +

+

+ Dependents: + <%=h @employee.dependents %> +

+

+ Pay Rate: + <%=h @employee.payrate %> +

+ +

+ <%= link_to "Edit", edit_employee_path(@employee) %> | + <%= link_to "Destroy", @employee, :confirm => 'Are you sure?', :method => :delete %> | + <%= link_to "View All", employees_path %> +

diff --git a/config/routes.rb b/config/routes.rb index 3f5d6fa..4cd8886 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ ActionController::Routing::Routes.draw do |map| + map.resources :employees + map.resources :people # The priority is based upon order of creation: first created -> highest priority. diff --git a/db/migrate/20090310055631_add_employee_details_to_person.rb b/db/migrate/20090310055631_add_employee_details_to_person.rb new file mode 100644 index 0000000..684d860 --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index 060eae1..996426e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -9,13 +9,21 @@ # # 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| t.string "firstname" t.string "lastname" t.datetime "created_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 diff --git a/test/functional/employees_controller_test.rb b/test/functional/employees_controller_test.rb new file mode 100644 index 0000000..b639deb --- /dev/null +++ b/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