Skip to content

Commit

Permalink
Index and new actions for jobs and departments.
Browse files Browse the repository at this point in the history
Test coverage.
  • Loading branch information
phill committed Mar 1, 2016
1 parent 7cf30f4 commit a56dd02
Show file tree
Hide file tree
Showing 40 changed files with 497 additions and 121 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ gem 'bootstrap_form'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem "rspec-rails", "~> 3.1.0"
gem "factory_girl_rails", "~> 4.4.1"
gem 'byebug'
end

Expand Down
24 changes: 24 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,14 @@ GEM
coffee-script-source (1.10.0)
concurrent-ruby (1.0.0)
debug_inspector (0.0.2)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.6.0)
factory_girl (4.4.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.4.1)
factory_girl (~> 4.4.0)
railties (>= 3.0.0)
globalid (0.3.6)
activesupport (>= 4.1.0)
i18n (0.7.0)
Expand Down Expand Up @@ -110,6 +116,22 @@ GEM
rake (10.5.0)
rdoc (4.2.2)
json (~> 1.4)
rspec-core (3.1.7)
rspec-support (~> 3.1.0)
rspec-expectations (3.1.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.1.0)
rspec-mocks (3.1.3)
rspec-support (~> 3.1.0)
rspec-rails (3.1.0)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.1.0)
rspec-expectations (~> 3.1.0)
rspec-mocks (~> 3.1.0)
rspec-support (~> 3.1.0)
rspec-support (3.1.2)
sass (3.4.21)
sass-rails (5.0.4)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -153,10 +175,12 @@ DEPENDENCIES
bootstrap_form
byebug
coffee-rails (~> 4.1.0)
factory_girl_rails (~> 4.4.1)
jbuilder (~> 2.0)
jquery-rails
pg
rails (= 4.2.5)
rspec-rails (~> 3.1.0)
sass-rails (~> 5.0)
sdoc (~> 0.4.0)
spring
Expand Down
33 changes: 8 additions & 25 deletions README.rdoc
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
== README
== Employment agency

This README would normally document whatever steps are necessary to get the
application up and running.
This application keeps a record of HR department's operations.
It implements the following operations:

Things you may want to cover:
* Creation of a new employee
* Editing employee's attributes
* Move an employee to another department or job and change salary
* Dismissal an employee
* Review operations connected with an employee

* Ruby version

* System dependencies

* Configuration

* Database creation

* Database initialization

* How to run the test suite

* Services (job queues, cache servers, search engines, etc.)

* Deployment instructions

* ...


Please feel free to use a different markup language if you do not plan to run
<tt>rake doc:app</tt>.
30 changes: 30 additions & 0 deletions app/controllers/departments_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class DepartmentsController < ApplicationController

# Shows a list of departments
def index
@departments = Department.all
end

# Shows a form for a new department
def new
@department = Department.new
end

# Creates a department
def create
@department = Department.new(department_params)

if @department.save
redirect_to departments_path
else
render 'new'
end
end

private

def department_params
params.require(:department).permit(:name)
end

end
6 changes: 3 additions & 3 deletions app/controllers/employees_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class EmployeesController < ApplicationController

# Shows a list of employees
def index
@employees = Employee.includes(:job, :department).all
@employees = Employee.includes(:job, :department).all.order(id: :desc)
end

# Shows a form for a new employee
Expand Down Expand Up @@ -78,15 +78,15 @@ def employee_edit_params
end

def employee_move_params
employee_params :department_id, :job_id, :salary
employee_params :department_id, :job_id, :salary, :action_date
end

def employee_new_params
employee_edit_params.merge employee_move_params
end

def employee_fire_params
employee_params :is_fired, :dismissal_reason
employee_params :is_fired, :dismissal_reason, :action_date
end

def employee_params(*param_list)
Expand Down
30 changes: 30 additions & 0 deletions app/controllers/jobs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class JobsController < ApplicationController

# Shows a list of jobs
def index
@jobs = Job.all
end

# Shows a form for a new job
def new
@job = Job.new
end

# Creates a job
def create
@job = Job.new(job_params)

if @job.save
redirect_to jobs_path
else
render 'new'
end
end

private

def job_params
params.require(:job).permit(:name)
end

end
7 changes: 6 additions & 1 deletion app/models/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ class Action < ActiveRecord::Base
belongs_to :department_old, class_name: "Department", foreign_key: "department_id_old"
belongs_to :department_new, class_name: "Department", foreign_key: "department_id_new"
belongs_to :job_old, class_name: "Job", foreign_key: "job_id_old"
belongs_to :job_new, class_name: "Job", foreign_key: "job_id_new"
belongs_to :job_new, class_name: "Job", foreign_key: "job_id_new"

# Validations
validates :employee_id, presence: true



def type_text
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/department.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ class Department < ActiveRecord::Base

# Relationships
has_many :employees

# Validations
validates :name, presence: true

end
26 changes: 12 additions & 14 deletions app/models/employee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ class Employee < ActiveRecord::Base
has_many :fire_actions


# Additional atribute that is used for dismissing or moving an employee
attr_accessor :action_date


# Validations
validates :first_name, :last_name, :job_id, :department_id, :salary, presence: true
validates :first_name, :last_name, :job_id, :department_id, :salary, presence: true
validate { errors.add(:action_date, "must be a valid date") unless action_date.blank? || (DateTime.parse(action_date) rescue false) }


# Callbacks
before_update :check_is_fired
after_create :save_hire_action
after_update :save_update_action

attr_accessor :action_date


TRANSFER_FIELDS = [:job_id, :department_id, :salary]


Expand All @@ -30,12 +32,6 @@ def full_name

private

def check_is_fired
return true unless self.is_fired
self.errors.add(:base, "Cannot update a fired employee")
false
end

def save_hire_action
self.hire_actions.create({
:date => self.created_at,
Expand All @@ -54,7 +50,7 @@ def save_update_action
def save_transfer_action(fields)
return if fields.empty?

changes = { :date => self.updated_at }
changes = { :date => get_action_date }
fields.each do |k, v|
changes["#{k}_old"] = Array.wrap(v).first
changes["#{k}_new"] = Array.wrap(v).last
Expand All @@ -63,9 +59,11 @@ def save_transfer_action(fields)
end

def save_dismiss_action
self.fire_actions.create({ :date => self.updated_at })
self.fire_actions.create({ :date => get_action_date })
end


def get_action_date
self.action_date.present? ? self.action_date : self.updated_at
end

end
3 changes: 3 additions & 0 deletions app/models/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@ class Job < ActiveRecord::Base

# Relationships
has_many :employees

# Validations
validates :name, presence: true

end
3 changes: 3 additions & 0 deletions app/views/departments/_department.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<tr>
<td><%= department.name %></td>
</tr>
18 changes: 18 additions & 0 deletions app/views/departments/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="page-header">
<h1>Departments</h1>
</div>

<p>
<a class="btn btn-success" href="<%= new_department_path %>">Add a department</a>
</p>

<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<%= render :partial => @departments %>
</tbody>
</table>
14 changes: 14 additions & 0 deletions app/views/departments/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="page-header">
<h2>New department</h2>
</div>

<%= bootstrap_form_for @department do |f| %>

<%= f.text_field :name %>

<div class="form-actions">
<%= f.submit "Create", class: "btn btn-primary" %>
<%= link_to "Return", departments_path, class: "btn btn-default" %>
</div>

<% end %>
4 changes: 2 additions & 2 deletions app/views/employees/_action.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<tr>
<td><%= action.date %></td>
<td><%= action.type_text %></td>
<td><span class="label label-<%= action.type_class %>"><%= action.type_text %></span></td>
<td><%= display_changes(action.department_old.try(:name), action.department_new.try(:name)) %></td>
<td><%= display_changes(action.job_old.try(:name), action.job_new.try(:name)) %></td>
<td><%= display_changes(action.salary_old, action.salary_new) %></td>
<td><%= display_changes(number_with_delimiter(action.salary_old), number_with_delimiter(action.salary_new)) %></td>
</tr>
1 change: 1 addition & 0 deletions app/views/employees/dismiss.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<%= bootstrap_form_for @employee, :url => fire_employee_path do |f| %>

<%= f.hidden_field :is_fired, :value => 1 %>
<%= f.text_field :action_date, :value => @employee.action_date || DateTime.now.to_s(:db) %>
<%= f.text_area :dismissal_reason %>

<div class="form-actions">
Expand Down
1 change: 1 addition & 0 deletions app/views/employees/transfer.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

<%= bootstrap_form_for @employee, :url => move_employee_path do |f| %>

<%= f.text_field :action_date, :value => @employee.action_date || DateTime.now.to_s(:db) %>
<%= f.collection_select :department_id, Department.all, :id, :name, :prompt => 'Choose a department' %>
<%= f.collection_select :job_id, Job.all, :id, :name, :prompt => 'Choose a job', :class => "form-control" %>
<%= f.number_field :salary, :step => 1 %>
Expand Down
3 changes: 3 additions & 0 deletions app/views/jobs/_job.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<tr>
<td><%= job.name %></td>
</tr>
18 changes: 18 additions & 0 deletions app/views/jobs/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="page-header">
<h1>Jobs</h1>
</div>

<p>
<a class="btn btn-success" href="<%= new_job_path %>">Add a job</a>
</p>

<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<%= render :partial => @jobs %>
</tbody>
</table>
14 changes: 14 additions & 0 deletions app/views/jobs/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="page-header">
<h2>New job</h2>
</div>

<%= bootstrap_form_for @job do |f| %>

<%= f.text_field :name %>

<div class="form-actions">
<%= f.submit "Create", class: "btn btn-primary" %>
<%= link_to "Return", jobs_path, class: "btn btn-default" %>
</div>

<% end %>
7 changes: 7 additions & 0 deletions app/views/layouts/_menu.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<div id="navbar" class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><%= link_to "Employees", employees_path %></li>
<li><%= link_to "Departments", departments_path %></li>
<li><%= link_to "Jobs", jobs_path %></li>
</ul>
</div>
Loading

0 comments on commit a56dd02

Please sign in to comment.