Skip to content

Commit

Permalink
Update people controller to have search actually work
Browse files Browse the repository at this point in the history
And new fields for the CSV download

Issue #301: Built-in report pages, to replace dependency on external query
            dashboards.
  • Loading branch information
Frank Duncan committed Feb 24, 2021
1 parent 3dc9f84 commit 2dc1deb
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 36 deletions.
42 changes: 35 additions & 7 deletions app/controllers/scheduler/people_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,30 @@ class Scheduler::PeopleController < Scheduler::BaseController
load_and_authorize_resource class: Roster::Person

has_scope :name_contains
has_scope :name_gteq
has_scope :in_shift_territory, as: :shift_territory, default: Proc.new {|controller| controller.current_user.primary_shift_territory_id}
#has_scope :in_shift_territory, as: :shift_territory do |controller, scope, val|
# positions = Scheduler::Shift.where(shift_territory_id: val).map{|sh| sh.positions}.flatten
# scope.in_shift_territory(val)
#end

# , default: Proc.new {|controller| controller.current_region.positions.where(name: ['DAT Team Lead', 'DAT Technician', 'DAT Trainee', 'DAT Dispatcher']).map(&:id)}
has_scope :with_position, type: :array, default: []
has_scope :last_shift do |controller, scope, val|
scope.where(Scheduler::ShiftAssignment.where(person_id: {roster_people: :id}).where('date > ?', Date.current-val.to_i).exists.not)
has_scope :date_after, :allow_blank => true, :default => FiscalYear.current.start_date.to_s do |controller, scope, val|
if not controller.params[:date_before].blank?
scope.where(Scheduler::ShiftAssignment.where("person_id = roster_people.id").where('date > ?', val).where('date < ?', controller.params[:date_before]).exists)
elsif not val.blank?
scope.where(Scheduler::ShiftAssignment.where("person_id = roster_people.id").where('date > ?', val).exists)
else
scope.where(Scheduler::ShiftAssignment.where("person_id = roster_people.id").exists)
end
end
has_scope :date_before do |controller, scope, val|
# This is a hack, I guess, but I don't know how to do it otherwise
# If there date_after is present, we have to let that scope handle everything
if not controller.params[:date_after].blank?
scope
elsif not controller.params[:date_before].blank?
scope.where(Scheduler::ShiftAssignment.where("person_id = roster_people.id").where('date < ?', val).exists)
else
scope
end
end

def collection
Expand All @@ -32,14 +46,28 @@ def prev_shift person

helper_method :num_shifts
def num_shifts person
person.shift_assignments.where("date < ?", Date.current).count
assignments = person.shift_assignments
if params[:date_after].nil?
assignments = assignments.where("date > ?", FiscalYear.current.start_date.to_s)
elsif not params[:date_after].blank?
assignments = assignments.where("date > ?", params[:date_after])
end
if not params[:date_before].blank?
assignments = assignments.where("date < ?", params[:date_before])
end
assignments.count
end

helper_method :next_shift
def next_shift person
person.shift_assignments.where("date >= ?", Date.current).minimum('date')
end

def default_search_params
{date_gteq: FiscalYear.current.start_date}
t
end

helper_method :date_ranges
def date_ranges
[ ["Now", -2],
Expand Down
4 changes: 0 additions & 4 deletions app/helpers/scheduler/people_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,4 @@ def flex_schedules
ids = collection.to_a.map(&:id)
@flex_schedules ||= Scheduler::FlexSchedule.where(id: ids).group_by(&:id)
end

def flex_schedule_for person_id
flex_schedules[person_id].try :first
end
end
13 changes: 1 addition & 12 deletions app/views/scheduler/people/_index_table.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
%th Num Shifts
%th Prev Shift
%th Next Shift
%th Last Login
%th Flex Schedule
%th=t '.actions', :default => t("helpers.actions")
%tbody
- collection.each do |person|
Expand All @@ -30,21 +28,12 @@
%td
-if d = next_shift(person)
=d.to_s :dow_short
%td
-if d = person.last_login
=d.to_s :dow_short
%br
(#{(Date.current-d.to_date).to_i} days ago)
%td
= flex_schedule_for(person.id).try(:num_shifts)
%td.small
-if flex = can?( :update, Scheduler::FlexSchedule.new(person: person))
= link_to "Flex Schedule", scheduler_flex_schedule_path(person), :class => 'btn btn-xs btn-default'
-if reminders = can?( :update, Scheduler::NotificationSetting.new(person: person))
= link_to "Reminders", scheduler_notification_setting_path(person), :class => 'btn btn-xs btn-default'
- if flex or reminders
%br
= link_to "Shifts", scheduler_person_shift_assignments_path(person), :class => 'btn btn-xs btn-default'
= link_to "Email", person.vc_email_url, :class => 'btn btn-xs btn-default'
= link_to "Email", "mailto:#{person.email}", :class => 'btn btn-xs btn-default'

.text-center= paginate collection, :theme => 'twitter-bootstrap-3', remote: true
12 changes: 2 additions & 10 deletions app/views/scheduler/people/index.csv.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%- headers = ['Name', 'Positions', 'Num Shifts', 'Prev Shift', 'Next Shift', 'Last Login', 'Flex Schedule'] -%>
<%- headers = ['Name', 'Positions', 'Num Shifts', 'Prev Shift', 'Next Shift'] -%>
<%= CSV.generate_line headers -%>
<%- collection.each do |person|
d = prev_shift(person)
Expand All @@ -13,20 +13,12 @@
else
next_shift = ""
end
d = person.last_login
if d
last_login = d.to_s :dow_short
else
last_login = ""
end
positions = person.positions.select{|p|!p.hidden}.map(&:name).join ", "
-%>
<%= CSV.generate_line([person.full_name,
positions,
pluralize(num_shifts(person), 'shift'),
prev_shift,
next_shift,
last_login,
flex_schedule_for(person.id).try(:num_shifts)
next_shift
]).html_safe -%>
<%- end -%>
9 changes: 6 additions & 3 deletions app/views/scheduler/people/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@
%tr
%th Shift Territory
%td=select_tag "shift_territory", options_from_collection_for_select(current_region.shift_territories.enabled, :id, :name, current_scopes[:shift_territory]), multiple: false, id: 'choose-shift_territory', prompt: "All", class: "form-control"
%tr
%th Shifts After
%td=date_field_tag "date_after", (current_scopes[:date_after] or FiscalYear.current.start_date.to_s), class: 'form-control', placeholder: 'After YYYY-MM-DD'
%tr
%th Shifts Before
%td=date_field_tag "date_before", current_scopes[:date_before], class: 'form-control', placeholder: 'Before YYYY-MM-DD'
%tr
%th Position
%td
Expand All @@ -26,9 +32,6 @@
- checked = current_scopes[:with_position] && current_scopes[:with_position].include?(second.id.to_s)
=check_box_tag "with_position[]", second.id, checked
= second.name
%tr
%th Last Shift Taken
%td=select_tag "last_shift", options_for_select(date_ranges, current_scopes[:last_shift].try(:to_i)), prompt: "Any Time", class: "form-control"
%tr
%th Name
%td=text_field_tag :name_contains, current_scopes[:name_contains], placeholder: "Search Name", class: "form-control"
Expand Down

0 comments on commit 2dc1deb

Please sign in to comment.