Skip to content

Commit

Permalink
Move logic to determine whether a col is hidden to a method
Browse files Browse the repository at this point in the history
  • Loading branch information
gtanzillo committed Mar 19, 2018
1 parent 7b22d8f commit cc9fd3a
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 2 deletions.
10 changes: 10 additions & 0 deletions app/models/miq_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,16 @@ def sort_col
sortby ? col_order.index(sortby.first) : 0
end

def column_is_hidden?(col)
return false unless col_options

@hidden_cols ||= col_options.keys.each_with_object([]) do |c, a|
a << c if col_options[c][:hidden]
end

@hidden_cols.include?(col.to_s)
end

def self.from_hash(h)
new(h)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/miq_report/generator/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def build_html_rows(clickable_rows = false)
row = 1 - row

col_order.each_with_index do |c, c_idx|
next if col_options[c].try(:has_key?, :hidden) && col_options[c][:hidden]
next if column_is_hidden?(c)

build_html_col(output, c, self.col_formats[c_idx], d.data)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/vmdb/global_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def report_build_html_table(report, table_body)
unless report.headers.nil?
report.headers.each_with_index do |h, i|
col = report.col_order[i]
next if report.col_options[col].try(:has_key?, :hidden) && report.col_options[col][:hidden]
next if report.column_is_hidden?(col)

html << "<th>" << CGI.escapeHTML(_(h.to_s)) << "</th>"
end
Expand Down
20 changes: 20 additions & 0 deletions spec/models/miq_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,26 @@ def generate_html_row(is_even, tenant_name, formatted_values)
end
end

describe "#column_is_hidden?" do
let(:report) {
MiqReport.new(
:name => "VMs",
:title => "Virtual Machines",
:db => "Vm",
:cols => %w(name guid hostname ems_ref vendor),
:col_order => %w(name hostname vendor guid emf_ref),
:headers => %w(Name Host Vendor Guid EMS),
:col_options => {"guid" => {:hidden => true}, "ems_ref" => {:hidden => true}}
)
}

it "detects hidden columns defined in #col_options" do
expect(report.column_is_hidden?(:guid)).to be_truthy
expect(report.column_is_hidden?(:ems_ref)).to be_truthy
expect(report.column_is_hidden?(:vendor)).to be_falsey
end
end

context "chargeback reports" do
let(:hourly_rate) { 0.01 }
let(:hourly_variable_tier_rate) { {:variable_rate => hourly_rate.to_s} }
Expand Down

0 comments on commit cc9fd3a

Please sign in to comment.