Module: SimplyCouch::Model::Ancestry::ClassMethods
- Defined in:
- lib/simply_couch/model/ancestry.rb
Instance Method Summary collapse
-
#build_tree(pages = nil) ⇒ Object
Build a tree from a flat set of pages making use of the path attribute.
- #full_tree(options = {}) ⇒ Object
- #roots(options = {}) ⇒ Object
-
#set_parent(parent, children) ⇒ Object
Recursive set parents.
Instance Method Details
#build_tree(pages = nil) ⇒ Object
Build a tree from a flat set of pages making use of the path attribute
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/simply_couch/model/ancestry.rb', line 260 def build_tree(pages = nil) pages ||= all return pages if pages.empty? # Do not process empty array @tree_wrapper = Struct.new(:children).new([]) # Dummy container as traversing begin, contains roots as children old_tree_slice = @tree_wrapper.children new_tree_slice = [] pages.sort_by!{|p| [p.path_ids.size, p.position]} root_depth = pages.first.path_ids.size current_depth = root_depth + 1 # Start counting/swapping from one depth deeper than first one for page in pages old_tree_slice << page and next if page.path_ids.size == root_depth # fill first slice with roots # Move further if new depth is reached if page.path_ids.size > current_depth old_tree_slice = new_tree_slice new_tree_slice = [] current_depth = page.path_ids.size end parent = old_tree_slice.find{|r| r.id == page.path_ids[-2]} # path id before last is parent id next unless parent # page is not associated in tree # Initialize children if needed parent.instance_variable_set('@children', []) unless parent.instance_variable_get('@children').is_a?(Array) # Avoid database call on deepest children page.instance_variable_set('@children', []) unless page.instance_variable_get('@children').is_a?(Array) parent.instance_variable_get('@children') << page page.instance_variable_set('@parent', parent) page.instance_variable_set('@parent_id', parent.id) new_tree_slice << page end #TODO: setting @descendants from cache as option to avoid database call when descendants is required @tree_wrapper.children end |
#full_tree(options = {}) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/simply_couch/model/ancestry.rb', line 239 def full_tree( = {}) if root_property = ancestry_by_property if .blank? records = all elsif .is_a?(Array) records = elsif (.is_a?(Symbol) || .is_a?(String)) records = send("find_all_by_#{root_property}", .to_s) elsif .is_a?(Hash) && .keys.include?(root_property) root_key = .delete(root_property) records = send("find_all_by_#{root_property}", root_key.to_s) else records = [:records].presence || all end else records = .is_a?(Array) ? : (.present? && [:records].presence) || all end build_tree(records) #.first.children end |
#roots(options = {}) ⇒ Object
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/simply_couch/model/ancestry.rb', line 221 def roots( = {}) if root_property = ancestry_by_property if .blank? = {} elsif .is_a?(Symbol) = {startkey: [.to_s], endkey: [.to_s, {}]} elsif .is_a?(String) = {startkey: [], endkey: [, {}]} elsif .keys.include?(root_property) root_key = .delete(root_property) [:startkey] = [root_key.to_s] [:endkey] = [root_key.to_s, {}] end end [:reduce] = false database.view(roots_view()) end |
#set_parent(parent, children) ⇒ Object
Recursive set parents
297 298 299 300 301 302 303 |
# File 'lib/simply_couch/model/ancestry.rb', line 297 def set_parent(parent, children) for child in children.sort_by!(&:position) child.instance_variable_set('@parent', parent) child.update_tree_path set_parent child, child.children end end |