Class: Fancygrid::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/fancygrid/node.rb

Direct Known Subclasses

Column, Grid

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name, options = {}) ⇒ Node

Initializes the node.

Raises:

  • (ArgumentError)


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, options = {})
  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 = options.fetch(:class) do 
    self.name.to_s.classify.constantize 
  end
  
  @table_name = options.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

#childrenObject (readonly)

Array of child nodes.



12
13
14
# File 'lib/fancygrid/node.rb', line 12

def children
  @children
end

#nameObject (readonly)

The name of this node.



15
16
17
# File 'lib/fancygrid/node.rb', line 15

def name
  @name
end

#parentObject (readonly)

The paent node.



9
10
11
# File 'lib/fancygrid/node.rb', line 9

def parent
  @parent
end

#resource_classObject (readonly)

The corresponding resource class.



18
19
20
# File 'lib/fancygrid/node.rb', line 18

def resource_class
  @resource_class
end

#rootObject (readonly)

The fancygrid root node.



6
7
8
# File 'lib/fancygrid/node.rb', line 6

def root
  @root
end

#table_nameObject (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)
  options = names.extract_options!
  options[:searchable] = options.fetch(:searchable, true)
  options[:selectable] = options.fetch(:selectable, true)
  columns(names, options)
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, options = {})
  options[:class] ||= self.resource_class
  options[:table_name] ||= self.table_name
  Fancygrid::Column.new(self, name, options)
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)
  options = names.extract_options!
  options[:searchable] = options.fetch(:searchable, false)
  options[:selectable] = options.fetch(:selectable, false)
  options[:value_proc] = Proc.new if block_given?
  names.flatten.map { |name| self.column(name, options) }
end

#columns_for(name, options = {}) {|node| ... } ⇒ Object

Creates and yields a child node.

Yields:

  • (node)


51
52
53
54
55
# File 'lib/fancygrid/node.rb', line 51

def columns_for(name, options = {})# :yields:
  node = Fancygrid::Node.new(self, name, options)
  yield node if block_given?
  return node
end

#name_chainObject

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.

Returns:

  • (Boolean)


98
99
100
# File 'lib/fancygrid/node.rb', line 98

def root?
  self.root.equal?(self)
end