2025-07-18 17:35:57 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2011-05-29 13:11:52 -07:00
|
|
|
#-- copyright
|
2020-01-15 11:31:26 +01:00
|
|
|
# OpenProject is an open source project management software.
|
2024-07-30 13:42:36 +02:00
|
|
|
# Copyright (C) the OpenProject GmbH
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2011-05-29 13:11:52 -07:00
|
|
|
# This program is free software; you can redistribute it and/or
|
2013-06-05 16:27:56 +02:00
|
|
|
# modify it under the terms of the GNU General Public License version 3.
|
2011-05-30 20:52:25 +02:00
|
|
|
#
|
2013-09-16 17:59:31 +02:00
|
|
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
2021-01-13 17:47:45 +01:00
|
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
2013-09-16 17:59:31 +02:00
|
|
|
# Copyright (C) 2010-2013 the ChiliProject Team
|
|
|
|
|
#
|
|
|
|
|
# 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.
|
|
|
|
|
#
|
|
|
|
|
# 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.
|
|
|
|
|
#
|
|
|
|
|
# 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.
|
|
|
|
|
#
|
2021-09-02 21:49:06 +02:00
|
|
|
# See COPYRIGHT and LICENSE files for more details.
|
2011-05-29 13:11:52 -07:00
|
|
|
#++
|
|
|
|
|
|
2020-04-09 11:54:26 +02:00
|
|
|
class CustomValue < ApplicationRecord
|
2007-03-12 17:59:02 +00:00
|
|
|
belongs_to :custom_field
|
2014-11-03 21:10:39 +01:00
|
|
|
belongs_to :customized, polymorphic: true
|
2025-09-08 12:12:42 +02:00
|
|
|
|
2012-08-28 17:53:39 +02:00
|
|
|
validate :validate_presence_of_required_value
|
|
|
|
|
validate :validate_format_of_value
|
|
|
|
|
validate :validate_type_of_value
|
|
|
|
|
validate :validate_length_of_value
|
|
|
|
|
|
2024-03-08 10:47:03 +07:00
|
|
|
after_create :activate_custom_field_in_customized_project, if: -> { customized.is_a?(Project) }
|
2024-02-28 15:31:11 +07:00
|
|
|
|
2017-05-08 10:39:09 +02:00
|
|
|
delegate :typed_value,
|
|
|
|
|
:formatted_value,
|
|
|
|
|
to: :strategy
|
2015-02-25 14:54:17 +01:00
|
|
|
|
2017-05-08 10:50:45 +02:00
|
|
|
delegate :editable?,
|
2024-08-05 17:10:56 +03:00
|
|
|
:admin_only?,
|
2017-05-08 10:50:45 +02:00
|
|
|
:required?,
|
2025-12-04 15:55:10 +01:00
|
|
|
:is_for_all?,
|
2017-05-08 10:50:45 +02:00
|
|
|
:max_length,
|
|
|
|
|
:min_length,
|
2025-10-23 11:50:29 +03:00
|
|
|
:calculated_value?,
|
2017-05-08 10:50:45 +02:00
|
|
|
to: :custom_field
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2009-01-11 16:33:51 +00:00
|
|
|
delegate :to_s, to: :value
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2016-11-17 09:09:51 +01:00
|
|
|
def value=(val)
|
|
|
|
|
parsed_value = strategy.parse_value(val)
|
2012-08-28 17:53:39 +02:00
|
|
|
|
2016-11-17 09:09:51 +01:00
|
|
|
super(parsed_value)
|
2016-09-14 11:36:44 +02:00
|
|
|
end
|
|
|
|
|
|
2021-03-30 17:29:32 +02:00
|
|
|
def strategy
|
|
|
|
|
@strategy ||= begin
|
|
|
|
|
format = custom_field&.field_format || "empty"
|
2024-11-12 14:01:49 +01:00
|
|
|
OpenProject::CustomFieldFormat.find_by(name: format).formatter.new(self)
|
2021-03-30 17:29:32 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2023-01-11 08:55:15 +01:00
|
|
|
def default?
|
2023-08-25 10:30:40 +02:00
|
|
|
value_is_included_in_multi_value_default? \
|
|
|
|
|
|| value_is_same_as_default?
|
2023-01-11 08:55:15 +01:00
|
|
|
end
|
|
|
|
|
|
2024-02-28 15:31:11 +07:00
|
|
|
def activate_custom_field_in_customized_project
|
2024-03-08 10:47:03 +07:00
|
|
|
return if default? || value.blank?
|
|
|
|
|
|
2024-02-28 15:31:11 +07:00
|
|
|
# if a custom value is created for a project via CustomValue.create(...),
|
|
|
|
|
# the custom field needs to be activated in the project
|
|
|
|
|
unless customized&.project_custom_fields&.include?(custom_field)
|
|
|
|
|
customized.project_custom_fields << custom_field
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-11-17 09:09:51 +01:00
|
|
|
protected
|
|
|
|
|
|
2023-08-25 10:30:40 +02:00
|
|
|
def value_is_included_in_multi_value_default?
|
|
|
|
|
return false unless custom_field.multi_value?
|
|
|
|
|
|
|
|
|
|
[custom_field.default_value, value].all?(&:blank?) \
|
|
|
|
|
|| custom_field.default_value&.include?(custom_field.cast_value(value))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def value_is_same_as_default?
|
|
|
|
|
custom_field.cast_value(value) == custom_field.default_value
|
|
|
|
|
end
|
|
|
|
|
|
2012-08-28 17:53:39 +02:00
|
|
|
def validate_presence_of_required_value
|
2017-05-08 10:50:45 +02:00
|
|
|
errors.add(:value, :blank) if custom_field.required? && !strategy.value_present?
|
2012-08-28 17:53:39 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def validate_format_of_value
|
2021-02-11 16:02:18 +01:00
|
|
|
if value.present? && custom_field.has_regexp? && !(value =~ Regexp.new(custom_field.regexp))
|
2024-04-24 14:14:08 +02:00
|
|
|
errors.add(:value, :regex_match_failed, expression: custom_field.regexp)
|
2012-08-28 17:53:39 +02:00
|
|
|
end
|
2017-07-20 10:46:55 +02:00
|
|
|
rescue RegexpError => e
|
|
|
|
|
errors.add(:base, :regex_invalid)
|
|
|
|
|
Rails.logger.error "Custom Field ID#{custom_field_id} has an invalid regex: #{e.message}"
|
2012-08-28 17:53:39 +02:00
|
|
|
end
|
2011-05-30 20:52:25 +02:00
|
|
|
|
2012-08-28 17:53:39 +02:00
|
|
|
def validate_type_of_value
|
|
|
|
|
if value.present?
|
2015-02-26 16:01:42 +01:00
|
|
|
validation_error = strategy.validate_type_of_value
|
|
|
|
|
if validation_error
|
|
|
|
|
errors.add(:value, validation_error)
|
2008-01-24 18:15:38 +00:00
|
|
|
end
|
2007-03-12 17:59:02 +00:00
|
|
|
end
|
2006-06-28 18:11:03 +00:00
|
|
|
end
|
2012-08-28 17:53:39 +02:00
|
|
|
|
|
|
|
|
def validate_length_of_value
|
2017-05-08 10:50:45 +02:00
|
|
|
if value.present? && (min_length.present? || max_length.present?)
|
|
|
|
|
validate_min_length_of_value
|
|
|
|
|
validate_max_length_of_value
|
2012-08-28 17:53:39 +02:00
|
|
|
end
|
|
|
|
|
end
|
2015-02-25 14:54:17 +01:00
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2017-05-08 10:50:45 +02:00
|
|
|
def validate_min_length_of_value
|
|
|
|
|
errors.add(:value, :too_short, count: min_length) if min_length > 0 && value.length < min_length
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def validate_max_length_of_value
|
|
|
|
|
errors.add(:value, :too_long, count: max_length) if max_length > 0 && value.length > max_length
|
|
|
|
|
end
|
2007-03-12 17:59:02 +00:00
|
|
|
end
|