Skip to content

Commit 66c8a58

Browse files
authored
Merge pull request DefactoSoftware#395 from DefactoSoftware/hugo/fix-billables-memory-issue
[ready] Fix extreme memory usages on /billables for accounts with 7000+ billable entries
2 parents 5ec044a + 53d2c10 commit 66c8a58

14 files changed

+113
-301
lines changed

app/assets/stylesheets/_billables.scss

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@
2626

2727
max-width: 90em;
2828

29-
.submit-billable-entries {
30-
@include media($laptop-screen) {
31-
opacity: 0;
32-
}
29+
#submit-billable-entries-test {
30+
opacity: .0;
3331
}
3432
}
3533
}
+7-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
class BillablesController < ApplicationController
22
def index
3-
@hours_entries = Hour.query(entry_filter_or_default,
4-
[:user, :project, :category])
5-
@mileages_entries = Mileage.query(entry_filter_or_default,
6-
[:user, :project])
7-
8-
@billable_list = BillableList.new(@hours_entries, @mileages_entries)
9-
@filters = EntryFilter.new(entry_filter_or_default)
3+
@projects = projects_with_billable_entries
104
end
115

126
def bill_entries
@@ -22,7 +16,11 @@ def bill_entries
2216

2317
private
2418

25-
def entry_filter_or_default
26-
params[:entry_filter] || { billed: false }
19+
def projects_with_billable_entries
20+
billable_projects = Project.where(billable: true, archived: false)
21+
22+
billable_projects.select do |project|
23+
project if project.has_billable_entries?
24+
end
2725
end
2826
end

app/helpers/application_helper.rb

+6-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def current_locale
4242

4343
def billable_entry_checkbox(entry, entry_type)
4444
if entry.billed
45-
""
45+
""
4646
else
4747
tag(:input,
4848
type: "checkbox",
@@ -77,4 +77,9 @@ def easter?
7777
holiday = Holidays.on(Date.today, :nl)
7878
holiday[0][:name] == "Pasen" if holiday.any?
7979
end
80+
81+
def billable_hours_of(project)
82+
Hour.includes(:category, :user, :project).
83+
where(project: project, billed: false)
84+
end
8085
end

app/models/billable_list.rb

-16
This file was deleted.

app/models/project.rb

+5
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def budget_status
5050
budget - hours.sum(:value) if budget
5151
end
5252

53+
def has_billable_entries?
54+
hours.exists?(billed: false) ||
55+
mileages.exists?(billed: false)
56+
end
57+
5358
private
5459

5560
def slug_source

app/views/billables/_entries.html.haml

-23
This file was deleted.

app/views/billables/_filters.html.haml

-8
This file was deleted.

app/views/billables/_hours_entries.html.haml

+7-9
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
%th= t("billables.table.category")
99
%th= t("billables.table.hours")
1010
%th= t("billables.table.user")
11-
- entries.each do |entry|
11+
- billable_hours_of(project).each do |hour|
1212
%tr.info-row
13-
%td=billable_entry_checkbox(entry, "hours")
14-
%td=I18n.l (entry.date)
15-
%td=entry.category.name
16-
%td.center= entry.value
13+
%td= billable_entry_checkbox(hour, "hours")
14+
%td= I18n.l (hour.date)
15+
%td= hour.category.name
16+
%td.center= hour.value
1717
%td
18-
= link_to entry.user do
19-
= entry.user.full_name
20-
21-
18+
= link_to hour.user do
19+
= hour.user.full_name

app/views/billables/_mileages_entries.html.haml

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
%th
99
%th= t("entries.index.mileages")
1010
%th= t("billables.table.user")
11-
- entries.each do |entry|
11+
- project.mileages.where(billed: false).each do |mileage|
1212
%tr.info-row
13-
%td=billable_entry_checkbox(entry, "mileages")
14-
%td=I18n.l (entry.date)
13+
%td= billable_entry_checkbox(mileage, "mileages")
14+
%td= I18n.l (mileage.date)
1515
%td
16-
%td.center= entry.value
16+
%td.center= mileage.value
1717
%td
18-
= link_to entry.user do
19-
= entry.user.full_name
18+
= link_to mileage.user do
19+
= mileage.user.full_name

app/views/billables/_projects.html.haml

-10
This file was deleted.

app/views/billables/index.html.haml

+8-11
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@
44
%button{id: "submit-billable-entries", disabled: true}
55
=t("billables.buttons.bill_selected")
66
.billables
7-
.sidebar
8-
.container
9-
= render partial: "filters", locals: { filter_url: billables_path }
107
.outer
118
%h1= t("billables.billable_entries")
12-
- unless @hours_entries.any? || @mileages_entries.any?
13-
.info
14-
%p= t("info.no_billable_entries_html")
159
= form_tag("/billables", method: "post", remote: true, id: "billable-entries-form", autocomplete: "off") do
16-
- @billable_list.clients.each do |client|
17-
%h1= client.name
18-
= render "projects", projects: client.projects
19-
20-
10+
- @projects.each do |project|
11+
.container
12+
%h1= project.name
13+
- if project.hours.exists?(billed: false)
14+
= render "hours_entries", project: project
15+
- if project.mileages.exists?(billed: false)
16+
= render "mileages_entries", project: project
17+
%button#submit-billable-entries-test= t("billables.buttons.bill_selected")

spec/features/user_manages_billables_spec.rb

-185
This file was deleted.

0 commit comments

Comments
 (0)