Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created February 3, 2016 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/4bd633a941197d18166c to your computer and use it in GitHub Desktop.
Save krisleech/4bd633a941197d18166c to your computer and use it in GitHub Desktop.

Reporting

The aim of this gem is to standardise reporting. It also allow registration of reports and have them appear in the UI.

It will be used for reports which can not otherwise be produced with the data grid.

To register a report:

Reporting.register(name:   'BRU Recruitment Report', 
                   report: Recruitment::Reports::BruRecruitment.new,
                   view:   Recruitment::Engine.root.join('app/views/reports/bru_recruitment'))

Reports should follow provide this interface as a minimum:

class MyReport
  def call
  end
end

This report, since it has no user input will be have the create view rendered.

Reports should be stateless so they same report can be called many times with different inputs.

Providing user input to the report:

class MyReport
  class Form
    include Reporting::Form
    attribute :start_date, Date
    validate :start_date, presence: true
  end
end

class MyReport
  def call(form)
  end
end

Since the above report requires user input the new view will be rendered first, if the form object is valid the report is generated and the create action rendered.

Automatic CSV generation:

You can have your report automatically generate CSV if the dataset returns a Hash. The Hash keys will become the columns. To restrict the columns provide a method as such:

class MyReport
  def csv_columns
    %w(...)
  end
end

Multiple views

Normally the create view is used to render a report, if you wish to provide multiple ways of rendering the same report add an attribute to the form to allow the user to select their view and render a different partial from the create action based on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment