module Ancestry::MaterializedPath::InstanceMethods

Public Instance Methods

ancestor_ids() click to toggle source
# File lib/ancestry/materialized_path.rb, line 113
def ancestor_ids
  parse_ancestry_column(read_attribute(self.ancestry_base_class.ancestry_column))
end
ancestor_ids=(value) click to toggle source
# File lib/ancestry/materialized_path.rb, line 108
def ancestor_ids=(value)
  col = self.ancestry_base_class.ancestry_column
  value.present? ? write_attribute(col, value.join(ANCESTRY_DELIMITER)) : write_attribute(col, nil)
end
ancestor_ids_before_last_save() click to toggle source
# File lib/ancestry/materialized_path.rb, line 121
def ancestor_ids_before_last_save
  parse_ancestry_column(send("#{self.ancestry_base_class.ancestry_column}#{BEFORE_LAST_SAVE_SUFFIX}"))
end
ancestor_ids_in_database() click to toggle source
# File lib/ancestry/materialized_path.rb, line 117
def ancestor_ids_in_database
  parse_ancestry_column(send("#{self.ancestry_base_class.ancestry_column}#{IN_DATABASE_SUFFIX}"))
end
ancestors?() click to toggle source

optimization - better to go directly to column and avoid parsing

# File lib/ancestry/materialized_path.rb, line 103
def ancestors?
  read_attribute(self.ancestry_base_class.ancestry_column).present?
end
Also aliased as: has_parent?
child_ancestry() click to toggle source

private (public so class methods can find it) The ancestry value for this record's children (before save) This is technically child_ancestry_was

# File lib/ancestry/materialized_path.rb, line 140
def child_ancestry
  # New records cannot have children
  raise Ancestry::AncestryException.new(I18n.t("ancestry.no_child_for_new_record")) if new_record?
  path_was = self.send("#{self.ancestry_base_class.ancestry_column}#{IN_DATABASE_SUFFIX}")
  path_was.blank? ? id.to_s : "#{path_was}/#{id}"
end
has_parent?()
Alias for: ancestors?
parent_id_before_last_save() click to toggle source
# File lib/ancestry/materialized_path.rb, line 125
def parent_id_before_last_save
  ancestry_was = send("#{self.ancestry_base_class.ancestry_column}#{BEFORE_LAST_SAVE_SUFFIX}")
  return unless ancestry_was.present?

  parse_ancestry_column(ancestry_was).last
end
sane_ancestry?() click to toggle source

Validates the ancestry, but can also be applied if validation is bypassed to determine if children should be affected

# File lib/ancestry/materialized_path.rb, line 97
def sane_ancestry?
  ancestry_value = read_attribute(self.ancestry_base_class.ancestry_column)
  (ancestry_value.nil? || !ancestor_ids.include?(self.id)) && valid?
end
sibling_of?(node) click to toggle source

optimization - better to go directly to column and avoid parsing

# File lib/ancestry/materialized_path.rb, line 133
def sibling_of?(node)
  self.read_attribute(self.ancestry_base_class.ancestry_column) == node.read_attribute(self.ancestry_base_class.ancestry_column)
end

Private Instance Methods

parse_ancestry_column(obj) click to toggle source
# File lib/ancestry/materialized_path.rb, line 149
def parse_ancestry_column obj
  return [] unless obj
  obj_ids = obj.split(ANCESTRY_DELIMITER)
  self.class.primary_key_is_an_integer? ? obj_ids.map!(&:to_i) : obj_ids
end