Module: Tabulatr::Data::DSL

Defined in:
lib/tabulatr/data/dsl.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. ++

Instance Method Summary collapse

Instance Method Details

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



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/tabulatr/data/dsl.rb', line 66

def actions(opts = {}, &block)
  raise 'give a block to action column' unless block_given?
  @table_columns ||= []
  table_column = Tabulatr::Renderer::Action.from(
    name: '_actions',
    table_name: main_class.table_name.to_sym,
    klass: @base,
    col_options: Tabulatr::ParamsBuilder.new({header: (opts[:header] || ''), filter: false, sortable: false}),
    output: block)
  @table_columns << table_column
end

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



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tabulatr/data/dsl.rb', line 49

def association(assoc, name, opts = {}, &block)
  @table_columns ||= []
  assoc_klass = main_class.reflect_on_association(assoc.to_sym)
  tname = assoc_klass.try(:quoted_table_name) || assoc_klass.try(:klass).try(:quoted_table_name)
  sql_options = determine_sql(opts, tname, name)
  opts = {
      sort_sql: sql_options[:sort_sql],
      filter_sql: sql_options[:filter_sql]}.merge(opts)
  table_column = Tabulatr::Renderer::Association.from(
      name: name,
      klass: assoc_klass.try(:klass),
      col_options: Tabulatr::ParamsBuilder.new(opts),
      table_name: assoc,
      output: block_given? ? block : ->(record){a=record.send(assoc); a.try(:read_attribute, name) || a.try(name)})
  @table_columns << table_column
end

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/tabulatr/data/dsl.rb', line 78

def buttons(opts = {}, &block)
  raise 'give a block to action column' unless block_given?
  @table_columns ||= []
  output = ->(r) {
    tdbb = Tabulatr::Data::ButtonBuilder.new
    self.instance_exec tdbb, r, &block
    self.controller.render_to_string partial: '/tabulatr/tabulatr_buttons', locals: {buttons: tdbb.val}, formats: [:html]
  }
  opts = {header: opts[:header] || '', filter: false, sortable: false}.merge(opts)
  table_column = Tabulatr::Renderer::Buttons.from(
    name: (opts[:name] || '_buttons'),
    table_name: main_class.table_name.to_sym,
    klass: @base,
    col_options: Tabulatr::ParamsBuilder.new(opts),
    output: output)
  @table_columns << table_column
end

#checkbox(opts = {}) ⇒ Object



96
97
98
99
100
101
# File 'lib/tabulatr/data/dsl.rb', line 96

def checkbox(opts = {})
  @table_columns ||= []
  box = Tabulatr::Renderer::Checkbox.from(klass: @base,
          col_options: Tabulatr::ParamsBuilder.new(opts.merge(filter: false, sortable: false)))
  @table_columns << box
end

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



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/tabulatr/data/dsl.rb', line 31

def column(name, opts = {}, &block)
  @table_columns ||= []
  sql_options = determine_sql(opts, main_class.quoted_table_name, name)
  opts = {
          sort_sql: sql_options[:sort_sql],
          filter: true,
          default_filter: nil,
          sortable: true,
          filter_sql: sql_options[:filter_sql]}.merge(opts)
  table_column = Tabulatr::Renderer::Column.from(
      name: name,
      klass: @base,
      col_options: Tabulatr::ParamsBuilder.new(opts),
      table_name: main_class.table_name.to_sym,
      output: block_given? ? block : ->(record){record.send(name)})
  @table_columns << table_column
end

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



113
114
115
116
# File 'lib/tabulatr/data/dsl.rb', line 113

def filter(name, partial: nil, &block)
  @filters ||= []
  @filters << Tabulatr::Renderer::Filter.new(name, partial: partial, &block)
end

#main_classObject



26
27
28
29
# File 'lib/tabulatr/data/dsl.rb', line 26

def main_class
  target_class_name # to get auto setting @target_class
  @target_class
end

#row(&block) ⇒ Object



108
109
110
111
# File 'lib/tabulatr/data/dsl.rb', line 108

def row &block
  raise 'Please pass a block to row if you want to use it.' unless block_given?
  @row = block
end

#search(*args, &block) ⇒ Object



103
104
105
106
# File 'lib/tabulatr/data/dsl.rb', line 103

def search(*args, &block)
  raise "either column or block" if args.present? && block_given?
  @search = args.presence || block
end