mirror of
https://github.com/opf/openproject.git
synced 2026-06-14 03:30:14 +00:00
137 lines
4.1 KiB
Ruby
137 lines
4.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
#-- copyright
|
|
# OpenProject is an open source project management software.
|
|
# Copyright (C) the OpenProject GmbH
|
|
#
|
|
# This program is free software; you can redistribute it and/or
|
|
# modify it under the terms of the GNU General Public License version 3.
|
|
#
|
|
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
|
|
# Copyright (C) 2006-2013 Jean-Philippe Lang
|
|
# 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
|
|
belongs_to :customized, polymorphic: true
|
|
|
|
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) }
|
|
|
|
delegate :typed_value,
|
|
:formatted_value,
|
|
to: :strategy
|
|
|
|
delegate :editable?,
|
|
:admin_only?,
|
|
:required?,
|
|
:is_for_all?,
|
|
:max_length,
|
|
:min_length,
|
|
:calculated_value?,
|
|
to: :custom_field
|
|
|
|
delegate :to_s, to: :value
|
|
|
|
def value=(val)
|
|
parsed_value = strategy.parse_value(val)
|
|
|
|
super(parsed_value)
|
|
end
|
|
|
|
def strategy
|
|
@strategy ||= begin
|
|
format = custom_field&.field_format || "empty"
|
|
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
|
|
|
|
def activate_custom_field_in_customized_project
|
|
return if default? || value.blank?
|
|
|
|
# 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
|
|
errors.add(:value, :blank) if custom_field.required? && !strategy.value_present?
|
|
end
|
|
|
|
def validate_format_of_value
|
|
if value.present? && custom_field.has_regexp? && !(value =~ Regexp.new(custom_field.regexp))
|
|
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
|
|
|
|
def validate_type_of_value
|
|
if value.present?
|
|
validation_error = strategy.validate_type_of_value
|
|
if validation_error
|
|
errors.add(:value, validation_error)
|
|
end
|
|
end
|
|
end
|
|
|
|
def validate_length_of_value
|
|
if value.present? && (min_length.present? || max_length.present?)
|
|
validate_min_length_of_value
|
|
validate_max_length_of_value
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
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
|