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

refactor spread code into a type sub class

as-op862f84b6ced7
diff --git a/app/components/work_packages/exports/generate/modal_dialog_component.rb b/app/components/work_packages/exports/generate/modal_dialog_component.rb
index 5015be9a77a..248fd641474 100644
--- a/app/components/work_packages/exports/generate/modal_dialog_component.rb
+++ b/app/components/work_packages/exports/generate/modal_dialog_component.rb
@@ -59,7 +59,7 @@ module WorkPackages
end
def templates_options
- work_package.type.pdf_export_templates_for_type.filter(&:enabled)
+ work_package.type.pdf_export_templates.list_enabled
end
def hyphenation_default
diff --git a/app/components/work_packages/types/export_template_list_component.rb b/app/components/work_packages/types/export_template_list_component.rb
index 8e3818e21ab..a53536d3cca 100644
--- a/app/components/work_packages/types/export_template_list_component.rb
+++ b/app/components/work_packages/types/export_template_list_component.rb
@@ -65,7 +65,7 @@ module WorkPackages
end
def pdf_export_templates
- @type.pdf_export_templates_for_type
+ @type.pdf_export_templates.list
end
end
end
diff --git a/app/controllers/work_packages/types/pdf_export_template_controller.rb b/app/controllers/work_packages/types/pdf_export_template_controller.rb
index 0393167a93f..f39133f8456 100644
--- a/app/controllers/work_packages/types/pdf_export_template_controller.rb
+++ b/app/controllers/work_packages/types/pdf_export_template_controller.rb
@@ -39,7 +39,7 @@ module WorkPackages
def enable_all
return render_404_turbo_stream if @type.nil?
- @type.export_templates_disabled = []
+ @type.pdf_export_templates.enable_all
@type.save!
respond_section_with_turbo_streams
end
@@ -47,8 +47,7 @@ module WorkPackages
def disable_all
return render_404_turbo_stream if @type.nil?
- @type.export_templates_disabled = WorkPackage::PDFExport::Templates::static_templates
- .pluck(:id)
+ @type.pdf_export_templates.disable_all
@type.save!
respond_section_with_turbo_streams
end
@@ -56,40 +55,21 @@ module WorkPackages
def toggle
return render_404_turbo_stream if @template.nil?
- toggle_template(@template.id)
+ @type.pdf_export_templates.toggle(@template.id)
+ @type.save!
respond_with_turbo_streams
end
def drop
return render_404_turbo_stream if @template.nil?
- position = params[:position].to_i - 1 # drop index starts at 1
- move_template(@template.id, position)
+ @type.pdf_export_templates.move(@template.id, params[:position].to_i - 1) # drop index starts at 1
+ @type.save!
respond_to_with_turbo_streams
end
protected
- def move_template(template_id, position)
- template_ids = @type.pdf_export_templates_for_type.map(&:id)
- prev_index = template_ids.find_index(template_id)
- template_ids.delete_at(prev_index) unless prev_index.nil?
- template_ids.insert(position, template_id)
- @type.export_templates_order = template_ids
- @type.save!
- end
-
- def toggle_template(template_id)
- disabled = @type.export_templates_disabled || []
- if disabled.include?(template_id)
- disabled.delete(template_id)
- else
- disabled.push(template_id)
- end
- @type.export_templates_disabled = disabled
- @type.save!
- end
-
def respond_section_with_turbo_streams
replace_via_turbo_stream(
component: ::WorkPackages::Types::ExportTemplateListComponent.new(type: @type)
@@ -106,7 +86,7 @@ module WorkPackages
end
def find_template
- @template = @type.pdf_export_templates_for_type.find { |t| t.id == params[:id] }
+ @template = @type.pdf_export_templates.find(params[:id])
end
end
end
diff --git a/app/models/type.rb b/app/models/type.rb
index af6bb80a8ab..6c3020bf00a 100644
--- a/app/models/type.rb
+++ b/app/models/type.rb
@@ -38,8 +38,8 @@ class Type < ApplicationRecord
attribute :patterns, Types::Patterns::CollectionType.new
- store_attribute :pdf_export_templates, :export_templates_disabled, :json
- store_attribute :pdf_export_templates, :export_templates_order, :json
+ store_attribute :pdf_export_templates_config, :export_templates_disabled, :json
+ store_attribute :pdf_export_templates_config, :export_templates_order, :json
before_destroy :check_integrity
@@ -115,8 +115,8 @@ class Type < ApplicationRecord
patterns.all_enabled
end
- def pdf_export_templates_for_type
- WorkPackage::PDFExport::Templates::build_template_list(export_templates_disabled, export_templates_order)
+ def pdf_export_templates
+ @pdf_export_templates ||= ::Type::PdfExportTemplates.new(self)
end
private
diff --git a/app/models/type/pdf_export_templates.rb b/app/models/type/pdf_export_templates.rb
new file mode 100644
index 00000000000..5b07acf3cc1
--- /dev/null
+++ b/app/models/type/pdf_export_templates.rb
@@ -0,0 +1,91 @@
+#-- 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 Type::PdfExportTemplates
+ Template = Data.define(:id, :label, :caption, :enabled)
+
+ def initialize(model)
+ @type = model
+ end
+
+ def list
+ disabled = @type.export_templates_disabled || []
+ templates = build_in_templates.map do |build_in_template|
+ Template.new(**build_in_template, enabled: disabled.exclude?(build_in_template[:id]))
+ end
+ order = @type.export_templates_order || []
+ return templates if order.empty?
+
+ indexes = order.each_with_index.to_a.to_h
+ templates.sort_by { |template| indexes[template.id] }
+ end
+
+ def list_enabled
+ list.filter(&:enabled)
+ end
+
+ def find(template_id)
+ build_in_template = build_in_templates.find { |t| t[:id] == template_id }
+ if build_in_template
+ disabled = @type.export_templates_disabled || []
+ Template.new(**build_in_template, enabled: disabled.exclude?(build_in_template[:id]))
+ end
+ end
+
+ def enable_all
+ @type.export_templates_disabled = []
+ end
+
+ def disable_all
+ @type.export_templates_disabled = build_in_templates.pluck(:id)
+ end
+
+ def toggle(template_id)
+ disabled = @type.export_templates_disabled || []
+ if disabled.include?(template_id)
+ disabled.delete(template_id)
+ else
+ disabled.push(template_id)
+ end
+ @type.export_templates_disabled = disabled
+ end
+
+ def move(template_id, position)
+ template_ids = @type.pdf_export_templates_for_type.map(&:id)
+ prev_index = template_ids.find_index(template_id)
+ template_ids.delete_at(prev_index) unless prev_index.nil?
+ template_ids.insert(position, template_id)
+ @type.export_templates_order = template_ids
+ end
+
+ private
+
+ def build_in_templates
+ ::WorkPackage::PDFExport::Templates::TEMPLATES
+ end
+end
diff --git a/app/models/work_package/pdf_export/templates.rb b/app/models/work_package/pdf_export/templates.rb
index 733ed08c7a5..87d17c00c86 100644
--- a/app/models/work_package/pdf_export/templates.rb
+++ b/app/models/work_package/pdf_export/templates.rb
@@ -28,35 +28,17 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module WorkPackage::PDFExport
- Template = Data.define(:id, :label, :caption, :enabled)
-
- class Templates
- def self.build_template_list(disabled_ids, ordered_ids)
- disabled = disabled_ids || []
- templates = static_templates.map do |build_in_template|
- Template.new(**build_in_template, enabled: disabled.exclude?(build_in_template[:id]))
- end
- order = ordered_ids || []
- return templates if order.empty?
-
- indexes = order.each_with_index.to_a.to_h
- templates.sort_by { |template| indexes[template.id] }
- end
-
- def self.static_templates
- [
- {
- id: "attributes",
- label: I18n.t("pdf_generator.template_attributes.label"),
- caption: I18n.t("pdf_generator.template_attributes.caption")
- },
- {
- id: "contract",
- label: I18n.t("pdf_generator.template_contract.label"),
- caption: I18n.t("pdf_generator.template_contract.caption")
- }
- ]
- end
- end
+module WorkPackage::PDFExport::Templates
+ TEMPLATES = [
+ {
+ id: "attributes",
+ label: I18n.t("pdf_generator.template_attributes.label"),
+ caption: I18n.t("pdf_generator.template_attributes.caption")
+ },
+ {
+ id: "contract",
+ label: I18n.t("pdf_generator.template_contract.label"),
+ caption: I18n.t("pdf_generator.template_contract.caption")
+ }
+ ]
end
diff --git a/db/migrate/20250210163523_add_export_templates_to_type.rb b/db/migrate/20250210163523_add_export_templates_to_type.rb
index 150137ab1a9..4dc5a9d176d 100644
--- a/db/migrate/20250210163523_add_export_templates_to_type.rb
+++ b/db/migrate/20250210163523_add_export_templates_to_type.rb
@@ -30,6 +30,6 @@
class AddExportTemplatesToType < ActiveRecord::Migration[7.1]
def change
- add_column :types, :pdf_export_templates, :jsonb, default: {}
+ add_column :types, :pdf_export_templates_config, :jsonb, default: {}
end
end