Skip to content
This repository has been archived by the owner on May 13, 2020. It is now read-only.

Issue 32: Admin can see users timesheets #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/assets/javascripts/partials/timesheet.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ jQuery ->
$("#datepicker").datepicker
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@henriquelampert I see that there's only one route for "timesheet#show". So there's no need to rename the path to "/timesheet_for/". Please, revert this and use "/timesheet/" again.

onSelect: (dateText, inst) ->
[month, day, year] = dateText.split('/')
window.location = "/timesheet/#{year}/#{month}/#{day}"
window.location = "/timesheet/" + timesheet_user_id + "/#{year}/#{month}/#{day}"

$(".icon-calendar").on 'click', ->
$('#datepicker').toggle()
14 changes: 13 additions & 1 deletion app/assets/stylesheets/partials/timesheet.css.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,22 @@
font-size: 12px;
}

#weekdays-list>li {
#weekdays-list > li {
text-align: center;

a { font-size: 16px; }
}

#users-list {

right: 0;
left: auto;

& > li a {
display: block;
border-style: none;
border-radius: 0px;
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the following style attributes, in order to improve the position of the dropdown menu:
#users-list {
right: 0;
left: auto;
}

}
25 changes: 18 additions & 7 deletions app/controllers/timesheet_controller.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
class TimesheetController < ApplicationController

# GET /timesheet(/:year/:month/:day)
# GET /timesheet/:user_id(/:year/:month/:day)
def show
if params[:year].present? && params[:month].present? && params[:day].present?
@date = Time.zone.parse("#{params[:year]}-#{params[:month]}-#{params[:day]}").to_date
@weekdays = (@[email protected]_end_of_week)
@week_summary = Timesheet.new(current_user).week_hours(@date)
@day_entries = Timesheet.new(current_user).day_entries(@date)
@users = User.all
args = params.clone

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try to avoid changing the params hash. Do something like "args = params.clone" and work with the "args" hash.
It'd also be nice to not fail silently when the user_id is invalid. For example, if I'm not an admin, but have tried to change the user_id in the URL, it may be better to have an error raised rather than seeing my timesheet (replacing the user_id with my own).

if args[:user_id].blank?
args[:user_id] = current_user.id
end

if current_user.is_admin || (current_user.id == args[:user_id].to_i)
if args[:year].present? && args[:month].present? && args[:day].present?
@timesheet = Timesheet.new(args)
else
date = Time.zone.now
redirect_to timesheet_url(user_id: args[:user_id], year: date.year, month: date.month, day: date.day)
end
else
date = Time.zone.now
redirect_to timesheet_url(year: date.year, month: date.month, day: date.day)
redirect_to timesheet_url(user_id: current_user.id, year: date.year, month: date.month, day: date.day),
alert: flash_message(:have_no_right, User)
end

end

end
25 changes: 14 additions & 11 deletions app/models/timesheet.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
class Timesheet

def initialize(user)
@user = user
end
attr_reader :user, :date, :day_entries, :weekdays, :week_summary

def week_hours(day = Time.zone.now)
day = day.to_date
week = (day.at_beginning_of_week..day.at_end_of_week)
def initialize(args)
@user = User.find_by_id(args[:user_id])
@date = parse_to_date(args[:day], args[:month], args[:year])
@day_entries = day_entries_for(@date)
@weekdays = (@[email protected]_end_of_week)
@week_summary = week_hours(@weekdays)
end

week.map { |weekday| { weekday.day => day_time(weekday) } }.reduce(:merge)
def day_entries_for(day)
TimeEntry.for_user(@user).for_day(day)
end

def day_time(day)
TimeEntry.for_user(@user).for_day(day).sum(:total_time)
def week_hours(weekdays)
weekdays.map { |weekday| { weekday.day => day_entries_for(weekday).sum(:total_time) } }.reduce(:merge)
end

def day_entries(day = Time.zone.now)
TimeEntry.for_user(@user).for_day(day)
def parse_to_date(day, month, year)
Time.zone.parse("#{year}-#{month}-#{day}").to_date
end

end
4 changes: 2 additions & 2 deletions app/views/layouts/_header.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

<% if user_signed_in? %>
<ul class="nav">
<li<%= ' class="active"'.html_safe if page_scope == :timesheet %>><%= link_to 'Track Time', timesheet_url %></li>
<li<%= ' class="active"'.html_safe if page_scope == :timesheet %>><%= link_to 'Track Time', timesheet_url(current_user.id) %></li>
<% if current_user.is_admin? %>
<li<%= ' class="active"'.html_safe if page_scope == :settings %>><%= link_to 'Settings', users_path %></li>
<% end %>
Expand All @@ -36,7 +36,7 @@
<div class="subnav subnav-fixed">
<ul class="nav nav-pills">
<% if page_scope == :timesheet %>
<li<%= ' class="active"'.html_safe if ['timesheet','time_entries'].include? controller_name %>><%= link_to 'Track', timesheet_path %></li>
<li<%= ' class="active"'.html_safe if ['timesheet','time_entries'].include? controller_name %>><%= link_to 'Track', timesheet_path(current_user.id) %></li>
<% if current_user.is_admin? %>
<li<%= ' class="active"'.html_safe if controller_name == 'reports' %>><%= link_to 'Reports', reports_path %></li>
<% end %>
Expand Down
45 changes: 31 additions & 14 deletions app/views/timesheet/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,32 +1,49 @@
<!-- Sets user id to be used in timesheet.js/datepicker() -->
<script type="text/javascript">
var timesheet_user_id = <%= @timesheet.user.id %>
</script>

<!-- Primary menu with the datetime, days control, calendar and user -->
<div class="container row">

<div class="pagination span5">
<h2 class="pull-left"><%= @date.strftime("%A, %d %b") %></h2>
<h2 class="pull-left">
<%= @timesheet.date.strftime("%A, %d %b") %>
<span class="label label-warning"><%= @timesheet.user.name %></span>
</h2>
</div>

<div class="span7 pull-right" id="timesheet-controls">

<div id="datepicker" class="hide"></div>

<ul class="pager pull-right">
<li><a href='#' class="icon-calendar"></a></li>
<li><a href="#" class="icon-calendar"></a></li>
<% if current_user.is_admin? %>
<li><a href='#' class="icon-user"></a></li>
<li class="dropdown">
<a href="#" data-toggle="dropdown" class="dropdown-toggle">
<span class="icon-user"></span>
</a>
<ul class="dropdown-menu" id="users-list">
<% @users.each do |user| %>
<li><%= link_to user.name, timesheet_url(user.id) %></li>
<% end %>
</ul>
</li>
<% end %>
</ul>

<div class="pagination pull-right">

<ul>
<li class="previous">
<%= link_to '', timesheet_url(today_date(@date.yesterday)), class: 'icon-arrow-left' %>
<%= link_to '', timesheet_url(today_date(@timesheet.date.yesterday)), class: 'icon-arrow-left' %>
</li>
<li<%= ' class="active"'.html_safe if @date.to_date == Time.zone.now.to_date %>>
<li<%= ' class="active"'.html_safe if @timesheet.date.to_date == Time.zone.now.to_date %>>
<%= link_to 'Today', timesheet_url %>
</li>
<li class="next">
<%= link_to '', timesheet_url(today_date(@date.tomorrow)), class: 'icon-arrow-right' %>
<%= link_to '', timesheet_url(today_date(@timesheet.date.tomorrow)), class: 'icon-arrow-right' %>
</li>
</ul>

Expand All @@ -48,25 +65,25 @@
<!-- Header of table with weekdays synchronized with primary menu above and the DB in table below -->
<div class="pagination pagination-large pull-right">
<ul id="weekdays-list">
<% @weekdays.each do |weekday| %>
<li <% if weekday.day == @date.day %> class="active" <% end %>>
<%= link_to timesheet_url(today_date(@date.advance({ days: (weekday - @date).days }))) do %>
<% @timesheet.weekdays.each do |weekday| %>
<li <% if weekday.day == @timesheet.date.day %> class="active" <% end %>>
<%= link_to timesheet_url(today_date(@timesheet.date.advance({ days: (weekday - @timesheet.date).days }))) do %>
<%= weekday.strftime('%A') %><br>
<span class="day-time">(<%= hours_and_minutes(@week_summary[weekday.day]) %>)</span>
<span class="day-time">(<%= hours_and_minutes(@timesheet.week_summary[weekday.day]) %>)</span>
<% end %>
</li>
<% end %>
<li>
<%= link_to '#' do %>
Total<br>
<span class="day-time">(<%= hours_and_minutes(@week_summary.values.sum) %>)</span>
<span class="day-time">(<%= hours_and_minutes(@timesheet.week_summary.values.sum) %>)</span>
<% end %>
</li>
</ul>
</div>
</div>

<% if @day_entries.empty? %>
<% if @timesheet.day_entries.empty? %>
<div class="text-center alert alert-info"><%= t :no_time_entry %></div>
<% else %>
<!-- Table with the time entries -->
Expand All @@ -81,7 +98,7 @@
</tr>
</thead>
<tbody>
<% @day_entries.each do |time_entry| %>
<% @timesheet.day_entries.each do |time_entry| %>
<tr>
<td class="row-fluid span6">
<ul class="breadcrumb row-fluid span10">
Expand Down Expand Up @@ -180,7 +197,7 @@
</td>
</tr>

<% end %><!-- end for @day_entries loop -->
<% end %><!-- end for @timesheet.day_entries loop -->

</tbody>

Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

patch 'time_entries/:id/finish' => 'time_entries#finish', as: :time_entry_finish

get 'timesheet(/:year/:month/:day)' => 'timesheet#show', as: :timesheet
get 'timesheet/:user_id(/:year/:month/:day)' => 'timesheet#show', as: :timesheet

get 'reports' => 'reports#new', as: :reports
post 'reports' => 'reports#view'
Expand Down