Merged r14944 (#21413).

git-svn-id: http://svn.redmine.org/redmine/branches/3.2-stable@14945 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang
2015-12-05 10:12:54 +00:00
parent 421793c8a2
commit f2fd790555
3 changed files with 29 additions and 16 deletions

View File

@@ -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)