diff --git a/Gemfile b/Gemfile index baf832869..f572df575 100644 --- a/Gemfile +++ b/Gemfile @@ -62,7 +62,7 @@ gem 'ajax-datatables-rails', '~> 0.3.1' gem 'aspector' # Aspect-oriented programming for Rails gem 'auto_strip_attributes', '~> 2.1' # Removes unnecessary whitespaces AR gem 'bcrypt', '~> 3.1.10' -gem 'caracal-rails' # Build docx report +gem 'caracal' # Build docx report gem 'deface', '~> 1.0' gem 'down', '~> 5.0' gem 'faker' # Generate fake data diff --git a/Gemfile.lock b/Gemfile.lock index 0e585e182..74688b9c5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -198,9 +198,6 @@ GEM nokogiri (~> 1.6) rubyzip (~> 1.1) tilt (>= 1.4) - caracal-rails (1.0.2) - caracal (~> 1.0) - rails (>= 3.2) case_transform (0.2) activesupport childprocess (3.0.0) @@ -647,7 +644,7 @@ DEPENDENCIES canaid! capybara capybara-email - caracal-rails + caracal cucumber-rails (~> 1.8) database_cleaner deface (~> 1.0) diff --git a/app/controllers/reports_controller.rb b/app/controllers/reports_controller.rb index 9901bc7a7..6f578ddc9 100644 --- a/app/controllers/reports_controller.rb +++ b/app/controllers/reports_controller.rb @@ -22,7 +22,7 @@ class ReportsController < ApplicationController result_contents ).freeze - before_action :load_vars, only: %i(edit update document_preview) + before_action :load_vars, only: %i(edit update document_preview generate) before_action :load_vars_nested, only: BEFORE_ACTION_METHODS before_action :load_visible_projects, only: %i(new edit) before_action :load_available_repositories, @@ -167,15 +167,20 @@ class ReportsController < ApplicationController end # Generation action - # Currently, only .PDF is supported def generate respond_to do |format| - format.docx do - @user = current_user - @team = current_team - @scinote_url = root_url - @data = params[:data] - headers["Content-Disposition"] = 'attachment; filename="scinote_report.docx"' + format.pdf do + render pdf: 'report', header: { html: { template: 'reports/header.pdf.erb' }}, + footer: { html: { template: 'reports/footer.pdf.erb', + locals: { current_time: I18n.l(Time.zone.now, format: :full) }}}, + locals: { content: content }, + template: 'reports/report.pdf.erb', + disable_javascript: true + end + format.json do + @report.update!(docx_file_processing: true) + Reports::DocxJob.perform_now(@report, params[:data], current_user, current_team, root_url) + render json: {}, status: :accepted end end end diff --git a/app/jobs/reports/docx_job.rb b/app/jobs/reports/docx_job.rb new file mode 100644 index 000000000..cfa82f412 --- /dev/null +++ b/app/jobs/reports/docx_job.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +module Reports + class DocxJob < ApplicationJob + queue_as :reports + + def perform(report, data, user, team, root_url) + file = Tempfile.new(['report', '.docx']) + begin + docx = Caracal::Document.new(file.path) + Reports::Docx.new(data, docx, user: user, team: team, scinote_url: root_url).draw + docx.save + report.docx_file.attach(io: file, filename: 'report.docx') + report.update!(docx_file_processing: false) + ensure + file.close + file.unlink + end + end + end +end diff --git a/app/views/reports/generate.docx.caracal b/app/views/reports/generate.docx.caracal deleted file mode 100644 index ed5b7bc45..000000000 --- a/app/views/reports/generate.docx.caracal +++ /dev/null @@ -1 +0,0 @@ -Reports::Docx.new(@data,docx,user: @user, team: @team, scinote_url: @scinote_url).draw \ No newline at end of file diff --git a/app/views/reports/new/_report_navigation.html.erb b/app/views/reports/new/_report_navigation.html.erb index bb43484d7..4096afc7f 100644 --- a/app/views/reports/new/_report_navigation.html.erb +++ b/app/views/reports/new/_report_navigation.html.erb @@ -26,8 +26,8 @@ diff --git a/config/routes.rb b/config/routes.rb index 129d75d34..7b51f8aa5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -249,10 +249,20 @@ Rails.application.routes.draw do resources :reports, path: '/reports', only: %i(edit update create) do + member do + post 'generate', to: 'reports#generate', format: %w(pdf json) + end + collection do # The posts following here should in theory be gets, # but are posts because of parameters payload - post 'generate', to: 'reports#generate', format: %w(docx pdf) + get 'new/', to: 'reports#new' + get 'new/project_contents_modal', + to: 'reports#project_contents_modal', + as: :project_contents_modal + post 'new/project_contents', + to: 'reports#project_contents', + as: :project_contents get 'new/experiment_contents_modal', to: 'reports#experiment_contents_modal', as: :experiment_contents_modal