Class: DynatableBuilder::Builder
- Inherits:
-
Object
- Object
- DynatableBuilder::Builder
- Defined in:
- lib/dynatable_builder/builder.rb
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#defined_scope ⇒ Object
readonly
Returns the value of attribute defined_scope.
-
#includes ⇒ Object
readonly
Returns the value of attribute includes.
-
#joins ⇒ Object
readonly
Returns the value of attribute joins.
-
#search ⇒ Object
readonly
Returns the value of attribute search.
-
#view ⇒ Object
readonly
Returns the value of attribute view.
Instance Method Summary collapse
- #add_helper(helper = nil, &block) ⇒ Object
- #as_json ⇒ Object
- #collection ⇒ Object
-
#initialize(view, defined_scope, opts = {}) ⇒ Builder
constructor
A new instance of Builder.
- #respond_to?(method, include_private = false) ⇒ Boolean
- #to_builder ⇒ Object
- #to_json ⇒ Object
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
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
3 4 5 |
# File 'lib/dynatable_builder/builder.rb', line 3 def columns @columns end |
#defined_scope ⇒ Object (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 |
#includes ⇒ Object (readonly)
Returns the value of attribute includes.
3 4 5 |
# File 'lib/dynatable_builder/builder.rb', line 3 def includes @includes end |
#joins ⇒ Object (readonly)
Returns the value of attribute joins.
3 4 5 |
# File 'lib/dynatable_builder/builder.rb', line 3 def joins @joins end |
#search ⇒ Object (readonly)
Returns the value of attribute search.
3 4 5 |
# File 'lib/dynatable_builder/builder.rb', line 3 def search @search end |
#view ⇒ Object (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_json ⇒ Object
42 43 44 |
# File 'lib/dynatable_builder/builder.rb', line 42 def as_json to_builder.attributes! end |
#collection ⇒ Object
17 18 19 |
# File 'lib/dynatable_builder/builder.rb', line 17 def collection @collection ||= fetch_collection end |
#respond_to?(method, include_private = false) ⇒ 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_builder ⇒ Object
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_json ⇒ Object
46 47 48 |
# File 'lib/dynatable_builder/builder.rb', line 46 def to_json to_builder.target! end |