Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active August 29, 2015 14:11
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/0e185e38e246cb929176 to your computer and use it in GitHub Desktop.
Save krisleech/0e185e38e246cb929176 to your computer and use it in GitHub Desktop.
DataGrid

DataGrid: build cells, rows and columns

Cells referenced by x,y (like spreadsheet)

Features:

  • Cells with conditional formatting, e.g. cell is red is value over threshold
  • Cells with formula, e.g. SUM(A5-A6)

Table has default cell attributes, e.g. height/width

template_cells = []
template_cells << BasicCell.new(name: ‘Name’, key: ‘name’)
template_cells << BasicCell.new(name: ‘Ref’, key: ‘ref’)
template_cells << DateCell.new(name: ‘start_date’, format: %d/%m/%Y’, key: ‘start_date’)

studies.each do |study|
  table.rows.add |row|
    template_cells.each do |template_cell|
      cell = template_cell.dup
      cell.value = study.send(cell.key)
      row.cells << cell
    end
  end
end

build a table by creating some template cells (which represent a row). For each study duplicate the cells and populate them with data from the study.

feed table in to a HAML template.

Assosiations

BasicCell.new(key: 'organisation.name')

Multiple formattings

e.g. Display a formatted date which is a link

LinkedCell.new(cell: DateCell.new(key: started_on), url: '...')

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