-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into assertion-pdf-validation
- Loading branch information
Showing
101 changed files
with
4,070 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# Copyright (c) Universidade Federal Fluminense (UFF). | ||
# This file is part of SAPOS. Please, consult the license terms in the LICENSE file. | ||
|
||
# frozen_string_literal: true | ||
|
||
class AssertionsController < ApplicationController | ||
include SharedPdfConcern | ||
|
||
authorize_resource | ||
before_action :permit_query_params | ||
|
||
def permit_query_params | ||
params[:query_params].permit! unless params[:query_params].nil? | ||
end | ||
|
||
active_scaffold :assertion do |config| | ||
config.action_links.add "simulate", | ||
label: "<i title='#{I18n.t("active_scaffold.assertion.simulate")}' class='fa fa-table'></i>".html_safe, | ||
page: true, | ||
inline: true, | ||
position: :after, | ||
type: :member | ||
|
||
form_columns = [ | ||
:name, :query, :assertion_template | ||
] | ||
|
||
config.create.label = :create_assertion_label | ||
config.list.sorting = { name: "ASC" } | ||
config.list.columns = [:name, :query] | ||
config.columns = form_columns | ||
config.update.columns = form_columns | ||
config.show.columns = form_columns | ||
config.actions.exclude :deleted_records | ||
|
||
config.columns[:query].form_ui = :select | ||
end | ||
|
||
def simulate | ||
@assertion = Assertion.find(params[:id]) | ||
args = @assertion.query.map_params(get_query_params) | ||
result = @assertion.query.execute(args) | ||
@messages = result[:rows] || [] | ||
@query_sql = result[:query] | ||
|
||
# Allow user to simulate with different arguments | ||
# Analyzes derivations and builds new temporary parameters for missing ones | ||
# Also set simulation_value based on arguments | ||
@query_params = @assertion.query.params | ||
|
||
@query_result = result | ||
render action: "simulate" | ||
end | ||
|
||
def assertion_pdf | ||
@assertion = Assertion.find(params[:id]) | ||
args = @assertion.query.map_params(get_query_params) | ||
@assertion.args = args | ||
|
||
respond_to do |format| | ||
format.pdf do | ||
title = I18n.t("pdf_content.assertion.assertion_pdf.filename") | ||
assertion = @assertion.name | ||
filename = "#{title} - #{assertion}.pdf" | ||
send_data render_assertion_pdf(@assertion, filename), | ||
filename: filename, | ||
type: "application/pdf" | ||
end | ||
end | ||
end | ||
|
||
private | ||
def get_query_params | ||
return params[:query_params].to_unsafe_h if params[:query_params].is_a?( | ||
ActionController::Parameters | ||
) | ||
params[:query_params] || {} | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# app/helpers/assertions_helper.rb | ||
module AssertionsHelper | ||
include PdfHelper | ||
include AssertionsPdfHelper | ||
|
||
def query_sql_form_column(record, options) | ||
code_mirror_view_widget( | ||
"sql_query-view-#{record.id}", "text/x-mysql", | ||
(record.query.try(:sql) || ""), true | ||
) | ||
end | ||
|
||
def assertion_template_form_column(record, options) | ||
code_mirror_text_area_widget( | ||
:assertion_template, "record_assertion_template_#{record.id}", "text/html", | ||
options.merge( | ||
value: record.assertion_template || | ||
I18n.t("active_scaffold.notification.body_template_default") | ||
) | ||
) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# frozen_string_literal: true | ||
|
||
# app/helpers/assertions_pdf_helper.rb | ||
module AssertionsPdfHelper | ||
include AssertionHelperConcern | ||
|
||
def format_text(bindings, template) | ||
formatter = ErbFormatter.new(bindings) | ||
formatter.format(template) | ||
end | ||
|
||
def find_unique_columns(columns, rows) | ||
columns.select do |column| | ||
rows.all? { |row| row[columns.index(column)] == rows.first[columns.index(column)] } | ||
end | ||
end | ||
|
||
def assertion_box_text_print(pdf, template, bindings, box_width, box_height) | ||
pdf.move_down 30 | ||
|
||
text = format_text(bindings, template) | ||
|
||
lines = pdf.text_box text, at: [(pdf.bounds.width - box_width) / 2, pdf.cursor], width: box_width, height: box_height, align: :justify, inline_format: true, dry_run: true | ||
|
||
while lines.size > 0 | ||
not_printed_text_length = lines.map { |line| line[:text].length }.sum | ||
text = text[-not_printed_text_length..-1] | ||
|
||
pdf.start_new_page | ||
lines = pdf.text_box text, at: [(pdf.bounds.width - box_width) / 2, pdf.cursor], width: box_width, height: box_height, align: :justify, inline_format: true, dry_run: true | ||
end | ||
end | ||
|
||
def assertion_table(pdf, options = {}) | ||
assertion = options[:assertion] | ||
args = assertion.args | ||
results = get_query_results(assertion, args) | ||
template = assertion.assertion_template | ||
rows = results[:rows] | ||
columns = results[:columns] | ||
|
||
if results[:rows].size == 1 | ||
bindings = {}.merge(Hash[columns.zip(rows.first)]) | ||
else | ||
unique_columns = find_unique_columns(columns, rows) | ||
bindings = { | ||
rows: rows, | ||
columns: columns | ||
}.merge(Hash[unique_columns.zip(rows.first.values_at(*unique_columns.map { |col| columns.index(col) }))]) | ||
end | ||
|
||
box_width = 500 | ||
box_height = 560 | ||
|
||
assertion_box_text_print(pdf, template, bindings, box_width, box_height) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# app/helpers/concerns/assertion_helper_concern.rb | ||
module AssertionHelperConcern | ||
def get_query_results(assertion, args) | ||
assertion.query.execute(args) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.