Class: Fancygrid::Node
- Inherits:
-
Object
- Object
- Fancygrid::Node
- Defined in:
- lib/fancygrid/node.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Array of child nodes.
-
#name ⇒ Object
readonly
The name of this node.
-
#parent ⇒ Object
readonly
The paent node.
-
#resource_class ⇒ Object
readonly
The corresponding resource class.
-
#root ⇒ Object
readonly
The fancygrid root node.
-
#table_name ⇒ Object
readonly
The table name of the resource class.
Instance Method Summary collapse
-
#attributes(*names) ⇒ Object
Creates and adds a column for each given parameter.
-
#collect_columns(collection) ⇒ Object
Collects all columns into the given collection.
-
#column(name, options = {}) ⇒ Object
Creates and adds a column to this node.
-
#columns(*names) ⇒ Object
Creates and adds a column for each given parameter.
-
#columns_for(name, options = {}) {|node| ... } ⇒ Object
Creates and yields a child node.
-
#initialize(parent, name, options = {}) ⇒ Node
constructor
Initializes the node.
-
#name_chain ⇒ Object
Gets a dot separated string of names of all parent nodes including own.
-
#root? ⇒ Boolean
Returns true if this node is the root node of the tree.
Constructor Details
#initialize(parent, name, options = {}) ⇒ Node
Initializes the node.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/fancygrid/node.rb', line 25 def initialize(parent, name, = {}) raise ArgumentError, "expected parent to be a Node" if !parent.nil? && !parent.is_a?(Fancygrid::Node) raise ArgumentError, "name must not be blank" if name.blank? @parent = parent @name = name @children = [] @root = self.get_root @resource_class = .fetch(:class) do self.name.to_s.classify.constantize end @table_name = .fetch(:table_name) do if self.resource_class.respond_to?(:table_name) self.resource_class.table_name else self.resource_class.name.tableize end end self.parent.add_child self if self.parent.present? end |
Instance Attribute Details
#children ⇒ Object (readonly)
Array of child nodes.
12 13 14 |
# File 'lib/fancygrid/node.rb', line 12 def children @children end |
#name ⇒ Object (readonly)
The name of this node.
15 16 17 |
# File 'lib/fancygrid/node.rb', line 15 def name @name end |
#parent ⇒ Object (readonly)
The paent node.
9 10 11 |
# File 'lib/fancygrid/node.rb', line 9 def parent @parent end |
#resource_class ⇒ Object (readonly)
The corresponding resource class.
18 19 20 |
# File 'lib/fancygrid/node.rb', line 18 def resource_class @resource_class end |
#root ⇒ Object (readonly)
The fancygrid root node.
6 7 8 |
# File 'lib/fancygrid/node.rb', line 6 def root @root end |
#table_name ⇒ Object (readonly)
The table name of the resource class.
21 22 23 |
# File 'lib/fancygrid/node.rb', line 21 def table_name @table_name end |
Instance Method Details
#attributes(*names) ⇒ Object
Creates and adds a column for each given parameter. The columns are marked as selectable and searchable.
79 80 81 82 83 84 |
# File 'lib/fancygrid/node.rb', line 79 def attributes(*names) = names. [:searchable] = .fetch(:searchable, true) [:selectable] = .fetch(:selectable, true) columns(names, ) end |
#collect_columns(collection) ⇒ Object
Collects all columns into the given collection
104 105 106 107 108 109 |
# File 'lib/fancygrid/node.rb', line 104 def collect_columns collection self.children.each do |child| child.collect_columns collection end return collection end |
#column(name, options = {}) ⇒ Object
Creates and adds a column to this node.
59 60 61 62 63 |
# File 'lib/fancygrid/node.rb', line 59 def column(name, = {}) [:class] ||= self.resource_class [:table_name] ||= self.table_name Fancygrid::Column.new(self, name, ) end |
#columns(*names) ⇒ Object
Creates and adds a column for each given parameter. The columns are marked to be not selectable and not searchable.
68 69 70 71 72 73 74 |
# File 'lib/fancygrid/node.rb', line 68 def columns(*names) = names. [:searchable] = .fetch(:searchable, false) [:selectable] = .fetch(:selectable, false) [:value_proc] = Proc.new if block_given? names.flatten.map { |name| self.column(name, ) } end |
#columns_for(name, options = {}) {|node| ... } ⇒ Object
Creates and yields a child node.
51 52 53 54 55 |
# File 'lib/fancygrid/node.rb', line 51 def columns_for(name, = {})# :yields: node = Fancygrid::Node.new(self, name, ) yield node if block_given? return node end |
#name_chain ⇒ Object
Gets a dot separated string of names of all parent nodes including own.
88 89 90 91 92 93 94 |
# File 'lib/fancygrid/node.rb', line 88 def name_chain if @name_chain.nil? prefix = (parent and parent.name_chain) @name_chain = [prefix, self.name].compact.join(".") end return @name_chain end |
#root? ⇒ Boolean
Returns true if this node is the root node of the tree.
98 99 100 |
# File 'lib/fancygrid/node.rb', line 98 def root? self.root.equal?(self) end |