To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / app / models / custom_field.rb @ 1534:b31caaed9d4d
History | View | Annotate | Download (8.5 KB)
| 1 | 441:cbce1fd3b1b7 | Chris | # Redmine - project management software
|
|---|---|---|---|
| 2 | 1494:e248c7af89ec | Chris | # Copyright (C) 2006-2014 Jean-Philippe Lang
|
| 3 | 0:513646585e45 | Chris | #
|
| 4 | # This program is free software; you can redistribute it and/or
|
||
| 5 | # modify it under the terms of the GNU General Public License
|
||
| 6 | # as published by the Free Software Foundation; either version 2
|
||
| 7 | # of the License, or (at your option) any later version.
|
||
| 8 | 909:cbb26bc654de | Chris | #
|
| 9 | 0:513646585e45 | Chris | # This program is distributed in the hope that it will be useful,
|
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 12 | # GNU General Public License for more details.
|
||
| 13 | 909:cbb26bc654de | Chris | #
|
| 14 | 0:513646585e45 | Chris | # You should have received a copy of the GNU General Public License
|
| 15 | # along with this program; if not, write to the Free Software
|
||
| 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||
| 17 | |||
| 18 | class CustomField < ActiveRecord::Base |
||
| 19 | 1115:433d4f72a19b | Chris | include Redmine::SubclassFactory |
| 20 | |||
| 21 | 0:513646585e45 | Chris | has_many :custom_values, :dependent => :delete_all |
| 22 | 1464:261b3d9a4903 | Chris | has_and_belongs_to_many :roles, :join_table => "#{table_name_prefix}custom_fields_roles#{table_name_suffix}", :foreign_key => "custom_field_id" |
| 23 | 0:513646585e45 | Chris | acts_as_list :scope => 'type = \'#{self.class}\'' |
| 24 | serialize :possible_values
|
||
| 25 | 1517:dffacf8a6908 | Chris | store :format_store
|
| 26 | 909:cbb26bc654de | Chris | |
| 27 | 0:513646585e45 | Chris | validates_presence_of :name, :field_format |
| 28 | validates_uniqueness_of :name, :scope => :type |
||
| 29 | validates_length_of :name, :maximum => 30 |
||
| 30 | 1517:dffacf8a6908 | Chris | validates_inclusion_of :field_format, :in => Proc.new { Redmine::FieldFormat.available_formats } |
| 31 | 1464:261b3d9a4903 | Chris | validate :validate_custom_field
|
| 32 | 0:513646585e45 | Chris | |
| 33 | 1115:433d4f72a19b | Chris | before_validation :set_searchable
|
| 34 | 1517:dffacf8a6908 | Chris | before_save do |field|
|
| 35 | field.format.before_custom_field_save(field) |
||
| 36 | end
|
||
| 37 | 1464:261b3d9a4903 | Chris | after_save :handle_multiplicity_change
|
| 38 | after_save do |field|
|
||
| 39 | if field.visible_changed? && field.visible
|
||
| 40 | field.roles.clear |
||
| 41 | end
|
||
| 42 | end
|
||
| 43 | 909:cbb26bc654de | Chris | |
| 44 | 1464:261b3d9a4903 | Chris | scope :sorted, lambda { order("#{table_name}.position ASC") } |
| 45 | scope :visible, lambda {|*args|
|
||
| 46 | user = args.shift || User.current
|
||
| 47 | if user.admin?
|
||
| 48 | # nop
|
||
| 49 | elsif user.memberships.any?
|
||
| 50 | where("#{table_name}.visible = ? OR #{table_name}.id IN (SELECT DISTINCT cfr.custom_field_id FROM #{Member.table_name} m" +
|
||
| 51 | " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
|
||
| 52 | " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
|
||
| 53 | " WHERE m.user_id = ?)",
|
||
| 54 | true, user.id)
|
||
| 55 | else
|
||
| 56 | where(:visible => true) |
||
| 57 | end
|
||
| 58 | } |
||
| 59 | 1115:433d4f72a19b | Chris | |
| 60 | 1464:261b3d9a4903 | Chris | def visible_by?(project, user=User.current) |
| 61 | visible? || user.admin? |
||
| 62 | end
|
||
| 63 | 1115:433d4f72a19b | Chris | |
| 64 | 1517:dffacf8a6908 | Chris | def format |
| 65 | @format ||= Redmine::FieldFormat.find(field_format) |
||
| 66 | end
|
||
| 67 | |||
| 68 | 1115:433d4f72a19b | Chris | def field_format=(arg) |
| 69 | # cannot change format of a saved custom field
|
||
| 70 | 1517:dffacf8a6908 | Chris | if new_record?
|
| 71 | @format = nil |
||
| 72 | super
|
||
| 73 | end
|
||
| 74 | 0:513646585e45 | Chris | end
|
| 75 | 909:cbb26bc654de | Chris | |
| 76 | 1115:433d4f72a19b | Chris | def set_searchable |
| 77 | 0:513646585e45 | Chris | # make sure these fields are not searchable
|
| 78 | 1517:dffacf8a6908 | Chris | self.searchable = false unless format.class.searchable_supported |
| 79 | 1115:433d4f72a19b | Chris | # make sure only these fields can have multiple values
|
| 80 | 1517:dffacf8a6908 | Chris | self.multiple = false unless format.class.multiple_supported |
| 81 | 0:513646585e45 | Chris | true
|
| 82 | end
|
||
| 83 | 909:cbb26bc654de | Chris | |
| 84 | 1115:433d4f72a19b | Chris | def validate_custom_field |
| 85 | 1517:dffacf8a6908 | Chris | format.validate_custom_field(self).each do |attribute, message| |
| 86 | errors.add attribute, message |
||
| 87 | 0:513646585e45 | Chris | end
|
| 88 | 909:cbb26bc654de | Chris | |
| 89 | if regexp.present?
|
||
| 90 | begin
|
||
| 91 | Regexp.new(regexp)
|
||
| 92 | rescue
|
||
| 93 | errors.add(:regexp, :invalid) |
||
| 94 | end
|
||
| 95 | end
|
||
| 96 | |||
| 97 | 1517:dffacf8a6908 | Chris | if default_value.present?
|
| 98 | validate_field_value(default_value).each do |message|
|
||
| 99 | errors.add :default_value, message
|
||
| 100 | end
|
||
| 101 | 1115:433d4f72a19b | Chris | end
|
| 102 | 0:513646585e45 | Chris | end
|
| 103 | 909:cbb26bc654de | Chris | |
| 104 | 1517:dffacf8a6908 | Chris | def possible_custom_value_options(custom_value) |
| 105 | format.possible_custom_value_options(custom_value) |
||
| 106 | end
|
||
| 107 | |||
| 108 | def possible_values_options(object=nil) |
||
| 109 | if object.is_a?(Array) |
||
| 110 | object.map {|o| format.possible_values_options(self, o)}.reduce(:&) || []
|
||
| 111 | 441:cbce1fd3b1b7 | Chris | else
|
| 112 | 1517:dffacf8a6908 | Chris | format.possible_values_options(self, object) || []
|
| 113 | 441:cbce1fd3b1b7 | Chris | end
|
| 114 | end
|
||
| 115 | 909:cbb26bc654de | Chris | |
| 116 | 1517:dffacf8a6908 | Chris | def possible_values |
| 117 | values = read_attribute(:possible_values)
|
||
| 118 | if values.is_a?(Array) |
||
| 119 | values.each do |value|
|
||
| 120 | value.force_encoding('UTF-8') if value.respond_to?(:force_encoding) |
||
| 121 | end
|
||
| 122 | values |
||
| 123 | 441:cbce1fd3b1b7 | Chris | else
|
| 124 | 1517:dffacf8a6908 | Chris | [] |
| 125 | 441:cbce1fd3b1b7 | Chris | end
|
| 126 | end
|
||
| 127 | 909:cbb26bc654de | Chris | |
| 128 | 0:513646585e45 | Chris | # Makes possible_values accept a multiline string
|
| 129 | def possible_values=(arg) |
||
| 130 | if arg.is_a?(Array) |
||
| 131 | 1517:dffacf8a6908 | Chris | values = arg.compact.collect(&:strip).select {|v| !v.blank?}
|
| 132 | write_attribute(:possible_values, values)
|
||
| 133 | 0:513646585e45 | Chris | else
|
| 134 | self.possible_values = arg.to_s.split(/[\n\r]+/) |
||
| 135 | end
|
||
| 136 | end
|
||
| 137 | 909:cbb26bc654de | Chris | |
| 138 | 0:513646585e45 | Chris | def cast_value(value) |
| 139 | 1517:dffacf8a6908 | Chris | format.cast_value(self, value)
|
| 140 | 0:513646585e45 | Chris | end
|
| 141 | 909:cbb26bc654de | Chris | |
| 142 | 1115:433d4f72a19b | Chris | def value_from_keyword(keyword, customized) |
| 143 | possible_values_options = possible_values_options(customized) |
||
| 144 | if possible_values_options.present?
|
||
| 145 | keyword = keyword.to_s.downcase |
||
| 146 | if v = possible_values_options.detect {|text, id| text.downcase == keyword}
|
||
| 147 | if v.is_a?(Array) |
||
| 148 | v.last |
||
| 149 | else
|
||
| 150 | v |
||
| 151 | end
|
||
| 152 | end
|
||
| 153 | else
|
||
| 154 | keyword |
||
| 155 | end
|
||
| 156 | end
|
||
| 157 | 1464:261b3d9a4903 | Chris | |
| 158 | 0:513646585e45 | Chris | # Returns a ORDER BY clause that can used to sort customized
|
| 159 | # objects by their value of the custom field.
|
||
| 160 | 1115:433d4f72a19b | Chris | # Returns nil if the custom field can not be used for sorting.
|
| 161 | 0:513646585e45 | Chris | def order_statement |
| 162 | 1115:433d4f72a19b | Chris | return nil if multiple? |
| 163 | 1517:dffacf8a6908 | Chris | format.order_statement(self)
|
| 164 | 0:513646585e45 | Chris | end
|
| 165 | |||
| 166 | 1115:433d4f72a19b | Chris | # Returns a GROUP BY clause that can used to group by custom value
|
| 167 | # Returns nil if the custom field can not be used for grouping.
|
||
| 168 | 1464:261b3d9a4903 | Chris | def group_statement |
| 169 | 1115:433d4f72a19b | Chris | return nil if multiple? |
| 170 | 1517:dffacf8a6908 | Chris | format.group_statement(self)
|
| 171 | 1115:433d4f72a19b | Chris | end
|
| 172 | |||
| 173 | def join_for_order_statement |
||
| 174 | 1517:dffacf8a6908 | Chris | format.join_for_order_statement(self)
|
| 175 | 1115:433d4f72a19b | Chris | end
|
| 176 | |||
| 177 | 1517:dffacf8a6908 | Chris | def visibility_by_project_condition(project_key=nil, user=User.current, id_column=nil) |
| 178 | 1464:261b3d9a4903 | Chris | if visible? || user.admin?
|
| 179 | "1=1"
|
||
| 180 | elsif user.anonymous?
|
||
| 181 | "1=0"
|
||
| 182 | else
|
||
| 183 | project_key ||= "#{self.class.customized_class.table_name}.project_id"
|
||
| 184 | 1517:dffacf8a6908 | Chris | id_column ||= id |
| 185 | 1464:261b3d9a4903 | Chris | "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" +
|
| 186 | " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
|
||
| 187 | " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
|
||
| 188 | 1517:dffacf8a6908 | Chris | " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id_column})"
|
| 189 | 1464:261b3d9a4903 | Chris | end
|
| 190 | end
|
||
| 191 | |||
| 192 | def self.visibility_condition |
||
| 193 | if user.admin?
|
||
| 194 | "1=1"
|
||
| 195 | elsif user.anonymous?
|
||
| 196 | "#{table_name}.visible"
|
||
| 197 | else
|
||
| 198 | "#{project_key} IN (SELECT DISTINCT m.project_id FROM #{Member.table_name} m" +
|
||
| 199 | " INNER JOIN #{MemberRole.table_name} mr ON mr.member_id = m.id" +
|
||
| 200 | " INNER JOIN #{table_name_prefix}custom_fields_roles#{table_name_suffix} cfr ON cfr.role_id = mr.role_id" +
|
||
| 201 | " WHERE m.user_id = #{user.id} AND cfr.custom_field_id = #{id})"
|
||
| 202 | end
|
||
| 203 | end
|
||
| 204 | |||
| 205 | 0:513646585e45 | Chris | def <=>(field) |
| 206 | position <=> field.position |
||
| 207 | end
|
||
| 208 | 909:cbb26bc654de | Chris | |
| 209 | 1115:433d4f72a19b | Chris | # Returns the class that values represent
|
| 210 | def value_class |
||
| 211 | 1517:dffacf8a6908 | Chris | format.target_class if format.respond_to?(:target_class) |
| 212 | 1115:433d4f72a19b | Chris | end
|
| 213 | |||
| 214 | 0:513646585e45 | Chris | def self.customized_class |
| 215 | self.name =~ /^(.+)CustomField$/ |
||
| 216 | 1464:261b3d9a4903 | Chris | $1.constantize rescue nil |
| 217 | 0:513646585e45 | Chris | end
|
| 218 | 909:cbb26bc654de | Chris | |
| 219 | 0:513646585e45 | Chris | # to move in project_custom_field
|
| 220 | def self.for_all |
||
| 221 | 1464:261b3d9a4903 | Chris | where(:is_for_all => true).order('position').all |
| 222 | 0:513646585e45 | Chris | end
|
| 223 | 909:cbb26bc654de | Chris | |
| 224 | 0:513646585e45 | Chris | def type_name |
| 225 | nil
|
||
| 226 | end
|
||
| 227 | 1115:433d4f72a19b | Chris | |
| 228 | # Returns the error messages for the given value
|
||
| 229 | # or an empty array if value is a valid value for the custom field
|
||
| 230 | 1517:dffacf8a6908 | Chris | def validate_custom_value(custom_value) |
| 231 | value = custom_value.value |
||
| 232 | 1115:433d4f72a19b | Chris | errs = [] |
| 233 | if value.is_a?(Array) |
||
| 234 | if !multiple?
|
||
| 235 | errs << ::I18n.t('activerecord.errors.messages.invalid') |
||
| 236 | end
|
||
| 237 | if is_required? && value.detect(&:present?).nil? |
||
| 238 | errs << ::I18n.t('activerecord.errors.messages.blank') |
||
| 239 | end
|
||
| 240 | else
|
||
| 241 | if is_required? && value.blank?
|
||
| 242 | errs << ::I18n.t('activerecord.errors.messages.blank') |
||
| 243 | end
|
||
| 244 | end
|
||
| 245 | 1517:dffacf8a6908 | Chris | errs += format.validate_custom_value(custom_value) |
| 246 | 1115:433d4f72a19b | Chris | errs |
| 247 | end
|
||
| 248 | |||
| 249 | 1517:dffacf8a6908 | Chris | # Returns the error messages for the default custom field value
|
| 250 | def validate_field_value(value) |
||
| 251 | validate_custom_value(CustomValue.new(:custom_field => self, :value => value)) |
||
| 252 | end
|
||
| 253 | |||
| 254 | 1115:433d4f72a19b | Chris | # Returns true if value is a valid value for the custom field
|
| 255 | def valid_field_value?(value) |
||
| 256 | validate_field_value(value).empty? |
||
| 257 | end
|
||
| 258 | |||
| 259 | def format_in?(*args) |
||
| 260 | args.include?(field_format) |
||
| 261 | end
|
||
| 262 | |||
| 263 | protected |
||
| 264 | |||
| 265 | 1464:261b3d9a4903 | Chris | # Removes multiple values for the custom field after setting the multiple attribute to false
|
| 266 | # We kepp the value with the highest id for each customized object
|
||
| 267 | def handle_multiplicity_change |
||
| 268 | if !new_record? && multiple_was && !multiple
|
||
| 269 | ids = custom_values. |
||
| 270 | where("EXISTS(SELECT 1 FROM #{CustomValue.table_name} cve WHERE cve.custom_field_id = #{CustomValue.table_name}.custom_field_id" +
|
||
| 271 | " AND cve.customized_type = #{CustomValue.table_name}.customized_type AND cve.customized_id = #{CustomValue.table_name}.customized_id" +
|
||
| 272 | " AND cve.id > #{CustomValue.table_name}.id)").
|
||
| 273 | pluck(:id)
|
||
| 274 | |||
| 275 | if ids.any?
|
||
| 276 | custom_values.where(:id => ids).delete_all
|
||
| 277 | end
|
||
| 278 | end
|
||
| 279 | end
|
||
| 280 | 0:513646585e45 | Chris | end
|
| 281 | 1517:dffacf8a6908 | Chris | |
| 282 | require_dependency 'redmine/field_format' |