Class: Tabulatr::Renderer::ColumnsFromBlock

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, table_data_object) ⇒ ColumnsFromBlock

Returns a new instance of ColumnsFromBlock.



28
29
30
31
32
33
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 28

def initialize(klass, table_data_object)
  @klass = klass
  @table_data = table_data_object
  @columns ||= []
  @filters ||= []
end

Instance Attribute Details

#columnsObject

Returns the value of attribute columns.



26
27
28
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 26

def columns
  @columns
end

#filtersObject

Returns the value of attribute filters.



26
27
28
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 26

def filters
  @filters
end

#klassObject

Returns the value of attribute klass.



26
27
28
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 26

def klass
  @klass
end

#table_dataObject

Returns the value of attribute table_data.



26
27
28
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 26

def table_data
  @table_data
end

Class Method Details

.process(klass, table_data_object = nil) {|i| ... } ⇒ Object

Yields:

  • (i)


79
80
81
82
83
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 79

def self.process(klass, table_data_object = nil, &block)
  i = self.new(klass, table_data_object)
  yield(i)
  i
end

Instance Method Details

#action(opts = {}, &block) ⇒ Object



57
58
59
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 57

def action(opts={}, &block)
  @columns << Action.from(klass: klass, col_options: Tabulatr::ParamsBuilder.new(opts.merge(filter: false, sortable: false)), &block)
end

#association(table_name, name, opts = {}, &block) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 43

def association(table_name, name, opts={}, &block)
  if table_data
    @columns << fetch_column_from_table_data(table_name, name, opts, &block)
  else
    assoc_klass = klass.reflect_on_association(table_name.to_sym)
    @columns << Association.from(klass: assoc_klass.try(:klass),
      name: name, table_name: table_name, col_options: Tabulatr::ParamsBuilder.new(opts), &block)
  end
end

#buttons(opts = {}, &block) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 61

def buttons(opts={}, &block)
  output = ->(r) {
    bb = self.instance_exec Tabulatr::Data::ButtonBuilder.new, r, &block
    self.controller.render_to_string partial: '/tabulatr/tabulatr_buttons', locals: {buttons: bb}, formats: [:html]
  }
  opts = {filter: false, sortable: false}.merge(opts)
  @columns << Buttons.from(klass: klass, col_options: Tabulatr::ParamsBuilder.new(opts), output: output, &block)
end

#checkbox(opts = {}) ⇒ Object



53
54
55
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 53

def checkbox(opts={})
  @columns << Checkbox.from(klass: klass, col_options: Tabulatr::ParamsBuilder.new(opts.merge(filter: false, sortable: false)))
end

#column(name, opts = {}, &block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 35

def column(name, opts={}, &block)
  if table_data
    @columns << fetch_column_from_table_data(klass.table_name.to_sym, name, opts, &block)
  else
    @columns << Column.from(klass: klass, table_name: klass.table_name.to_sym, name: name, col_options: Tabulatr::ParamsBuilder.new(opts), &block)
  end
end

#filter(name, partial: nil, &block) ⇒ Object



70
71
72
73
74
75
76
77
# File 'lib/tabulatr/renderer/columns_from_block.rb', line 70

def filter(name, partial: nil, &block)
  if table_data
    found_filter = fetch_filter_from_table_data(name)
    @filters << found_filter if found_filter.present?
  else
    @filters << Tabulatr::Renderer::Filter.new(name, partial: partial, &block)
  end
end