Class: DynatableBuilder::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/dynatable_builder/builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Builder.



6
7
8
9
10
11
12
13
14
15
# File 'lib/dynatable_builder/builder.rb', line 6

def initialize(view, defined_scope, opts = {})
  @view = view
  @defined_scope = defined_scope

  (opts[:helpers] || []).each { |h| add_helper(h) }

  [:columns, :includes, :joins, :search].each do |arg|
    instance_variable_set "@#{arg}", opts[arg]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



96
97
98
99
100
101
102
# File 'lib/dynatable_builder/builder.rb', line 96

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.



3
4
5
# File 'lib/dynatable_builder/builder.rb', line 3

def columns
  @columns
end

#defined_scopeObject (readonly)

Returns the value of attribute defined_scope.



3
4
5
# File 'lib/dynatable_builder/builder.rb', line 3

def defined_scope
  @defined_scope
end

#includesObject (readonly)

Returns the value of attribute includes.



3
4
5
# File 'lib/dynatable_builder/builder.rb', line 3

def includes
  @includes
end

#joinsObject (readonly)

Returns the value of attribute joins.



3
4
5
# File 'lib/dynatable_builder/builder.rb', line 3

def joins
  @joins
end

#searchObject (readonly)

Returns the value of attribute search.



3
4
5
# File 'lib/dynatable_builder/builder.rb', line 3

def search
  @search
end

#viewObject (readonly)

Returns the value of attribute view.



3
4
5
# File 'lib/dynatable_builder/builder.rb', line 3

def view
  @view
end

Instance Method Details

#add_helper(helper = nil, &block) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/dynatable_builder/builder.rb', line 50

def add_helper(helper = nil, &block)
  helper = block if block_given?
  if helper.respond_to?(:call)
    extend Module.new(&helper)
  else
    extend helper
  end
end

#as_jsonObject



42
43
44
# File 'lib/dynatable_builder/builder.rb', line 42

def as_json
  to_builder.attributes!
end

#collectionObject



17
18
19
# File 'lib/dynatable_builder/builder.rb', line 17

def collection
  @collection ||= fetch_collection
end

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

Returns:

  • (Boolean)


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

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

#to_builderObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dynatable_builder/builder.rb', line 21

def to_builder
  Jbuilder.new do |json|
    json.records do
      if columns
        json.array! collection do |object|
          if columns.respond_to?(:call)
            generate_object_nodes(json, object)
          else
            json.extract!(object, *columns)
          end
        end
      else
        json.array! collection
      end
    end

    json.queryRecordCount total_count
    json.totalRecordCount total_count
  end
end

#to_jsonObject



46
47
48
# File 'lib/dynatable_builder/builder.rb', line 46

def to_json
  to_builder.target!
end