Skip to content

Commit

Permalink
Merge c46a3bd into 2683806
Browse files Browse the repository at this point in the history
  • Loading branch information
jezstephens committed Feb 3, 2015
2 parents 2683806 + c46a3bd commit 2c7d8db
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
21 changes: 21 additions & 0 deletions README.md
Expand Up @@ -137,6 +137,27 @@ class MoneyLaunderingReport < Dossier::Report
end
```

## Hidden Columns

You may override `display_column?` in your report class in order to hide columns from the formatted results. For instance, you might select an employee's ID and name in order to generate a link from their name to their profile page, without actually displaying the ID value itself:

```ruby
class EmployeeReport < Dossier::Report
# ...

def display_column?(name)
name != 'id'
end

def format_name(value, row)
url = formatter.url_formatter.url_helpers.employee_path(row['id'])
formatter.url_formatter.link_to(value, url)
end
end
```

By default, all selected columns are displayed.

## Report Options and Footers

You may want to specify parameters for a report: which columns to show, a range of dates, etc. Dossier supports this via URL parameters, anything in `params[:options]` will be passed into your report's `initialize` method and made available via the `options` reader.
Expand Down
4 changes: 4 additions & 0 deletions lib/dossier/report.rb
Expand Up @@ -59,6 +59,10 @@ def format_column(column, value)
value
end

def display_column?(column)
true
end

def dossier_client
Dossier.client
end
Expand Down
13 changes: 10 additions & 3 deletions lib/dossier/result.rb
Expand Up @@ -52,7 +52,11 @@ def each
class Formatted < Result

def headers
@formatted_headers ||= raw_headers.map { |h| report.format_header(h) }
@formatted_headers ||= raw_headers.select { |h|
report.display_column?(h)
}.map { |h|
report.format_header(h)
}
end

def each
Expand All @@ -64,7 +68,10 @@ def format(row)
raise ArgumentError.new("#{row.inspect} must be a kind of Enumerable")
end

row.each_with_index.map do |value, i|
row.each_with_index.select { |value, i|
column = raw_headers.at(i)
report.display_column?(column)
}.map { |value, i|
column = raw_headers.at(i)
method = "format_#{column}"

Expand All @@ -76,7 +83,7 @@ def format(row)
else
report.format_column(column, value)
end
end
}
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/dummy/app/reports/employee_report.rb
Expand Up @@ -55,6 +55,10 @@ def names
@names ||= options.fetch(:names) { [] }.dup
end

def display_column?(name)
name != 'id'
end

def format_salary(amount, row)
return "Who's Asking?" if row[:division] == "Corporate Malfeasance"
formatter.number_to_currency(amount)
Expand Down
7 changes: 7 additions & 0 deletions spec/features/employee_spec.rb
Expand Up @@ -12,6 +12,13 @@
end
end

context "hiding columns" do
it "does not display hidden columns" do
visit '/reports/employee'
expect(page).to_not have_content('Id')
end
end

context "when a custom view exists for the report" do

it "uses the custom view" do
Expand Down

0 comments on commit 2c7d8db

Please sign in to comment.