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



72
73
74
75
76
77
78
79
80
# File 'lib/zena/use/ancestry.rb', line 72

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



100
101
102
103
104
105
106
107
108
109
# File 'lib/zena/use/ancestry.rb', line 100

def ancestors(start=[])
  if id == current_site.root_id
    []
  elsif parent_id.nil?
    []
  else
    path = fullpath.split('/')[0..-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.



114
115
116
# File 'lib/zena/use/ancestry.rb', line 114

def basepath
  self[:basepath]
end

#fullpath_as_title(path = fullpath) ⇒ Object

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



140
141
142
143
144
145
146
147
# File 'lib/zena/use/ancestry.rb', line 140

def fullpath_as_title(path = fullpath)
  if path == self.fullpath
    # 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)


89
90
91
92
93
94
95
96
# File 'lib/zena/use/ancestry.rb', line 89

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



128
129
130
131
132
133
134
135
136
137
# File 'lib/zena/use/ancestry.rb', line 128

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.



119
120
121
122
123
124
125
126
# File 'lib/zena/use/ancestry.rb', line 119

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.



83
84
85
86
# File 'lib/zena/use/ancestry.rb', line 83

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