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



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

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



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

def association(assoc, name, opts = {}, &block)
  @table_columns ||= []
  assoc_klass = main_class.reflect_on_association(assoc.to_sym)
  sql_options = determine_sql(opts, assoc_klass.try(:quoted_table_name), 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



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

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



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

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
# 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,
          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



111
112
113
114
# File 'lib/tabulatr/data/dsl.rb', line 111

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



106
107
108
109
# File 'lib/tabulatr/data/dsl.rb', line 106

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



101
102
103
104
# File 'lib/tabulatr/data/dsl.rb', line 101

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