Class: Tabulatr::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/tabulatr/renderer/renderer.rb,
lib/tabulatr/renderer/columns_from_block.rb

Overview

– Copyright © 2010-2014 Peter Horn & Florian Thomas, metaminded UG

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Defined Under Namespace

Classes: Action, Association, Buttons, Checkbox, Column, ColumnsFromBlock, Filter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, view, filter: Tabulatr.filter, search: Tabulatr.search, paginate: Tabulatr.paginate, pagesize: Tabulatr.pagesize, sortable: Tabulatr.sortable, batch_actions: Tabulatr.batch_actions, footer_content: Tabulatr.footer_content, path: Tabulatr.path, order_by: Tabulatr.order_by, html_class: Tabulatr.html_class, pagination_position: Tabulatr.pagination_position, counter_position: Tabulatr.counter_position, persistent: Tabulatr.persistent) ⇒ Renderer

Returns a new instance of Renderer.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tabulatr/renderer/renderer.rb', line 26

def initialize(klass, view,
    filter:              Tabulatr.filter,
    search:              Tabulatr.search,
    paginate:            Tabulatr.paginate,
    pagesize:            Tabulatr.pagesize,
    sortable:            Tabulatr.sortable,
    batch_actions:       Tabulatr.batch_actions,
    footer_content:      Tabulatr.footer_content,
    path:                Tabulatr.path,
    order_by:            Tabulatr.order_by,
    html_class:          Tabulatr.html_class,
    pagination_position: Tabulatr.pagination_position,
    counter_position:    Tabulatr.counter_position,
    persistent:          Tabulatr.persistent)
  @klass = klass
  @view = view
  @table_options = {
    filter: filter,
    search: search,
    paginate: paginate,
    pagesize: pagesize,
    sortable: sortable,
    batch_actions: batch_actions,
    footer_content: footer_content,
    path: path,
    order_by: order_by,
    html_class: 'table tabulatr_table '.concat(html_class),
    pagination_position: pagination_position,
    counter_position: counter_position,
    persistent: paginate ? persistent : false
  }
  @classname = @klass.name.underscore
end

Class Method Details

.build_static_table(records, view, toptions = {}, &block) ⇒ Object



92
93
94
95
96
# File 'lib/tabulatr/renderer/renderer.rb', line 92

def self.build_static_table(records, view, toptions={}, &block)
  return '' unless records.present?
  klass = records.first.class
  new(klass, view, toptions).build_static_table(records, &block)
end

.build_table(klass, view, toptions = {}, columns, filters, tabulatr_data_class, &block) ⇒ Object



98
99
100
# File 'lib/tabulatr/renderer/renderer.rb', line 98

def self.build_table(klass, view, toptions={}, columns, filters, tabulatr_data_class, &block)
  new(klass, view, toptions).build_table(columns, filters, tabulatr_data_class, &block)
end

Instance Method Details

#build_static_table(records, &block) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tabulatr/renderer/renderer.rb', line 75

def build_static_table(records, &block)
  @columns = ColumnsFromBlock.process(@klass, &block).columns

  @view.render(partial: '/tabulatr/tabulatr_static_table', locals: {
    columns: @columns,
    table_options: @table_options,
    klass: @klass,
    classname: @classname,
    records: records
  })
end

#build_table(columns, filters, tabulatr_data_class, &block) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/tabulatr/renderer/renderer.rb', line 60

def build_table(columns, filters, tabulatr_data_class, &block)
  tdc = get_data_class(tabulatr_data_class)
  set_columns_and_filters(tdc, columns, filters, &block)
  @view.render(partial: '/tabulatr/tabulatr_table', locals: {
    columns: @columns,
    table_options: @table_options,
    klass: @klass,
    classname: @classname,
    tabulatr_data: tdc,
    table_id: generate_id,
    formatted_name: Tabulatr::Utility.formatted_name(@klass.name),
    filter: tdc.filters || []
  })
end

#generate_idObject



87
88
89
90
# File 'lib/tabulatr/renderer/renderer.rb', line 87

def generate_id
  controller_path = @view.controller.class.name.underscore.gsub('/', '-')
  "#{Tabulatr::Utility.formatted_name(@klass.name)}_table_#{controller_path}_#{@view.controller.action_name}_#{@view.instance_variable_get(:@_tabulatr_table_index)}"
end