Skip to content

Commit

Permalink
Merge pull request #18426 from lpichler/extract_format_column_report_…
Browse files Browse the repository at this point in the history
…generator

Extract method format_column from report generator
  • Loading branch information
gtanzillo committed Feb 19, 2019
2 parents df26bba + 3cebe9a commit 574bb81
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
18 changes: 13 additions & 5 deletions app/models/miq_report/generator/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,27 @@ def build_html_rows(clickable_rows = false)
html_rows
end

def format_column(col_name, row_data, time_zone, col_format = nil)
if col_name == 'resource_type'
ui_lookup(:model => row_data[col_name]) # Lookup models in resource_type col
elsif db == 'Tenant' && TenantQuota.can_format_field?(col_name, row_data['tenant_quotas.name'])
CGI.escapeHTML(TenantQuota.format_quota_value(col_name, row_data[col_name], row_data['tenant_quotas.name']))
elsif ['<compare>', '<drift>'].include?(db.to_s)
CGI.escapeHTML(row_data[col_name].to_s)
else
CGI.escapeHTML(format(col_name.split("__").first, row_data[col_name], :format => col_format || :_default_, :tz => time_zone)) # rubocop:disable Style/FormatString
end
end

def build_html_col(output, col_name, col_format, row_data, time_zone)
style = get_style_class(col_name, row_data, time_zone)
style_class = !style.nil? ? " class='#{style}'" : nil
if col_name == 'resource_type'
output << "<td#{style_class}>"
output << ui_lookup(:model => row_data[col_name]) # Lookup models in resource_type col
elsif db == 'Tenant' && TenantQuota.can_format_field?(col_name, row_data['tenant_quotas.name'])
output << "<td#{style_class} " + 'style="text-align:right">'
output << CGI.escapeHTML(TenantQuota.format_quota_value(col_name, row_data[col_name], row_data['tenant_quotas.name']))
elsif ['<compare>', '<drift>'].include?(db.to_s)
output << "<td#{style_class}>"
output << CGI.escapeHTML(row_data[col_name].to_s)
else
if row_data[col_name].kind_of?(Time)
output << "<td#{style_class} " + 'style="text-align:center">'
Expand All @@ -86,9 +95,8 @@ def build_html_col(output, col_name, col_format, row_data, time_zone)
else
output << "<td#{style_class}>"
end
output << CGI.escapeHTML(format(col_name.split("__").first, row_data[col_name],
:format => col_format || :_default_, :tz => time_zone))
end
output << format_column(col_name, row_data, time_zone, col_format)
output << '</td>'
end

Expand Down
48 changes: 48 additions & 0 deletions spec/models/miq_report/generator/html_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
describe MiqReport do
let(:miq_report) { FactoryBot.create(:miq_report) }

before do
EvmSpecHelper.local_miq_server
end

describe "#format_column" do
it "formats resource_type column" do
expect(miq_report.format_column("resource_type", {"resource_type" => "Vm"}, Time.zone)).to eq("VM and Instance")
end

it "formats column with base report Tenant" do
miq_report.db = "Tenant"
expect(miq_report.format_column("tenant_quotas.used", {"tenant_quotas.used" => 786, "tenant_quotas.name" => "cpu_allocated"}, Time.zone)).to eq("786 Count")
end

it "formats column with format method" do
expect(miq_report.format_column("name", {"name" => "VM1"}, Time.zone)).to eq("VM1")
end
end

describe "#build_html_col" do
it "renders html for resource_type column" do
expect(miq_report.build_html_col([], "resource_type", nil, {"resource_type" => "Vm"}, Time.zone)).to match_array(["<td>", "VM and Instance", "</td>"])
end

it "renders html for with base report Tenant" do
miq_report.db = "Tenant"
expect(miq_report.build_html_col([], "tenant_quotas.used", nil, {"tenant_quotas.used" => 786, "tenant_quotas.name" => "cpu_allocated"}, Time.zone)).to match_array(["<td style=\"text-align:right\">", "786 Count", "</td>"])
end

it "renders html for string column" do
expect(miq_report.build_html_col([], "name", nil, {"name" => "VM1"}, Time.zone)).to match_array(["<td>", "VM1", "</td>"])
end

it "renders html for time column" do
string_time_now = "02/15/19 14:32:23 UTC"
time_now = Time.zone.parse(string_time_now).utc
expect(miq_report.build_html_col([], "time", nil, {"time" => time_now}, Time.zone)).to match_array(["<td style=\"text-align:center\">", string_time_now, "</td>"])
end

it "renders html for integer and float column" do
expect(miq_report.build_html_col([], "cpus", nil, {"cpus" => 10}, Time.zone)).to match_array(["<td style=\"text-align:right\">", "10", "</td>"])
expect(miq_report.build_html_col([], "average_speed", nil, {"average_speed" => 20.44}, Time.zone)).to match_array(["<td style=\"text-align:right\">", "20.44", "</td>"])
end
end
end

0 comments on commit 574bb81

Please sign in to comment.