##// END OF EJS Templates
Fixed that non-empty blank strings as custom field values are not properly validated (#16169)....
Fixed that non-empty blank strings as custom field values are not properly validated (#16169). git-svn-id: http://svn.redmine.org/redmine/trunk@12938 e93f8b46-1217-0410-a6f0-8f06a7374b81

File last commit:

r12663:4c7a76785d9f
r12663:4c7a76785d9f
Show More
custom_field.rb
281 lines | 8.4 KiB | text/x-ruby | RubyLexer
Jean-Philippe Lang
Adds User and Version custom field format that can be used to reference a project member or version in custom fields (#2096)....
r5152 # Redmine - project management software
Toshi MARUYAMA
update copyright year (#15977)...
r12461 # Copyright (C) 2006-2014 Jean-Philippe Lang
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 #
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393 #
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 # This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393 #
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 # You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class CustomField < ActiveRecord::Base
Jean-Philippe Lang
Extracted new_subclass_instance method to a module....
r8063 include Redmine::SubclassFactory
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 has_many :custom_values, :dependent => :delete_all
Jean-Philippe Lang
Role-based issue custom field visibility (#5037)....
r11782 has_and_belongs_to_many :roles, :join_table => "#{table_name_prefix}custom_fields_roles#{table_name_suffix}", :foreign_key => "custom_field_id"
Jean-Philippe Lang
Custom fields can now be reordered....
r888 acts_as_list :scope => 'type = \'#{self.class}\''
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 serialize :possible_values
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 store :format_store
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 validates_presence_of :name, :field_format
Jean-Philippe Lang
Allow same name for custom fields on different object types....
r1729 validates_uniqueness_of :name, :scope => :type
Jean-Philippe Lang
Added several validates_length_of...
r590 validates_length_of :name, :maximum => 30
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 validates_inclusion_of :field_format, :in => Proc.new { Redmine::FieldFormat.available_formats }
Jean-Philippe Lang
Extracts custom field values validation from CustomValue so that they can be validated globally from the customized object (#1189)....
r8597 validate :validate_custom_field
Jean-Philippe Lang
Role-based issue custom field visibility (#5037)....
r11782
Toshi MARUYAMA
Rails3: model: replace deprecated before_validation method at CustomField model...
r8071 before_validation :set_searchable
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 before_save do |field|
field.format.before_custom_field_save(field)
end
Jean-Philippe Lang
Ability to uncheck "Multiple values" for existing custom fields (#12251)....
r10937 after_save :handle_multiplicity_change
Jean-Philippe Lang
Role-based issue custom field visibility (#5037)....
r11782 after_save do |field|
if field.visible_changed? && field.visible
field.roles.clear
end
end
Toshi MARUYAMA
Rails3: replace deprecated 'validate' method at CustomField model....
r6792
Jean-Baptiste Barth
Use lambda form in model scopes (#12499)...
r10722 scope :sorted, lambda { order("#{table_name}.position ASC") }
Jean-Philippe Lang
Role-based issue custom field visibility (#5037)....
r11782 scope :visible, lambda {|*args|
user = args.shift || User.current
if user.admin?
# nop
elsif user.memberships.any?
where("#{table_name}.visible = ? OR #{table_name}.id IN (SELECT DISTINCT cfr.custom_field_id FROM #{Member.table_name} m" +
" INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
" INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
" WHERE m.user_id = ?)",
true, user.id)
else
where(:visible => true)
end
}
Jean-Philippe Lang
Replaces find(:all) calls....
r10689
Jean-Philippe Lang
Fixed that displaying time entries with custom field column raises an error (#5037)....
r11811 def visible_by?(project, user=User.current)
visible? || user.admin?
end
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 def format
@format ||= Redmine::FieldFormat.find(field_format)
end
Jean-Philippe Lang
Make sure that custom field format cannot be changed....
r10413 def field_format=(arg)
# cannot change format of a saved custom field
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 if new_record?
@format = nil
super
end
Jean-Philippe Lang
Make sure that custom field format cannot be changed....
r10413 end
Toshi MARUYAMA
Rails3: model: replace deprecated before_validation method at CustomField model...
r8071 def set_searchable
Jean-Philippe Lang
Search engine: issue custom fields can now be searched....
r981 # make sure these fields are not searchable
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 self.searchable = false unless format.class.searchable_supported
Jean-Philippe Lang
Adds support for multiselect custom fields (#1189)....
r8601 # make sure only these fields can have multiple values
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 self.multiple = false unless format.class.multiple_supported
Jean-Philippe Lang
Fixed: can not save numeric, date and boolean custom fields (broken by r994)....
r983 true
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
Extracts custom field values validation from CustomValue so that they can be validated globally from the customized object (#1189)....
r8597 def validate_custom_field
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 format.validate_custom_field(self).each do |attribute, message|
errors.add attribute, message
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
Validate custom field regexp (#8865)....
r6178 if regexp.present?
begin
Regexp.new(regexp)
rescue
errors.add(:regexp, :invalid)
end
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 if default_value.present?
validate_field_value(default_value).each do |message|
errors.add :default_value, message
end
Jean-Philippe Lang
Extracts custom field values validation from CustomValue so that they can be validated globally from the customized object (#1189)....
r8597 end
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 def possible_custom_value_options(custom_value)
format.possible_custom_value_options(custom_value)
end
def possible_values_options(object=nil)
if object.is_a?(Array)
object.map {|o| format.possible_values_options(self, o)}.reduce(:&) || []
Jean-Philippe Lang
Adds User and Version custom field format that can be used to reference a project member or version in custom fields (#2096)....
r5152 else
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 format.possible_values_options(self, object) || []
Jean-Philippe Lang
Adds User and Version custom field format that can be used to reference a project member or version in custom fields (#2096)....
r5152 end
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 def possible_values
values = super()
if values.is_a?(Array)
values.each do |value|
value.force_encoding('UTF-8') if value.respond_to?(:force_encoding)
Jean-Philippe Lang
Merged rails-3.2 branch....
r9346 end
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 values
else
[]
Jean-Philippe Lang
Adds User and Version custom field format that can be used to reference a project member or version in custom fields (#2096)....
r5152 end
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
Use a textarea for custom fields possible values (#2472)....
r2265 # Makes possible_values accept a multiline string
def possible_values=(arg)
if arg.is_a?(Array)
Jean-Philippe Lang
Merged rails-3.2 branch....
r9346 super(arg.compact.collect(&:strip).select {|v| !v.blank?})
Jean-Philippe Lang
Use a textarea for custom fields possible values (#2472)....
r2265 else
self.possible_values = arg.to_s.split(/[\n\r]+/)
end
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
Fixed: "None" category issue count is empty while grouping by category (#4308)....
r2998 def cast_value(value)
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 format.cast_value(self, value)
Jean-Philippe Lang
Fixed: "None" category issue count is empty while grouping by category (#4308)....
r2998 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
Fixed: Custom fields of type version not proper handled in receiving e-mails (#11571)....
r9974 def value_from_keyword(keyword, customized)
possible_values_options = possible_values_options(customized)
if possible_values_options.present?
keyword = keyword.to_s.downcase
Jean-Philippe Lang
Fixed that validation fails when receiving an email with list custom fields (#12400)....
r10763 if v = possible_values_options.detect {|text, id| text.downcase == keyword}
if v.is_a?(Array)
v.last
else
v
end
end
Jean-Philippe Lang
Fixed: Custom fields of type version not proper handled in receiving e-mails (#11571)....
r9974 else
keyword
end
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb...
r11124
Jean-Philippe Lang
Ability to sort the issue list by text, list, date and boolean custom fields (#1139)....
r2255 # Returns a ORDER BY clause that can used to sort customized
# objects by their value of the custom field.
Jean-Philippe Lang
Adds CustomField#group_statement....
r9888 # Returns nil if the custom field can not be used for sorting.
Jean-Philippe Lang
Ability to sort the issue list by text, list, date and boolean custom fields (#1139)....
r2255 def order_statement
Jean-Philippe Lang
Adds support for multiselect custom fields (#1189)....
r8601 return nil if multiple?
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 format.order_statement(self)
Jean-Philippe Lang
Ability to sort the issue list by text, list, date and boolean custom fields (#1139)....
r2255 end
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330
Jean-Philippe Lang
Adds CustomField#group_statement....
r9888 # Returns a GROUP BY clause that can used to group by custom value
# Returns nil if the custom field can not be used for grouping.
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb...
r11124 def group_statement
Jean-Philippe Lang
Adds CustomField#group_statement....
r9888 return nil if multiple?
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 format.group_statement(self)
Jean-Philippe Lang
Ability to group and sort the issue list by user/version custom field (#9419)....
r9890 end
def join_for_order_statement
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 format.join_for_order_statement(self)
Jean-Philippe Lang
Ability to group and sort the issue list by user/version custom field (#9419)....
r9890 end
Jean-Philippe Lang
Reduces the number of subqueries when searching with many custom fields set as searchable (#15781)....
r12206 def visibility_by_project_condition(project_key=nil, user=User.current, id_column=nil)
Jean-Philippe Lang
Role-based issue custom field visibility (#5037)....
r11782 if visible? || user.admin?
"1=1"
elsif user.anonymous?
"1=0"
else
project_key ||= "#{self.class.customized_class.table_name}.project_id"
Jean-Philippe Lang
Reduces the number of subqueries when searching with many custom fields set as searchable (#15781)....
r12206 id_column ||= id
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb...
r11958 "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" +
Jean-Philippe Lang
Role-based issue custom field visibility (#5037)....
r11782 " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
" INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
Jean-Philippe Lang
Reduces the number of subqueries when searching with many custom fields set as searchable (#15781)....
r12206 " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id_column})"
Jean-Philippe Lang
Role-based issue custom field visibility (#5037)....
r11782 end
end
def self.visibility_condition
if user.admin?
"1=1"
elsif user.anonymous?
"#{table_name}.visible"
else
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb...
r11958 "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" +
Jean-Philippe Lang
Role-based issue custom field visibility (#5037)....
r11782 " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
" INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
" WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id})"
end
end
Jean-Philippe Lang
Custom fields can now be reordered....
r888 def <=>(field)
position <=> field.position
end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
Ability to group and sort the issue list by user/version custom field (#9419)....
r9890 # Returns the class that values represent
def value_class
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 format.target_class if format.respond_to?(:target_class)
Jean-Philippe Lang
Ability to group and sort the issue list by user/version custom field (#9419)....
r9890 end
Jean-Philippe Lang
Ability to sort the issue list by text, list, date and boolean custom fields (#1139)....
r2255 def self.customized_class
self.name =~ /^(.+)CustomField$/
Jean-Philippe Lang
REST API: custom fields definition (#11159)....
r11935 $1.constantize rescue nil
Jean-Philippe Lang
Ability to sort the issue list by text, list, date and boolean custom fields (#1139)....
r2255 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 # to move in project_custom_field
def self.for_all
Jean-Philippe Lang
Replaces find(:all) calls....
r10690 where(:is_for_all => true).order('position').all
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 end
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb....
r6393
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 def type_name
nil
end
Jean-Philippe Lang
Extracts custom field values validation from CustomValue so that they can be validated globally from the customized object (#1189)....
r8597
Jean-Philippe Lang
Adds support for multiselect custom fields (#1189)....
r8601 # Returns the error messages for the given value
Jean-Philippe Lang
Extracts custom field values validation from CustomValue so that they can be validated globally from the customized object (#1189)....
r8597 # or an empty array if value is a valid value for the custom field
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 def validate_custom_value(custom_value)
value = custom_value.value
Jean-Philippe Lang
Extracts custom field values validation from CustomValue so that they can be validated globally from the customized object (#1189)....
r8597 errs = []
Jean-Philippe Lang
Adds support for multiselect custom fields (#1189)....
r8601 if value.is_a?(Array)
if !multiple?
errs << ::I18n.t('activerecord.errors.messages.invalid')
end
if is_required? && value.detect(&:present?).nil?
errs << ::I18n.t('activerecord.errors.messages.blank')
end
else
if is_required? && value.blank?
errs << ::I18n.t('activerecord.errors.messages.blank')
end
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 end
Jean-Philippe Lang
Fixed that non-empty blank strings as custom field values are not properly validated (#16169)....
r12663 errs += format.validate_custom_value(custom_value)
Jean-Philippe Lang
Extracts custom field values validation from CustomValue so that they can be validated globally from the customized object (#1189)....
r8597 errs
end
Jean-Philippe Lang
Merged custom fields format refactoring....
r12125 # Returns the error messages for the default custom field value
def validate_field_value(value)
validate_custom_value(CustomValue.new(:custom_field => self, :value => value))
end
Jean-Philippe Lang
Extracts custom field values validation from CustomValue so that they can be validated globally from the customized object (#1189)....
r8597 # Returns true if value is a valid value for the custom field
def valid_field_value?(value)
validate_field_value(value).empty?
end
Jean-Philippe Lang
Update the new custom field form with remotely....
r9980 def format_in?(*args)
args.include?(field_format)
end
Jean-Philippe Lang
Extracts custom field values validation from CustomValue so that they can be validated globally from the customized object (#1189)....
r8597 protected
Jean-Philippe Lang
Ability to uncheck "Multiple values" for existing custom fields (#12251)....
r10937 # Removes multiple values for the custom field after setting the multiple attribute to false
# We kepp the value with the highest id for each customized object
def handle_multiplicity_change
if !new_record? && multiple_was && !multiple
ids = custom_values.
Toshi MARUYAMA
remove trailing white-spaces from app/models/custom_field.rb...
r11124 where("EXISTS(SELECT 1 FROM #{CustomValue.table_name} cve WHERE cve.custom_field_id = #{CustomValue.table_name}.custom_field_id" +
" AND cve.customized_type = #{CustomValue.table_name}.customized_type AND cve.customized_id = #{CustomValue.table_name}.customized_id" +
Jean-Philippe Lang
Ability to uncheck "Multiple values" for existing custom fields (#12251)....
r10937 " AND cve.id > #{CustomValue.table_name}.id)").
pluck(:id)
if ids.any?
custom_values.where(:id => ids).delete_all
end
end
end
Jean-Philippe Lang
added svn:eol-style native property on /app files...
r330 end
Jean-Philippe Lang
Prevents no method errors when reloading in development mode....
r12396
require_dependency 'redmine/field_format'