Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

137 lines
4.1 KiB
Ruby
Raw Permalink Normal View History

2025-07-18 17:35:57 +01:00
# frozen_string_literal: true
#-- copyright
2020-01-15 11:31:26 +01:00
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
2011-05-30 20:52:25 +02:00
#
# This program is free software; you can redistribute it and/or
# 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.
#
# See COPYRIGHT and LICENSE files for more details.
#++
class CustomValue < ApplicationRecord
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
validate :validate_presence_of_required_value
validate :validate_format_of_value
validate :validate_type_of_value
validate :validate_length_of_value
after_create :activate_custom_field_in_customized_project, if: -> { customized.is_a?(Project) }
2024-02-28 15:31:11 +07:00
delegate :typed_value,
:formatted_value,
to: :strategy
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?,
:is_for_all?,
2017-05-08 10:50:45 +02:00
:max_length,
:min_length,
:calculated_value?,
2017-05-08 10:50:45 +02:00
to: :custom_field
2011-05-30 20:52:25 +02:00
delegate :to_s, to: :value
2011-05-30 20:52:25 +02:00
def value=(val)
parsed_value = strategy.parse_value(val)
super(parsed_value)
end
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)
end
end
def default?
value_is_included_in_multi_value_default? \
|| value_is_same_as_default?
end
2024-02-28 15:31:11 +07:00
def activate_custom_field_in_customized_project
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
protected
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
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?
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)
end
rescue RegexpError => e
errors.add(:base, :regex_invalid)
Rails.logger.error "Custom Field ID#{custom_field_id} has an invalid regex: #{e.message}"
end
2011-05-30 20:52:25 +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)
end
end
2006-06-28 18:11:03 +00:00
end
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
end
end
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
end