Module: Zena::Use::Ancestry

Defined in:
lib/zena/use/ancestry.rb

Overview

This module handles the creation and maintenance of a ‘fullpath’ and cached project/section_id.

Defined Under Namespace

Modules: ClassMethods, ModelMethods

Class Method Summary collapse

Class Method Details

.basepath_from_fullpath(fullpath) ⇒ Object



6
7
8
9
# File 'lib/zena/use/ancestry.rb', line 6

def self.basepath_from_fullpath(fullpath)
  return '' if !fullpath # This happens with pseudo root/home when node is not accessible
  fullpath.split('/')[1..-1].join('/')
end

.make_basepath(fullpath, custom_base, parent_basepath) ⇒ Object

Rebuild basepath and return new value



22
23
24
25
26
27
28
# File 'lib/zena/use/ancestry.rb', line 22

def self.make_basepath(fullpath, custom_base, parent_basepath)
  if custom_base
    fullpath[1..-1]
  else
    parent_basepath || []
  end
end

.make_fullpath(zip, parent_fullpath) ⇒ Object

Makes a fullpath from the node’s zip and parent zip Array. Returns fullpath as an Array.



13
14
15
16
17
18
19
# File 'lib/zena/use/ancestry.rb', line 13

def self.make_fullpath(zip, parent_fullpath)
  if parent_fullpath
    parent_fullpath + [zip]
  else
    [zip]
  end
end

.rebuild_all_paths(rec, parent_fullpath = nil, parent_basepath = nil, visited = {}) ⇒ Object

Forces a full recursive basepath and fullpath rebuild. If parent_fullpath and parent_basepath are nil, base is the root node. parent_fullpath and parent_basepath should be provided as Array.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/zena/use/ancestry.rb', line 52

def self.rebuild_all_paths(rec, parent_fullpath = nil, parent_basepath = nil, visited = {})
  raise Zena::InvalidRecord, "Infinit loop in 'ancestors'. Node zip = #{rec['zip']}." if visited[rec['id']]
  fullpath, basepath = rebuild_paths(rec, parent_fullpath, parent_basepath)
  visited[rec['id']] = true
  
  # Do the same for each child. Depth first. Batch of 100 for children listing.
  i = 0
  batch_size = 100
  while true
    list  = Zena::Db.fetch_attributes(['id', 'zip', 'custom_base', 'fullpath', 'basepath'], 'nodes', "parent_id = #{rec['id']} AND site_id = #{current_site.id} ORDER BY id ASC LIMIT #{batch_size} OFFSET #{i * batch_size}")

    break if list.empty?
    list.each do |child|
      rebuild_all_paths(child, fullpath, basepath, visited)
    end

    if list.size == batch_size
      # 100 more
      i += 1
    else
      break
    end
  end
end

.rebuild_paths(rec, parent_fullpath, parent_basepath) ⇒ Object

Forces rebuild of paths. Returns the new paths.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/zena/use/ancestry.rb', line 31

def self.rebuild_paths(rec, parent_fullpath, parent_basepath)
  id, zip, custom_base = rec['id'], rec['zip'], rec['custom_base']
  custom_base = custom_base == true || custom_base == Zena::Db::TRUE_RESULT
  fullpath = make_fullpath(zip, parent_fullpath)
  basepath = make_basepath(fullpath, custom_base, parent_basepath)
  new_paths = {'fullpath' => fullpath.join('/'), 'basepath' => basepath.join('/')}
  if !new_paths.keys.inject(true){|s,e| s && rec[e] == new_paths[e]}
    # Need to save
    log = "[#{zip}] Fix paths: #{rec['fullpath']} => #{new_paths['fullpath']}, #{rec['basepath']} => #{new_paths['basepath']}"
    Rails.logger.warn log
    if RAILS_ENV != 'test'
      # When running rake task
      puts log
    end
    Zena::Db.execute "UPDATE nodes SET #{new_paths.map {|k,v| "#{Zena::Db.connection.quote_column_name(k)}=#{Zena::Db.quote(v)}"}.join(', ')} WHERE id = #{id} AND site_id = #{current_site.id}"
  end
  return fullpath, basepath
end