Module: SimplyCouch::Model::Ancestry::ClassMethods

Defined in:
lib/simply_couch/model/ancestry.rb

Instance Method Summary collapse

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(options = {})
  if root_property = ancestry_by_property
    if options.blank?
      records = all
    elsif options.is_a?(Array)
      records = options
    elsif (options.is_a?(Symbol) || options.is_a?(String))
      records = send("find_all_by_#{root_property}", options.to_s)
    elsif options.is_a?(Hash) && options.keys.include?(root_property)
      root_key = options.delete(root_property)
      records = send("find_all_by_#{root_property}", root_key.to_s)
    else
      records = options[:records].presence || all
    end
  else
    records = options.is_a?(Array) ? options : (options.present? && options[: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(options = {})
  if root_property = ancestry_by_property
    if options.blank?
      options = {}
    elsif options.is_a?(Symbol)
      options = {startkey: [options.to_s], endkey: [options.to_s, {}]}
    elsif options.is_a?(String)
      options = {startkey: [options], endkey: [options, {}]}
    elsif options.keys.include?(root_property)
      root_key = options.delete(root_property)
      options[:startkey] = [root_key.to_s]
      options[:endkey] = [root_key.to_s, {}]
    end
  end
  options[:reduce] = false
  database.view(roots_view(options))
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