Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

3.4 KB
Raw Blame
# 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.
#++

module OpenProject
  class CustomFieldFormat
    include Redmine::I18n

    class_attribute :registered_by_name, default: {}

    attr_reader :name, :order, :label, :edit_as

    def initialize(name,
                   label:,
                   order:,
                   edit_as: name,
                   only: nil,
                   multi_value_possible: false,
                   enterprise_feature: nil,
                   enabled: lambda { true },
                   formatter: "CustomValue::StringStrategy")
      @name = name
      @label = label
      @order = order
      @edit_as = edit_as
      @class_names = only
      @multi_value_possible = multi_value_possible
      @enterprise_feature = enterprise_feature
      @enabled = enabled
      @formatter = formatter
    end

    def multi_value_possible?
      @multi_value_possible
    end

    def formatter
      # avoid using stale definitions in dev mode
      Kernel.const_get(@formatter)
    end

    def available?
      enabled? && enterprise_feature_allowed?
    end

    def enabled?
      @enabled.call
    end

    def disabled?
      !enabled?
    end

    def enterprise_feature_allowed?
      !@enterprise_feature || EnterpriseToken.allows_to?(@enterprise_feature)
    end

    def for_class_name?(class_name)
      @class_names.nil? || @class_names.include?(class_name)
    end

    class << self
      def registered = registered_by_name.values

      def map(&)
        yield self
      end

      # Registers a custom field format
      def register(custom_field_format, _options = {})
        return if registered_by_name.has_key?(custom_field_format.name)

        registered_by_name[custom_field_format.name] = custom_field_format
      end

      def available
        registered.select(&:available?)
      end

      def available_formats
        available.map(&:name)
      end

      def find_by(name:)
        registered_by_name[name.to_s]
      end

      def available_for_class_name(class_name)
        available
          .select { |format| format.for_class_name?(class_name) && !format.label.nil? }
          .sort_by(&:order)
      end

      def disabled_formats
        registered.select(&:disabled?).map(&:name)
      end
    end
  end
end