Class: DynatableBuilder::ViewHelpers::Builder

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers::TagHelper
Defined in:
lib/dynatable_builder/view_helpers.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, opts = {}) ⇒ Builder

Returns a new instance of Builder.



9
10
11
12
13
# File 'lib/dynatable_builder/view_helpers.rb', line 9

def initialize(view, opts = {})
  @view = view
  @columns = []
  @table_opts = opts
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



65
66
67
68
69
70
71
# File 'lib/dynatable_builder/view_helpers.rb', line 65

def method_missing(method, *args, &block)
  if view.respond_to?(method)
    view.send(method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



7
8
9
# File 'lib/dynatable_builder/view_helpers.rb', line 7

def columns
  @columns
end

#table_optsObject (readonly)

Returns the value of attribute table_opts.



7
8
9
# File 'lib/dynatable_builder/view_helpers.rb', line 7

def table_opts
  @table_opts
end

#viewObject (readonly)

Returns the value of attribute view.



7
8
9
# File 'lib/dynatable_builder/view_helpers.rb', line 7

def view
  @view
end

Instance Method Details

#column(title, opts = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dynatable_builder/view_helpers.rb', line 15

def column(title, opts = {})
  col_opts, dyna_opts = { title: title }, {}
  col_opts.merge! opts.except(:model, :attribute, :sort)

  dyna_opts[:dynatable_column] = opts[:attribute] || title.underscore.tr(' ', '_')

  if opts[:sort] == false
    dyna_opts[:dynatable_no_sort] = true
  else
    dyna_opts[:dynatable_sorts] = opts[:sort] || dyna_opts[:dynatable_column].dup

    if model_name = opts[:model] || table_opts[:model]
      dyna_opts[:dynatable_sorts].prepend "#{table_name(model_name)}."
    end

    if direction = opts[:default_sort]
      table_opts[:sort]  = dyna_opts[:dynatable_sorts]
      table_opts[:order] = direction
    end
  end

  columns << col_opts.merge(data: dyna_opts)
  nil
end

#evaluate(&block) ⇒ Object



40
41
42
# File 'lib/dynatable_builder/view_helpers.rb', line 40

def evaluate(&block)
  with_output_buffer { instance_eval(&block) }
end

#renderObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dynatable_builder/view_helpers.rb', line 44

def render
  # Without an id, multiple dynatables will conflict
  table_opts[:id] ||= "dynatable#{rand(1..100)}"
  table_opts[:class] = [table_opts[:class], 'dynatable'].compact.join(' ')
  (table_opts[:data] ||= {}).merge! table_opts.slice(:source, :sort, :order)

  (:table, table_opts.except(:model, :source, :sort, :order)) do
    (:thead) do
      columns.map do |column_opts|
        (:th, column_opts[:title], column_opts.except(:title))
      end.join('').html_safe
    end
  end
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/dynatable_builder/view_helpers.rb', line 59

def respond_to?(method, include_private = false)
  view.respond_to?(method, include_private) || super
end