module Ancestry::MaterializedPath::InstanceMethods

Public Instance Methods

ancestor_ids() click to toggle source
# File lib/ancestry/materialized_path.rb, line 123
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 119
def ancestor_ids=(value)
  write_attribute(self.ancestry_base_class.ancestry_column, generate_ancestry(value))
end
ancestor_ids_before_last_save() click to toggle source
# File lib/ancestry/materialized_path.rb, line 131
def ancestor_ids_before_last_save
  parse_ancestry_column(attribute_before_last_save(self.ancestry_base_class.ancestry_column))
end
ancestor_ids_in_database() click to toggle source
# File lib/ancestry/materialized_path.rb, line 127
def ancestor_ids_in_database
  parse_ancestry_column(attribute_in_database(self.class.ancestry_column))
end
ancestors?() click to toggle source

optimization - better to go directly to column and avoid parsing

# File lib/ancestry/materialized_path.rb, line 114
def ancestors?
  read_attribute(self.ancestry_base_class.ancestry_column) != self.ancestry_base_class.ancestry_root
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 151
def child_ancestry
  # New records cannot have children
  raise Ancestry::AncestryException.new(I18n.t("ancestry.no_child_for_new_record")) if new_record?
  [attribute_in_database(self.ancestry_base_class.ancestry_column), id].compact.join(self.ancestry_base_class.ancestry_delimiter)
end
child_ancestry_before_save() click to toggle source
# File lib/ancestry/materialized_path.rb, line 157
def child_ancestry_before_save
  # New records cannot have children
  raise Ancestry::AncestryException.new(I18n.t("ancestry.no_child_for_new_record")) if new_record?
  [attribute_before_last_save(self.ancestry_base_class.ancestry_column), id].compact.join(self.ancestry_base_class.ancestry_delimiter)
end
generate_ancestry(ancestor_ids) click to toggle source
# File lib/ancestry/materialized_path.rb, line 169
def generate_ancestry(ancestor_ids)
  if ancestor_ids.present? && ancestor_ids.any?
    ancestor_ids.join(self.ancestry_base_class.ancestry_delimiter)
  else
    self.ancestry_base_class.ancestry_root
  end
end
has_parent?()
Alias for: ancestors?
parent_id_before_last_save() click to toggle source
# File lib/ancestry/materialized_path.rb, line 139
def parent_id_before_last_save
  parse_ancestry_column(attribute_before_last_save(self.ancestry_base_class.ancestry_column)).last
end
parent_id_in_database() click to toggle source
# File lib/ancestry/materialized_path.rb, line 135
def parent_id_in_database
  parse_ancestry_column(attribute_in_database(self.class.ancestry_column)).last
end
parse_ancestry_column(obj) click to toggle source
# File lib/ancestry/materialized_path.rb, line 163
def parse_ancestry_column(obj)
  return [] if obj.nil? || obj == self.ancestry_base_class.ancestry_root
  obj_ids = obj.split(self.ancestry_base_class.ancestry_delimiter).delete_if(&:blank?)
  self.class.primary_key_is_an_integer? ? obj_ids.map!(&:to_i) : obj_ids
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 144
def sibling_of?(node)
  self.read_attribute(self.ancestry_base_class.ancestry_column) == node.read_attribute(self.ancestry_base_class.ancestry_column)
end