Module: EvenBetterNestedSet::NestedSet::ClassMethods
- Defined in:
- lib/eb_nested_set.rb
Instance Attribute Summary collapse
-
#nested_set_options ⇒ Object
Returns the value of attribute nested_set_options.
Instance Method Summary collapse
-
#find_boundaries(id) ⇒ Array[Integer]
Finds the left and right boundaries of a node given an id.
-
#find_last_root ⇒ ActiveRecord::Base
Finds the last root, used internally to find the point to insert new roots.
-
#find_with_nested_set(*args) ⇒ Array[ActiveRecord::Base], ActiveRecord::Base
Finds all nodes matching the criteria provided, and caches their descendants.
-
#nested_set ⇒ Array[ActiveRecord::Base]
Returns all nodes with children cached to a nested set.
-
#nested_set_column(name) ⇒ String
Returns the properly quoted column name given the generic term.
-
#quote_db_property(property) ⇒ String
Properly quotes a column name.
-
#recalculate_nested_set ⇒ Object
Recalculates the left and right values for the entire tree.
-
#sort_nodes_to_nested_set(nodes) ⇒ Array[ActiveRecord::Base]
Given a flat list of nodes, sorts them to a tree, caching descendants in the process.
Instance Attribute Details
#nested_set_options ⇒ Object
Returns the value of attribute nested_set_options.
65 66 67 |
# File 'lib/eb_nested_set.rb', line 65 def @nested_set_options end |
Instance Method Details
#find_boundaries(id) ⇒ Array[Integer]
Finds the left and right boundaries of a node given an id.
81 82 83 84 85 86 |
# File 'lib/eb_nested_set.rb', line 81 def find_boundaries(id) query = "SELECT #{nested_set_column(:left)}, #{nested_set_column(:right)}" + "FROM #{quote_db_property(table_name)}" + "WHERE #{quote_db_property(primary_key)} = #{id}" connection.select_rows(query).first end |
#find_last_root ⇒ ActiveRecord::Base
Finds the last root, used internally to find the point to insert new roots
72 73 74 |
# File 'lib/eb_nested_set.rb', line 72 def find_last_root find(:first, :order => "#{nested_set_column(:right)} DESC", :conditions => { :parent_id => nil }) end |
#find_with_nested_set(*args) ⇒ Array[ActiveRecord::Base], ActiveRecord::Base
Finds all nodes matching the criteria provided, and caches their descendants
103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/eb_nested_set.rb', line 103 def find_with_nested_set(*args) result = find(*args) if result.respond_to?(:cache_nested_set) result.cache_nested_set elsif result.respond_to?(:each) result.each do |node| node.cache_nested_set end end result end |
#nested_set ⇒ Array[ActiveRecord::Base]
Returns all nodes with children cached to a nested set
93 94 95 |
# File 'lib/eb_nested_set.rb', line 93 def nested_set sort_nodes_to_nested_set(find(:all, :order => "#{nested_set_column(:left)} ASC")) end |
#nested_set_column(name) ⇒ String
Returns the properly quoted column name given the generic term
149 150 151 |
# File 'lib/eb_nested_set.rb', line 149 def nested_set_column(name) quote_db_property([name]) end |
#quote_db_property(property) ⇒ String
Properly quotes a column name
171 172 173 |
# File 'lib/eb_nested_set.rb', line 171 def quote_db_property(property) connection.quote_column_name(property) end |
#recalculate_nested_set ⇒ Object
Recalculates the left and right values for the entire tree
156 157 158 159 160 161 162 163 |
# File 'lib/eb_nested_set.rb', line 156 def recalculate_nested_set transaction do left = 1 roots.each do |root| left = root.recalculate_nested_set(left) end end end |
#sort_nodes_to_nested_set(nodes) ⇒ Array[ActiveRecord::Base]
Given a flat list of nodes, sorts them to a tree, caching descendants in the process
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/eb_nested_set.rb', line 121 def sort_nodes_to_nested_set(nodes) roots = [] hashmap = {} for node in nodes.sort_by { |n| n.left } # if the parent is not in the hashmap, parent will be nil, therefore node will be a root node # in that case parent = node.parent_id ? hashmap[node.parent_id] : nil # make sure this is called at least once on every node, so leaves know that they have *no* children node.cache_children() if parent node.cache_parent(parent) parent.cache_children(node) else roots << node end hashmap[node.id] = node end return roots end |