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.home_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.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/zena/use/ancestry.rb', line 85

def find_by_path(path, parent_id = current_site.home_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.



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/zena/use/ancestry.rb', line 106

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



78
79
80
# File 'lib/zena/use/ancestry.rb', line 78

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