Skip to content

Instantly share code, notes, and snippets.

@jonsmock
Created December 22, 2011 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jonsmock/1508427 to your computer and use it in GitHub Desktop.
Save jonsmock/1508427 to your computer and use it in GitHub Desktop.
Oh the conditionals!
This example is not my real app, but it's modeled pretty closely to it.
My mom schedules a couple hundred employees at a local warehouse. In their
system, a worker can earn "points" for being late or missing shifts. Here
we have a sort of summary screen for my mom, who may need to follow up with
new employees or discipline employees with concerning levels of points.
Questions that arise as I code something like this:
- Which objects deserve presenters?
- Should I allow HTML in my presenters?
- Do partials help or do they actually hide complexity that should be
- Is this amount of logic ok in a view, if it's tested? (Views are essentially
methods, right?)
Of course, those kinds of questions make a 2 hour feature take a day...
class Employee < ActiveRecord::Base
include EmployeeStatus
def hired_on; end
def received_employee_handbook?; end
def scheduled_by; end
def last_worked_on; end
end
module EmployeeStatus
def recently_hired?; end
def scheduled?; end
def points_are_concerning?; end
def deliquent_points; end
end
class Roster
attr_accessible :employees
def initialize(employees)
@employees = employees
end
end
class RosterController < ApplicationController
def index
@roster = Roster.new(Employees.morning_shift)
end
end
<ul>
<% @roster.employees.each do |employee| %>
<li>
<h2><%= employee.full_name %></h2>
<% if employee.recently_hired? %>
<p>This employee was hired on <%= employee.hired_on %>
<% if employee.scheduled? %>
and was scheduled by <%= employee.scheduled_by %>!</p>
<%= link_to 'View Shifts', employee_path(employee) %>
<% else %>
but still needs to be scheduled.</p>
<%= link_to 'Schedule now', new_employee_shift_path(employee) %>
<% end %>
<% unless employee.received_employee_handbook? %>
<p>Make sure they have a copy of the handbook!</p>
<% end %>
<% elsif employee.points_are_concerning? %>
<p>This employee needs disciplinary action. They currently have
<%= pluralize(employee.deliquent_points, 'point') %>.</p>
<p>Please consult management before scheduling more shifts.</p>
<% else %>
<% if employee.scheduled? %>
This employee is in good standing. They last worked on <%= employee.last_worked_on %>.
<%= link_to 'View Shifts', employee_path(employee) %>
<% else %>
<p>This employee has never been scheduled.</p>
<%= link_to 'Schedule now', new_employee_shift_path(employee) %>
<% end %>
<% end %>
</li>
<% end %>
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment