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

Move export functionality into reporting module to avoid patching

Oliver Günther363a4cf8f8a1
diff --git a/modules/reporting/app/controllers/cost_reports_controller.rb b/modules/reporting/app/controllers/cost_reports_controller.rb
index d6a9e247770..a1c0d490cca 100644
--- a/modules/reporting/app/controllers/cost_reports_controller.rb
+++ b/modules/reporting/app/controllers/cost_reports_controller.rb
@@ -82,6 +82,15 @@ class CostReportsController < ApplicationController
format.html do
session[report_engine.name.underscore.to_sym].try(:delete, :name)
end
+
+ format.xls do
+ job_id = ::CostQuery::ScheduleExportService
+ .new(user: current_user)
+ .call(filter_params:, project: @project, cost_types: @cost_types)
+ .result
+
+ redirect_to job_status_path(job_id)
+ end
end
end
end
diff --git a/modules/reporting/app/models/cost_query/export.rb b/modules/reporting/app/models/cost_query/export.rb
new file mode 100644
index 00000000000..029a248569d
--- /dev/null
+++ b/modules/reporting/app/models/cost_query/export.rb
@@ -0,0 +1,10 @@
+class CostQuery::Export < Export
+ acts_as_attachable view_permission: :export_work_packages,
+ add_permission: :export_work_packages,
+ delete_permission: :export_work_packages,
+ only_user_allowed: true
+
+ def ready?
+ attachments.any?
+ end
+end
diff --git a/modules/xls_export/app/services/xls_export/cost_reports/schedule_export_service.rb b/modules/reporting/app/services/cost_query/schedule_export_service.rb
similarity index 56%
rename from modules/xls_export/app/services/xls_export/cost_reports/schedule_export_service.rb
rename to modules/reporting/app/services/cost_query/schedule_export_service.rb
index e12e2bf5ddb..059732e89ad 100644
--- a/modules/xls_export/app/services/xls_export/cost_reports/schedule_export_service.rb
+++ b/modules/reporting/app/services/cost_query/schedule_export_service.rb
@@ -26,32 +26,28 @@
# See COPYRIGHT and LICENSE files for more details.
#++
-module XlsExport
- module CostReports
- class ScheduleExportService
- attr_accessor :user
+class CostQuery::ScheduleExportService
+ attr_accessor :user
- def initialize(user:)
- self.user = user
- end
+ def initialize(user:)
+ self.user = user
+ end
- def call(filter_params:, project:, cost_types:)
- export_storage = XlsExport::CostReports::Export.create
- job = schedule_export(export_storage, filter_params, project, cost_types)
+ def call(filter_params:, project:, cost_types:)
+ export_storage = ::CostQuery::Export.create
+ job = schedule_export(export_storage, filter_params, project, cost_types)
- ServiceResult.success result: job.job_id
- end
+ ServiceResult.success result: job.job_id
+ end
- private
+ private
- def schedule_export(export_storage, filter_params, project, cost_types)
- XlsExport::CostReports::ExportJob.perform_later(export: export_storage,
- user:,
- mime_type: :xls,
- query: filter_params,
- project:,
- cost_types:)
- end
- end
+ def schedule_export(export_storage, filter_params, project, cost_types)
+ ::CostQuery::ExportJob.perform_later(export: export_storage,
+ user:,
+ mime_type: :xls,
+ query: filter_params,
+ project:,
+ cost_types:)
end
end
diff --git a/modules/reporting/app/views/cost_reports/index.html.erb b/modules/reporting/app/views/cost_reports/index.html.erb
index de858e902f8..4f9dc1a15c9 100644
--- a/modules/reporting/app/views/cost_reports/index.html.erb
+++ b/modules/reporting/app/views/cost_reports/index.html.erb
@@ -43,6 +43,16 @@ See COPYRIGHT and LICENSE files for more details.
</h2>
</div>
<ul class="toolbar-items">
+ <% if User.current.allowed_to? :export_work_packages, @project, global: @project.nil? %>
+ <li class="toolbar-item">
+ <%= link_to(t(:export_to_excel),
+ { controller: "cost_reports" ,
+ action: :index,
+ format: 'xls',
+ project_id: @project },
+ class: "button icon-export-xls-descr") %>
+ </li>
+ <% end %>
<%= call_hook(:view_cost_report_toolbar) %>
</ul>
</div>
diff --git a/modules/reporting/app/workers/cost_query/export_job.rb b/modules/reporting/app/workers/cost_query/export_job.rb
new file mode 100644
index 00000000000..90472f7add9
--- /dev/null
+++ b/modules/reporting/app/workers/cost_query/export_job.rb
@@ -0,0 +1,61 @@
+require 'active_storage/filename'
+
+class CostQuery::ExportJob < Exports::ExportJob
+ self.model = ::CostQuery
+
+ def title
+ I18n.t('export.cost_reports.title')
+ end
+
+ def project
+ options[:project]
+ end
+
+ def cost_types
+ options[:cost_types]
+ end
+
+ private
+
+ def prepare!
+ self.query = build_query(query)
+ end
+
+ def export!
+ # Build an xls file from a cost report.
+ # We only support extracting a simple xls table, so grouping is ignored.
+ handle_export_result(export, xls_report_result)
+ end
+
+ def xls_report_result
+ params = { query:, project:, cost_types: }
+ content = ::OpenProject::Reporting::CostEntryXlsTable.generate(params).xls
+ time = Time.zone.now.strftime('%d-%m-%Y-T-%H-%M-%S')
+ export_title = "cost-report-#{time}.xls"
+
+ ::Exports::Result.new(format: :xls,
+ title: export_title,
+ mime_type: 'application/vnd.ms-excel',
+ content:)
+ end
+
+ # rubocop:disable Metrics/AbcSize
+ def build_query(filters, groups = {})
+ query = CostQuery.new(project:)
+ query.tap do |q|
+ filters[:operators].each do |filter, operator|
+ unless filters[:values][filter] == ['<<inactive>>']
+ values = Array(filters[:values][filter]).map { |v| v == '<<null>>' ? nil : v }
+ q.filter(filter.to_sym,
+ operator:,
+ values:)
+ end
+ end
+ end
+ groups[:columns].try(:reverse_each) { |c| query.column(c) }
+ groups[:rows].try(:reverse_each) { |r| query.row(r) }
+ query
+ end
+
+ # rubocop:enable Metrics/AbcSize
+end
diff --git a/modules/reporting/config/locales/en.yml b/modules/reporting/config/locales/en.yml
index 62e391dbe0b..a59d5d0826d 100644
--- a/modules/reporting/config/locales/en.yml
+++ b/modules/reporting/config/locales/en.yml
@@ -99,6 +99,10 @@ en:
validation_failure_date: "is not a valid date"
validation_failure_integer: "is not a valid integer"
+ export:
+ cost_reports:
+ title: "Your Cost Reports XLS export"
+
reporting:
group_by:
selected_columns: "Selected columns"
diff --git a/modules/xls_export/lib/open_project/xls_export/xls_views/cost_entry_table.rb b/modules/reporting/lib/open_project/reporting/cost_entry_xls_table.rb
similarity index 96%
rename from modules/xls_export/lib/open_project/xls_export/xls_views/cost_entry_table.rb
rename to modules/reporting/lib/open_project/reporting/cost_entry_xls_table.rb
index 1b80420e5e9..11851766e81 100644
--- a/modules/xls_export/lib/open_project/xls_export/xls_views/cost_entry_table.rb
+++ b/modules/reporting/lib/open_project/reporting/cost_entry_xls_table.rb
@@ -1,4 +1,4 @@
-class OpenProject::XlsExport::XlsViews::CostEntryTable < OpenProject::XlsExport::XlsViews
+class OpenProject::Reporting::CostEntryXlsTable < OpenProject::XlsExport::XlsViews
def generate
@spreadsheet = OpenProject::XlsExport::SpreadsheetBuilder.new(I18n.t(:label_money))
default_query = serialize_query_without_hidden(@query)
diff --git a/modules/xls_export/app/models/xls_export/cost_reports/export.rb b/modules/xls_export/app/models/xls_export/cost_reports/export.rb
deleted file mode 100644
index 6986541d538..00000000000
--- a/modules/xls_export/app/models/xls_export/cost_reports/export.rb
+++ /dev/null
@@ -1,14 +0,0 @@
-module XlsExport
- module CostReports
- class Export < ::Export
- acts_as_attachable view_permission: :export_work_packages,
- add_permission: :export_work_packages,
- delete_permission: :export_work_packages,
- only_user_allowed: true
-
- def ready?
- attachments.any?
- end
- end
- end
-end
diff --git a/modules/xls_export/app/views/hooks/xls_report/_view_cost_report_toolbar.html.erb b/modules/xls_export/app/views/hooks/xls_report/_view_cost_report_toolbar.html.erb
index 518faed2ebd..e69de29bb2d 100644
--- a/modules/xls_export/app/views/hooks/xls_report/_view_cost_report_toolbar.html.erb
+++ b/modules/xls_export/app/views/hooks/xls_report/_view_cost_report_toolbar.html.erb
@@ -1,10 +0,0 @@
-<% if User.current.allowed_to? :export_work_packages, @project, global: @project.nil? %>
- <li class="toolbar-item">
- <%= link_to(t(:export_to_excel),
- { controller: "cost_reports" ,
- action: :index,
- format: 'xls',
- project_id: @project },
- class: "button icon-export-xls-descr") %>
- </li>
-<% end %>
diff --git a/modules/xls_export/app/workers/xls_export/cost_reports/export_job.rb b/modules/xls_export/app/workers/xls_export/cost_reports/export_job.rb
deleted file mode 100644
index 9512d1a4430..00000000000
--- a/modules/xls_export/app/workers/xls_export/cost_reports/export_job.rb
+++ /dev/null
@@ -1,62 +0,0 @@
-require 'active_storage/filename'
-
-module XlsExport
- module CostReports
- class ExportJob < ::Exports::ExportJob
- self.model = ::CostQuery
-
- def title
- I18n.t('export.cost_reports.title')
- end
-
- def project
- options[:project]
- end
-
- def cost_types
- options[:cost_types]
- end
-
- private
-
- def prepare!
- self.query = build_query(query)
- end
-
- def export!
- # Build an xls file from a cost report.
- # We only support extracting a simple xls table, so grouping is ignored.
- handle_export_result(export, xls_report_result)
- end
-
- def xls_report_result
- params = { query:, project:, cost_types: }
- content = ::OpenProject::XlsExport::XlsViews::CostEntryTable.generate(params).xls
- time = Time.now.strftime('%d-%m-%Y-T-%H-%M-%S')
- export_title = "cost-report-#{time}.xls"
-
- ::Exports::Result.new(format: :xls,
- title: export_title,
- mime_type: 'application/vnd.ms-excel',
- content:)
- end
-
- def build_query(filters, groups = {})
- query = CostQuery.new(project:)
- query.tap do |q|
- filters[:operators].each do |filter, operator|
- unless filters[:values][filter] == ['<<inactive>>']
- values = Array(filters[:values][filter]).map { |v| v == '<<null>>' ? nil : v }
- q.filter(filter.to_sym,
- operator:,
- values:)
- end
- end
- end
- groups[:columns].try(:reverse_each) { |c| query.column(c) }
- groups[:rows].try(:reverse_each) { |r| query.row(r) }
- query
- end
- end
- end
-end
diff --git a/modules/xls_export/config/locales/en.yml b/modules/xls_export/config/locales/en.yml
index 463ca2721f6..ff907b35a7b 100644
--- a/modules/xls_export/config/locales/en.yml
+++ b/modules/xls_export/config/locales/en.yml
@@ -5,8 +5,6 @@ en:
different_formats: Different formats
export:
- cost_reports:
- title: "Your Cost Reports XLS export"
format:
xls: "XLS"
xls_with_descriptions: "XLS with descriptions"
diff --git a/modules/xls_export/lib/open_project/xls_export/engine.rb b/modules/xls_export/lib/open_project/xls_export/engine.rb
index d63be150d3b..884d18c9407 100644
--- a/modules/xls_export/lib/open_project/xls_export/engine.rb
+++ b/modules/xls_export/lib/open_project/xls_export/engine.rb
@@ -8,10 +8,7 @@ module OpenProject::XlsExport
author_url: 'https://www.openproject.org',
bundled: true
- patches %i[CostReportsController]
-
config.to_prepare do
- OpenProject::XlsExport::Hooks::CostReportHook
OpenProject::XlsExport::Hooks::WorkPackageHook
end
diff --git a/modules/xls_export/lib/open_project/xls_export/hooks/cost_report_hook.rb b/modules/xls_export/lib/open_project/xls_export/hooks/cost_report_hook.rb
deleted file mode 100644
index 7b8508dea46..00000000000
--- a/modules/xls_export/lib/open_project/xls_export/hooks/cost_report_hook.rb
+++ /dev/null
@@ -1,5 +0,0 @@
-module OpenProject::XlsExport::Hooks
- class CostReportHook < OpenProject::Hook::ViewListener
- render_on :view_cost_report_toolbar, partial: 'hooks/xls_report/view_cost_report_toolbar'
- end
-end
diff --git a/modules/xls_export/lib/open_project/xls_export/patches/cost_reports_controller_patch.rb b/modules/xls_export/lib/open_project/xls_export/patches/cost_reports_controller_patch.rb
deleted file mode 100644
index 278b5e8176a..00000000000
--- a/modules/xls_export/lib/open_project/xls_export/patches/cost_reports_controller_patch.rb
+++ /dev/null
@@ -1,37 +0,0 @@
-module OpenProject::XlsExport::Patches
- module CostReportsControllerPatch
- def self.included(base)
- # :nodoc:
- base.prepend InstanceMethods
- end
-
- module InstanceMethods
- def excel_export?
- (params["action"] == "index" or params[:action] == "all") && params["format"] == "xls"
- end
-
- def ensure_project_scope?
- !excel_export? && super
- end
-
- # If the index action is called, hook the xls format into the cost report
- def respond_to
- return super unless excel_export?
-
- super do |format|
- yield format
- format.xls do
- job_id = XlsExport::CostReports::ScheduleExportService
- .new(user: current_user)
- .call(filter_params:, project: @project, cost_types: @cost_types)
- .result
-
- redirect_to job_status_path(job_id)
- end
- end
- end
- end
- end
-end
-
-CostReportsController.include OpenProject::XlsExport::Patches::CostReportsControllerPatch
diff --git a/modules/xls_export/openproject-xls_export.gemspec b/modules/xls_export/openproject-xls_export.gemspec
index 8ae8d44e04e..bdd739ea5e0 100644
--- a/modules/xls_export/openproject-xls_export.gemspec
+++ b/modules/xls_export/openproject-xls_export.gemspec
@@ -4,8 +4,7 @@ Gem::Specification.new do |s|
s.authors = "OpenProject GmbH"
s.email = "[email protected]"
s.summary = 'OpenProject XLS Export'
- s.description = 'Export issue lists as Excel spreadsheets (.xls). Support for exporting
- cost entries and cost reports is not yet migrated to Rails 3 and disabled.'
+ s.description = 'Export issue lists as Excel spreadsheets (.xls)'
s.license = "GPLv3"
s.files = Dir["{app,config,db,lib}/**/*"] + %w(CHANGELOG.md README.md)
diff --git a/modules/xls_export/spec/patches/cost_reports_controller_patch_spec.rb b/modules/xls_export/spec/patches/cost_reports_controller_patch_spec.rb
deleted file mode 100644
index d2b24bbfea4..00000000000
--- a/modules/xls_export/spec/patches/cost_reports_controller_patch_spec.rb
+++ /dev/null
@@ -1,21 +0,0 @@
-require 'spec_helper'
-
-describe 'CostReportsController', "rendering to xls" do
- skip 'XlsExport: CostReports support not yet migrated to Rails 3'
-
- it "responds with the xls if requested in the index" do
- skip
- render action: :index
- expect(response).to be_redirect
- end
-
- it "does not respond with the xls if requested in a detail view" do
- skip
- render action: :show
- expect(response).to be_redirect
- end
-
- it "generates xls from issues" do
- skip
- end
-end