Skip to content

Latest commit

 

History

History
76 lines (51 loc) · 1.72 KB

define-report-table.md

File metadata and controls

76 lines (51 loc) · 1.72 KB

Define Table

Table is used for config report's export data format.

A table is composed of:

  • A collection: include rows;
  • A row: include columns;
  • A column: related to a model object, directly or indirectly;

So, just config them.

step-1: Define a subclass inherit from OneReport::Base

# we usually place the file in app/reports/example_table
class ExampleTable < OneReport::Base
end

step-2: Config Collection

  • Before config collection, you should defined a scope for the Model first
# consider a User model
# need define some scope before used it;
class User
  scope :one_report_scope, ->(size) { limit(size) }
end
  • call collect method for define collection, the params should be a lambda, and the collection should response to method each
class ExampleTable < OneReport::Base

  def config(size)

    # the collection should respond to method `each`
    collect -> { User.one_report_scope(size) }

  end

end

step-3: Config Each Column

use for each collection element's method, to pass a method name or a lambda the order is important, all columns order by It's defined order

class ExampleTable < OneReport::Base

  def config(size)

    # the collection should respond to method `each`
    collect -> { User.one_report_scope(size) }
    
    # with default header and default field method
    # default header use 'titleize' method to format
    # default field method equal column's name
    column :name

    # with assigned header and default field method
    column :name, header: 'My name'

    # with assigned header and assigned field method
    column :name, header: 'My name', field: -> { name }
  end

end

Optional, config headers for the table