Skip to content

Commit 3872b6e

Browse files
committed
Added rake task 'stat:do_monthly' to instantiate and save MonthlyStat for previous month. Added link to MonthlyStat index on navbar for supervisor users.
1 parent 530baea commit 3872b6e

File tree

4 files changed

+82
-44
lines changed

4 files changed

+82
-44
lines changed

app/assets/stylesheets/monthly_stats.scss

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
.stat_box {
2-
// background-color: #F4F7CB;
32
width: 100%;
43
}
54

app/views/layouts/_header.html.erb

+15-9
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@
1515
<div class="collapse navbar-collapse" id="navbar-collapse-area">
1616
<ul class="nav navbar-nav navbar-left" id="navbar-buttons">
1717
<% if logged_in? %>
18-
<li>
19-
<%= link_to "Unassigned Clients", user_cp_path(current_user) %>
20-
</li>
18+
<% if policy(User).index? %>
19+
<li>
20+
<%= link_to "Monthly Statistics", monthly_stats_path %>
21+
</li>
22+
<% else %>
23+
<li>
24+
<%= link_to "Unassigned Clients", user_cp_path(current_user) %>
25+
</li>
2126

22-
<li>
23-
<%= link_to "My Clients", my_clients_path(current_user) %>
24-
</li>
27+
<li>
28+
<%= link_to "My Clients", my_clients_path(current_user) %>
29+
</li>
2530

26-
<li>
27-
<%= link_to "Calendar", my_calendar_path(current_user) %>
28-
</li>
31+
<li>
32+
<%= link_to "Calendar", my_calendar_path(current_user) %>
33+
</li>
34+
<% end %>
2935
<% end %>
3036
</ul>
3137
<ul class="nav navbar-nav navbar-right">
+47-34
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
11
<% provide(:title, "Monthly Statistics") %>
22

3-
<% (2016..Time.zone.now.year).to_a.reverse.each do |year_num| %>
4-
<div class="stat_table_container">
5-
<table class="table table-striped table-responsive stat_box">
6-
<tbody>
7-
<div class="bg-info small_box_header">
8-
<h2><strong><%= year_num %></strong></h2>
9-
</div>
10-
<tr class="info table-row center">
11-
<td><strong>Month</strong></td>
12-
<td><strong># of WRAP sessions</strong></td>
13-
<td>
14-
<strong>
15-
# of unduplicated members who attended a WRAP session
16-
</strong>
17-
</td>
18-
<td><strong># of WRAP plans opened</strong></td>
19-
<td><strong># of WRAP plans completed</strong></td>
20-
<td>
21-
<strong>
22-
# of unduplicated members who attended a non-WRAP session
23-
</strong>
24-
</td>
25-
<td><strong># of non-WRAP sessions</strong></td>
26-
</tr>
27-
<% (monthly_stats.minimum(:month)..monthly_stats.maximum(:month))
28-
.each do |month_num| %>
29-
<% current_monthly_stat = monthly_stats.where(
30-
"month = ? AND year = ?", month_num, year_num).first %>
31-
<%= render partial: "monthly_stat_row",
32-
locals: {current_monthly_stat: current_monthly_stat} %>
33-
<% end %>
34-
</tbody>
35-
</table>
36-
</div>
3+
<% if monthly_stats.count == 0 %>
4+
<h5>
5+
Sorry, there are no statistics available yet. Statistics for each month will
6+
be automatically posted here on the first day of the following month.
7+
</h5>
8+
<% else %>
9+
<h5>
10+
Statistics for each month will be automatically posted here on the first day
11+
of the following month.
12+
</h5>
13+
<% (2016..Time.zone.now.year).to_a.reverse.each do |year_num| %>
14+
<div class="stat_table_container">
15+
<div class="bg-info small_box_header">
16+
<h2><strong><%= year_num %></strong></h2>
17+
</div>
18+
<table class="table table-striped table-responsive stat_box">
19+
<thead>
20+
<tr class="info table-row center">
21+
<td><strong>Month</strong></td>
22+
<td><strong># of WRAP sessions</strong></td>
23+
<td>
24+
<strong>
25+
# of unduplicated members who attended a WRAP session
26+
</strong>
27+
</td>
28+
<td><strong># of WRAP plans opened</strong></td>
29+
<td><strong># of WRAP plans completed</strong></td>
30+
<td>
31+
<strong>
32+
# of unduplicated members who attended a non-WRAP session
33+
</strong>
34+
</td>
35+
<td><strong># of non-WRAP sessions</strong></td>
36+
</tr>
37+
</thead>
38+
<tbody>
39+
<% (monthly_stats.minimum(:month)..monthly_stats.maximum(:month))
40+
.each do |month_num| %>
41+
<% current_monthly_stat = monthly_stats.where(
42+
"month = ? AND year = ?", month_num, year_num).first %>
43+
<%= render partial: "monthly_stat_row",
44+
locals: {current_monthly_stat: current_monthly_stat} %>
45+
<% end %>
46+
</tbody>
47+
</table>
48+
</div>
49+
<% end %>
3750
<% end %>

lib/tasks/stat.rake

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace :stat do
2+
desc "Instantiate MonthlyStat for previous month and save to database"
3+
task do_monthly: :environment do
4+
one_month_ago = Time.zone.now - 1.month
5+
prev_month_stat_count =
6+
MonthlyStat.where("month = ? AND year = ?",
7+
one_month_ago.month, one_month_ago.year).count
8+
if prev_month_stat_count == 0
9+
new_stat = MonthlyStat.new
10+
new_stat.set_monthly_values(one_month_ago.year, one_month_ago.month)
11+
new_stat.save
12+
puts "MonthlyStat for #{one_month_ago.month}/#{one_month_ago.year}" +
13+
" saved to database."
14+
else
15+
puts "MonthlyStat for #{one_month_ago.month}/#{one_month_ago.year}" +
16+
" already exists in the database. No action taken."
17+
end
18+
end
19+
20+
end

0 commit comments

Comments
 (0)