use joins instead of subselects for eager loaded costs

pull/6827/head
Jens Ulferts 8 years ago
parent 763e62c5d9
commit 2758e52a85
No known key found for this signature in database
GPG Key ID: 3CAA4B1182CF5308
  1. 30
      app/models/work_package/abstract_costs.rb
  2. 8
      app/models/work_package/labor_costs.rb
  3. 4
      app/models/work_package/material_costs.rb

@ -46,6 +46,10 @@ class WorkPackage
raise NotImplementedError, "subclass responsiblity"
end
def costs_sum_alias
raise NotImplementedError, "subclass responsiblity"
end
private
def work_package_ids(work_packages)
@ -65,22 +69,9 @@ class WorkPackage
end
def add_costs_to(scope)
query = costs.to_sql
scope
.joins(
"LEFT JOIN (#{query}) as #{table_alias} ON #{table_alias}.wp_id = #{wp_table.name}.id")
.select("#{table_alias}.#{costs_table_name}_sum")
end
def costs
filter_authorized all_costs
end
def all_costs
WorkPackage
.joins(sum_arel.join_sources)
.select("work_packages.id as wp_id, #{costs_sum} as #{costs_table_name}_sum")
.select("#{costs_sum} AS #{costs_sum_alias}")
.group(wp_table[:id])
end
@ -103,9 +94,7 @@ class WorkPackage
def sum_arel
wp_table
.from(wp_table)
.join(ce_table, Arel::Nodes::OuterJoin).on(ce_table[:work_package_id].eq(wp_table[:id]))
.join(projects_table).on(projects_table[:id].eq(wp_table[:project_id]))
.outer_join(ce_table).on(ce_table_join_condition)
.group(wp_table[:id])
end
@ -117,6 +106,13 @@ class WorkPackage
costs_model.arel_table
end
def ce_table_join_condition
authorization_scope = filter_authorized costs_model.all
authorization_where = authorization_scope.ast.cores.last.wheres.last
ce_table[:work_package_id].eq(wp_table[:id]).and(authorization_where)
end
def projects_table
Project.arel_table
end

@ -7,5 +7,13 @@ class WorkPackage
def filter_authorized(scope)
TimeEntry.with_visible_costs_on scope
end
def ce_table
super.alias 'time_entry_labor'
end
def costs_sum_alias
'time_entries_sum'
end
end
end

@ -7,5 +7,9 @@ class WorkPackage
def filter_authorized(scope)
CostEntry.with_visible_costs_on scope
end
def costs_sum_alias
'cost_entries_sum'
end
end
end

Loading…
Cancel
Save