Module: PathFinder::InstanceMethods

Defined in:
lib/path_finder.rb

Instance Method Summary collapse

Instance Method Details

#descendantsObject



109
110
111
# File 'lib/path_finder.rb', line 109

def descendants
  children.map(&:descendants).flatten + children
end

#leaf?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/path_finder.rb', line 88

def leaf?
  children.empty?
end

#node?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/path_finder.rb', line 92

def node?
  !leaf?
end

#path_arrayObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/path_finder.rb', line 76

def path_array
  unless self.send(self.class.path_finder_column).blank?
    self.send(self.class.path_finder_column.split(self.class.path_finder_deliminator))
  else
    if new_record? && parent
      parent.path_array + [self.send(self.class.path_finder_uid)]
    else
      [] # this should not be possible (test required)
    end
  end
end

#path_text(attribute = 'name', join = ' | ') ⇒ Object

return a breadcrumb for any attribute



101
102
103
# File 'lib/path_finder.rb', line 101

def path_text(attribute = 'name', join = ' | ')
  ancestors.collect { |item| item.send(attribute) }.join(join)
end

#recreate_path!Object



113
114
115
116
# File 'lib/path_finder.rb', line 113

def recreate_path!
  set_path
  save!
end

#root?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/path_finder.rb', line 96

def root?
  parent_id.nil?
end

#self_and_descendantsObject



105
106
107
# File 'lib/path_finder.rb', line 105

def self_and_descendants
  descendants + [self]
end

#send_down(*args) ⇒ Object

Call method for ancestors, stop if we get an non-nil answer



119
120
121
122
123
124
125
# File 'lib/path_finder.rb', line 119

def send_down(*args)
  result = self.send(*args)
  if result.nil? && !self.root?
    result = self.parent.send_down(*args)
  end
  result
end

#send_up(*args) ⇒ Object

Call method on all children



128
129
130
131
132
133
# File 'lib/path_finder.rb', line 128

def send_up(*args)
  return if children.empty?
  children.each do | child |
    child.send_up(*args)
  end
end

#set_pathObject

FIXME: private before_validation_on_create



67
68
69
70
71
72
73
74
# File 'lib/path_finder.rb', line 67

def set_path
  return unless self.send(self.class.path_finder_column).blank?
  unless self.root?
    self.send(self.class.path_finder_column + '=', [self.parent.send(self.class.path_finder_column), self.send(self.class.path_finder_uid)].join(self.class.path_finder_deliminator).gsub('//', '/'))
  else
    self.send(self.class.path_finder_column + '=', self.send(self.class.path_finder_uid))
  end
end