Module: Zena::Use::Ancestry::ClassMethods

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

Constant Summary collapse

TITLE_ML_JOIN =
%Q{INNER JOIN idx_nodes_ml_strings AS id1 ON id1.node_id = nodes.id AND id1.key = 'title'}

Instance Method Summary collapse

Instance Method Details

#find_by_path(path, parent_id = current_site.root_id, multilingual = false) ⇒ Object

(slow). Find a node by it’s path. This is used during node importation when stored as zml files or to resolve custom_base url until we have an “alias” table.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zena/use/ancestry.rb', line 13

def find_by_path(path, parent_id = current_site.root_id, multilingual = false)
  res  = nil
  path = path.split('/') unless path.kind_of?(Array)
  last = path.size - 1
  path.each_with_index do |title, i|
    klass = i == last ? self : Node
    unless p = klass.find(:first,
        :select     => i == last ? 'nodes.*' : 'nodes.id',
        :joins      => multilingual ? title_join : TITLE_ML_JOIN,
        :conditions => ["parent_id = ? AND id1.value = ? AND #{secure_scope('nodes')}", parent_id, title]
      )
      # Block as soon as we cannot find an element
      return nil
    end
    parent_id = p['id']
    res = p if i == last
  end
  res
end

#fullpath_map(path, sym = :node) ⇒ Object

(slow). Transform a list of zips into a fullpath.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/zena/use/ancestry.rb', line 34

def fullpath_map(path, sym = :node)
  path = path.split('/') unless path.kind_of?(Array)
  zips = path.reject{|e| e == '..'}
  case sym
  when :title
    opts = {
      :select => 'zip, id1.value AS title',
      :joins  => title_join,
      :conditions => ["zip IN (?) AND #{secure_scope('nodes')}", zips],
    }
  when :node
    opts = {
      :conditions => ["zip IN (?) AND #{secure_scope('nodes')}", zips],
    }
  else
    # not supported
    raise Exception.new("#{sym} not supported for fullpath_map")
  end


  list = Node.send(:with_exclusive_scope) do
    Node.find(:all, opts)
  end

  list = Hash[*(list.map{|e| [e['zip'].to_i, sym == :node ? e : e[sym.to_s]]}).flatten]

  path.map do |zip|
    zip == '..' ? '..' : (list[zip.to_i] || (sym == :node ? nil : '*'))
  end.compact
end

#title_joinObject



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

def title_join
  %Q{INNER JOIN idx_nodes_ml_strings AS id1 ON id1.node_id = nodes.id AND id1.key = 'title' AND id1.lang = '#{visitor.lang}'}
end