2025-07-18 17:35:57 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2017-07-12 21:13:28 +02: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
|
2017-07-12 21:13:28 +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.
|
|
|
|
|
#
|
|
|
|
|
# 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
|
2017-07-12 21:13:28 +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.
|
2017-07-12 21:13:28 +02:00
|
|
|
#++
|
2017-07-03 14:49:13 +02:00
|
|
|
|
2020-04-09 11:54:26 +02:00
|
|
|
class AttributeHelpText < ApplicationRecord
|
2020-05-26 10:56:14 +02:00
|
|
|
acts_as_attachable viewable_by_all_users: true
|
|
|
|
|
|
2025-06-03 12:15:29 +01:00
|
|
|
def self.cached(user)
|
2025-06-30 13:41:41 +02:00
|
|
|
RequestStore.fetch(name) do
|
2026-03-23 11:55:28 +01:00
|
|
|
visible_condition(user).where(type: name).select(:id, :type, :attribute_name, :caption).index_by(&:attribute_name)
|
2025-06-30 13:41:41 +02:00
|
|
|
end
|
2025-06-03 12:15:29 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.for(model)
|
|
|
|
|
subclasses.find { |child| child.name.demodulize == model.model_name }
|
|
|
|
|
end
|
|
|
|
|
|
2017-07-03 14:49:13 +02:00
|
|
|
def self.available_types
|
|
|
|
|
subclasses.map { |child| child.name.demodulize }
|
|
|
|
|
end
|
|
|
|
|
|
2017-07-05 14:46:10 +02:00
|
|
|
def self.used_attributes(type)
|
|
|
|
|
where(type:)
|
2017-07-03 14:49:13 +02:00
|
|
|
.select(:attribute_name)
|
|
|
|
|
.distinct
|
|
|
|
|
.pluck(:attribute_name)
|
|
|
|
|
end
|
|
|
|
|
|
2017-07-04 10:48:58 +02:00
|
|
|
def self.all_by_scope
|
|
|
|
|
all.group_by(&:attribute_scope)
|
|
|
|
|
end
|
|
|
|
|
|
2017-07-12 21:13:28 +02:00
|
|
|
def self.visible(user)
|
|
|
|
|
scope = AttributeHelpText.subclasses[0].visible_condition(user)
|
|
|
|
|
|
|
|
|
|
AttributeHelpText.subclasses[1..-1].each do |subclass|
|
|
|
|
|
scope = scope.or(subclass.visible_condition(user))
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
scope
|
|
|
|
|
end
|
|
|
|
|
|
2025-06-09 13:06:24 +01:00
|
|
|
normalizes :attribute_name, with: -> { it.delete_suffix("_id") }
|
|
|
|
|
|
2021-11-22 11:09:39 +01:00
|
|
|
validates :help_text, presence: true
|
|
|
|
|
validates :attribute_name, uniqueness: { scope: :type }
|
2017-07-03 14:49:13 +02:00
|
|
|
|
2025-11-21 09:12:40 +01:00
|
|
|
def attribute_field_name
|
|
|
|
|
@attribute_field_name ||= self.class.available_attributes[attribute_name]
|
2017-07-03 14:49:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def attribute_scope
|
2020-05-26 10:56:14 +02:00
|
|
|
self.class.to_s.demodulize
|
2017-07-03 14:49:13 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def type_caption
|
2026-03-26 13:28:12 +01:00
|
|
|
raise SubclassResponsibilityError
|
2017-07-12 21:13:28 +02:00
|
|
|
end
|
|
|
|
|
|
2026-03-23 11:55:28 +01:00
|
|
|
def self.visible_condition(_user = nil)
|
2026-03-26 13:28:12 +01:00
|
|
|
raise SubclassResponsibilityError
|
2017-07-12 21:13:28 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def self.available_attributes
|
2026-03-26 13:28:12 +01:00
|
|
|
raise SubclassResponsibilityError
|
2017-07-03 14:49:13 +02:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-01-04 14:11:50 +01:00
|
|
|
require "attribute_help_text/work_package"
|
|
|
|
|
require "attribute_help_text/project"
|