summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2015-12-05 10:12:54 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2015-12-05 10:12:54 +0000
commitf2fd7905557d9fe2442eb577fc7e0c207356e648 (patch)
treec3a2cf9c5160cdba5c9bc353e275718d2ecd738a /lib
parent421793c8a27cf6da5b5f147c17c41d2843f66d5a (diff)
downloadredmine-f2fd7905557d9fe2442eb577fc7e0c207356e648.tar.gz
redmine-f2fd7905557d9fe2442eb577fc7e0c207356e648.zip
Merged r14944 (#21413).
git-svn-id: http://svn.redmine.org/redmine/branches/3.2-stable@14945 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib')
-rw-r--r--lib/redmine/field_format.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/redmine/field_format.rb b/lib/redmine/field_format.rb
index 841d6a08d..10e5b48a0 100644
--- a/lib/redmine/field_format.rb
+++ b/lib/redmine/field_format.rb
@@ -61,6 +61,10 @@ module Redmine
class_attribute :searchable_supported
self.searchable_supported = false
+ # Set this to true if field values can be summed up
+ class_attribute :totalable_supported
+ self.totalable_supported = false
+
# Restricts the classes that the custom field can be added to
# Set to nil for no restrictions
class_attribute :customized_class_names
@@ -370,6 +374,7 @@ module Redmine
class Numeric < Unbounded
self.form_partial = 'custom_fields/formats/numeric'
+ self.totalable_supported = true
def order_statement(custom_field)
# Make the database cast values into numeric
@@ -377,6 +382,18 @@ module Redmine
# CustomValue validations should ensure that it doesn't occur
"CAST(CASE #{join_alias custom_field}.value WHEN '' THEN '0' ELSE #{join_alias custom_field}.value END AS decimal(30,3))"
end
+
+ # Returns totals for the given scope
+ def total_for_scope(custom_field, scope)
+ scope.joins(:custom_values).
+ where(:custom_values => {:custom_field_id => custom_field.id}).
+ where.not(:custom_values => {:value => ''}).
+ sum("CAST(#{CustomValue.table_name}.value AS decimal(30,3))")
+ end
+
+ def cast_total_value(custom_field, value)
+ cast_single_value(custom_field, value)
+ end
end
class IntFormat < Numeric
@@ -412,6 +429,10 @@ module Redmine
value.to_f
end
+ def cast_total_value(custom_field, value)
+ value.to_f.round(2)
+ end
+
def validate_single_value(custom_field, value, customized=nil)
errs = super
errs << ::I18n.t('activerecord.errors.messages.invalid') unless (Kernel.Float(value) rescue nil)