Module: Zena::Use::Ancestry::ModelMethods

Includes:
RubyLess
Included in:
Node
Defined in:
lib/zena/use/ancestry.rb

Overview

ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



145
146
147
148
149
150
151
152
153
# File 'lib/zena/use/ancestry.rb', line 145

def self.included(base)
  base.class_eval do
    # We do not use before_save to make sure this happens after 'zip' is set in 'node_before_create'.
    before_create :rebuild_paths
    before_update :rebuild_paths
    after_save    :rebuild_children_fullpath
    extend ClassMethods
  end
end

Instance Method Details

#ancestors(start = []) ⇒ Object

Return the list of ancestors (without self): [root, obj, obj] ancestors to which the visitor has no access are removed from the list



173
174
175
176
177
178
179
180
181
182
# File 'lib/zena/use/ancestry.rb', line 173

def ancestors(start=[])
  if id == current_site.root_id
    []
  elsif parent_id.nil?
    []
  else
    path = fullpath.split('/')[1..-2]
    [current_site.root_node].compact + (secure(Node) { Node.fullpath_map(path, :node) } || [])
  end
end

#basepathObject

url base path. If a node is in ‘projects’ and projects has custom_base set, the node’s basepath becomes ‘projects’, so the url will be ‘projects/node34.html’. The basepath is cached. If rebuild is set to true, the cache is updated.



187
188
189
# File 'lib/zena/use/ancestry.rb', line 187

def basepath
  self[:basepath]
end

#fullpath_as_title(path = nil) ⇒ Object

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



213
214
215
216
217
218
219
220
221
222
# File 'lib/zena/use/ancestry.rb', line 213

def fullpath_as_title(path = nil)
  if !path
    # When using fullpath, we remove first element (root)
    path = fullpath.split('/')[1..-1]
    # secure returns nil instead of [] so we fix this.
    @fullpath_as_title ||= secure(Node) { Node.fullpath_map(path, :title) } || []
  else
    secure(Node) { Node.fullpath_map(path, :title) } || []
  end
end

#is_ancestor?(child) ⇒ Boolean

Return true if the current node is an ancestor for the given child

Returns:

  • (Boolean)


162
163
164
165
166
167
168
169
# File 'lib/zena/use/ancestry.rb', line 162

def is_ancestor?(child)
  # self is it's own ancestor
  child.id       == id                 ||
  # parent
  child.fullpath =~ %r{\A#{fullpath}/} ||
  # root
  id             == current_site.root_id
end

#pseudo_id(root_node, sym) ⇒ Object



201
202
203
204
205
206
207
208
209
210
# File 'lib/zena/use/ancestry.rb', line 201

def pseudo_id(root_node, sym)
  case sym
  when :zip
    self.zip
  when :relative_path
    full = self.fullpath
    root = root_node ? root_node.fullpath : ''
    "(#{fullpath_as_title(full.rel_path(root)).map(&:to_filename).join('/')})"
  end
end

#short_pathObject

(slow). Return an array with the node title and the last two parents’ titles.



192
193
194
195
196
197
198
199
# File 'lib/zena/use/ancestry.rb', line 192

def short_path
  path = (fullpath || '').split('/')
  if path.size > 2
    ['..'] + fullpath_as_title(path[-2..-1])
  else
    fullpath_as_title(path)
  end
end

#z_ancestorsObject

Return the list of ancestors as a Zafu compatible context.



156
157
158
159
# File 'lib/zena/use/ancestry.rb', line 156

def z_ancestors
  anc = ancestors
  anc.empty? ? nil : anc
end