refactor save_pdf_to_inventory_item service

This commit is contained in:
zmagod 2018-05-17 13:17:19 +02:00
parent b125863baf
commit e019767e90
4 changed files with 58 additions and 38 deletions

View file

@ -181,27 +181,25 @@ class ReportsController < ApplicationController
end
def save_pdf_to_inventory_item
report_service = ReportActions::SavePdfToInventoryItem.new(
save_pdf_to_inventory_item = ReportActions::SavePdfToInventoryItem.new(
current_user, current_team, save_PDF_params
)
report_service.call
cell_value = report_service.cell_value
if cell_value.save
if save_pdf_to_inventory_item.save
render json: {
message: I18n.t(
'projects.reports.new.save_PDF_to_inventory_modal.success_flash'
)
}, status: :ok
else
render json: { message: cell_value.errors.full_messages.join },
render json: { message: save_pdf_to_inventory_item.error_messages },
status: :unprocessable_entity
end
rescue ReportActions::RepositoryPermissionError => error
render json: { message: error },
status: :unprocessable_entity
status: :forbidden
rescue Exception => error
render json: { message: error.message },
status: :unprocessable_entity
status: :internal_server_error
end
# Modal for saving the existsing/new report

View file

@ -3,8 +3,6 @@
module ReportActions
class SavePdfToInventoryItem
attr_reader :cell_value
def initialize(user, team, params)
@user = user
@team = team
@ -12,12 +10,18 @@ module ReportActions
load_repository_collaborators
end
def call
file = generate_pdf(@params[:html])
asset = create_new_asset(file)
cell = fetch_repository_cell
def save
file_path = generate_pdf(@params[:html])
asset = create_new_asset(file_path)
cell = fetch_repository_cell
cell.destroy if cell
@cell_value = create_new_cell_value(asset)
@new_cell_value = create_new_cell_value(asset)
@new_cell_value.save
end
def error_messages
return I18n.t('general.error') unless @new_cell_value
@new_cell_value.errors.full_messages.join
end
private
@ -25,13 +29,9 @@ module ReportActions
include Canaid::Helpers::PermissionsHelper
def load_repository_collaborators
@repository = Repository.find_by_id(@params[:repository_id])
@repository_column = RepositoryColumn.find_by_id(
@params[:respository_column_id]
)
@repository_item = RepositoryRow.find_by_id(
@params[:repository_item_id]
)
@repository = load_repository
@repository_column = load_repository_column
@repository_item = load_repository_item
unless can_create_repository_rows?(@user, @repository.team)
raise ReportActions::RepositoryPermissionError,
I18n.t('projects.reports.new.no_permissions')
@ -39,27 +39,24 @@ module ReportActions
end
def generate_pdf(content)
ac = ActionView::Base.new(ActionController::Base.view_paths, {})
ac.extend ReportsHelper # include reports helper methods to view
ac.extend InputSanitizeHelper # include input sanitize methods to view
no_content_label = I18n.t('projects.reports.new.no_content_for_PDF_html')
save_path = Tempfile.open(['report', '.pdf'], Rails.root.join('tmp'))
content = no_content_label if content.blank?
pdf_file = WickedPdf.new.pdf_from_string(
ac.render(template: 'reports/report.pdf.erb',
locals: { content: content }),
file_path = create_temporary_file
pdf_file = WickedPdf.new.pdf_from_string(
action_view_context.render(
template: 'reports/report.pdf.erb',
locals: { content: prepare_pdf_content(content) }
),
header: { right: '[page] of [topage]' },
disable_javascript: true
)
File.open(save_path, 'wb') do |file|
File.open(file_path, 'wb') do |file|
file << pdf_file
end
save_path
file_path
end
def create_new_asset(file)
def create_new_asset(file_path)
asset = Asset.new(
file: file, created_by: @user, last_modified_by: @user, team: @team
file: file_path, created_by: @user, last_modified_by: @user, team: @team
)
asset.post_process_file(@team) if asset.save
asset
@ -82,6 +79,34 @@ module ReportActions
}
)
end
def load_repository
Repository.find_by_id(@params[:repository_id])
end
def load_repository_column
RepositoryColumn.find_by_id(@params[:respository_column_id])
end
def load_repository_item
RepositoryRow.find_by_id(@params[:repository_item_id])
end
def action_view_context
av = ActionView::Base.new(ActionController::Base.view_paths, {})
av.extend ReportsHelper # include reports helper methods to view
av.extend InputSanitizeHelper # include input sanitize methods to view
av
end
def create_temporary_file
Tempfile.open(['report', '.pdf'], Rails.root.join('tmp'))
end
def prepare_pdf_content(content)
return content unless content.blank?
I18n.t('projects.reports.new.no_content_for_PDF_html')
end
end
RepositoryPermissionError = Class.new(StandardError)

View file

@ -18,10 +18,6 @@
id="selectInventory"
class="form-control selectpicker"
data-live-search="true">
<option></option>
<% @available_repositories.each do |repository| %>
<option value="<%= repository.id %>"><%= repository.name %></option>
<% end %>
</select>
<label><%=t 'projects.reports.new.save_PDF_to_inventory_modal.inventory_column' %></label>
<select

View file

@ -1931,6 +1931,7 @@ en:
length_too_short: "Search query is too short (minimum is %{min_length} characters)"
busy: "The server is still processing your request. If you leave this page, the changes will be lost! Are you sure you want to continue?"
no_name: "(no name)"
error: "An error has occurred, please try again later."
api:
core: