Merge pull request #164 from NobodysNightmare/feature/indicate_summable_columns

Indicate summable plugin columns
pull/6827/head
ulferts 9 years ago
commit 1bde09bbac
  1. 19
      app/models/cost_entry.rb
  2. 43
      lib/open_project/costs/patches/query_patch.rb
  3. 19
      lib/open_project/costs/patches/time_entry_patch.rb
  4. 12
      lib/open_project/costs/patches/work_package_patch.rb

@ -58,6 +58,25 @@ class CostEntry < ActiveRecord::Base
.includes([:project, :user]) .includes([:project, :user])
} }
def self.costs_of(work_packages:)
# N.B. Because of an AR quirks the code below uses statements like
# where(work_package_id: ids)
# You would expect to be able to simply write those as
# where(work_package: work_packages)
# However, AR (Rails 4.2) will not expand :includes + :references inside a subquery,
# which will render the query invalid. Therefore we manually extract the IDs in a separate (pluck) query.
ids = if work_packages.respond_to?(:pluck)
work_packages.pluck(:id)
else
Array(work_packages).map { |wp| wp.id }
end
CostEntry.where(work_package_id: ids)
.joins(work_package: :project)
.visible_costs
.sum("COALESCE(#{CostEntry.table_name}.overridden_costs,
#{CostEntry.table_name}.costs)").to_f
end
def after_initialize def after_initialize
if new_record? && cost_type.nil? if new_record? && cost_type.nil?
if default_cost_type = CostType.default if default_cost_type = CostType.default

@ -22,6 +22,13 @@ module OpenProject::Costs::Patches::QueryPatch
include ActionView::Helpers::NumberHelper include ActionView::Helpers::NumberHelper
alias :super_value :value alias :super_value :value
def initialize(name, options = {})
super
@sum_function = options[:summable]
self.summable = @sum_function.respond_to?(:call)
end
def value(work_package) def value(work_package)
number_to_currency(work_package.send(name)) number_to_currency(work_package.send(name))
end end
@ -37,6 +44,10 @@ module OpenProject::Costs::Patches::QueryPatch
def xls_value(work_package) def xls_value(work_package)
super_value work_package super_value work_package
end end
def sum_of(work_packages)
@sum_function.call(work_packages)
end
end end
def self.included(base) # :nodoc: def self.included(base) # :nodoc:
@ -47,9 +58,26 @@ module OpenProject::Costs::Patches::QueryPatch
# Same as typing in the class # Same as typing in the class
base.class_eval do base.class_eval do
add_available_column(QueryColumn.new(:cost_object_subject)) add_available_column(QueryColumn.new(:cost_object_subject))
add_available_column(CurrencyQueryColumn.new(:material_costs))
add_available_column(CurrencyQueryColumn.new(:labor_costs)) add_available_column(CurrencyQueryColumn.new(
add_available_column(CurrencyQueryColumn.new(:overall_costs)) :material_costs,
summable: -> (work_packages) {
CostEntry.costs_of(work_packages: work_packages)
}))
add_available_column(CurrencyQueryColumn.new(
:labor_costs,
summable: -> (work_packages) {
TimeEntry.costs_of(work_packages: work_packages)
}))
add_available_column(CurrencyQueryColumn.new(
:overall_costs,
summable: -> (work_packages) {
labor_costs = TimeEntry.costs_of(work_packages: work_packages)
material_costs = CostEntry.costs_of(work_packages: work_packages)
labor_costs + material_costs
}))
Queries::WorkPackages::Filter.add_filter_type_by_field('cost_object_id', 'list_optional') Queries::WorkPackages::Filter.add_filter_type_by_field('cost_object_id', 'list_optional')
@ -58,15 +86,6 @@ module OpenProject::Costs::Patches::QueryPatch
end end
module ClassMethods module ClassMethods
# Setter for +available_columns+ that isn't provided by the core.
def available_columns=(v)
self.available_columns = (v)
end
# Method to add a column to the +available_columns+ that isn't provided by the core.
def add_available_column(column)
available_columns << (column)
end
end end
module InstanceMethods module InstanceMethods

@ -53,6 +53,25 @@ module OpenProject::Costs::Patches::TimeEntryPatch
includes(:project, :user) includes(:project, :user)
.where([view_time_entries, view_hourly_rates].join(' AND ')) .where([view_time_entries, view_hourly_rates].join(' AND '))
} }
def self.costs_of(work_packages:)
# N.B. Because of an AR quirks the code below uses statements like
# where(work_package_id: ids)
# You would expect to be able to simply write those as
# where(work_package: work_packages)
# However, AR (Rails 4.2) will not expand :includes + :references inside a subquery,
# which will render the query invalid. Therefore we manually extract the IDs in a separate (pluck) query.
ids = if work_packages.respond_to?(:pluck)
work_packages.pluck(:id)
else
Array(work_packages).map { |wp| wp.id }
end
TimeEntry.where(work_package_id: ids)
.joins(work_package: :project)
.visible_costs
.sum("COALESCE(#{TimeEntry.table_name}.overridden_costs,
#{TimeEntry.table_name}.costs)").to_f
end
end end
end end

@ -107,19 +107,11 @@ module OpenProject::Costs::Patches::WorkPackagePatch
end end
def material_costs def material_costs
@material_costs ||= cost_entries.visible_costs(User.current, project).sum("CASE CostEntry.costs_of(work_packages: self)
WHEN #{CostEntry.table_name}.overridden_costs IS NULL THEN
#{CostEntry.table_name}.costs
ELSE
#{CostEntry.table_name}.overridden_costs END").to_f
end end
def labor_costs def labor_costs
@labor_costs ||= time_entries.visible_costs(User.current, project).sum("CASE TimeEntry.costs_of(work_packages: self)
WHEN #{TimeEntry.table_name}.overridden_costs IS NULL THEN
#{TimeEntry.table_name}.costs
ELSE
#{TimeEntry.table_name}.overridden_costs END").to_f
end end
def overall_costs def overall_costs

Loading…
Cancel
Save