module Ancestry::MaterializedPath::InstanceMethods
Public Instance Methods
ancestor_ids()
click to toggle source
# File lib/ancestry/materialized_path.rb, line 101 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 96 def ancestor_ids=(value) col = self.ancestry_base_class.ancestry_column value.present? ? write_attribute(col, generate_ancestry(value)) : write_attribute(col, ROOT) end
ancestor_ids_before_last_save()
click to toggle source
# File lib/ancestry/materialized_path.rb, line 109 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 105 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 91 def ancestors? read_attribute(self.ancestry_base_class.ancestry_column) != 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 128 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}#{ANCESTRY_DELIMITER}#{id}" end
generate_ancestry(ancestor_ids)
click to toggle source
# File lib/ancestry/materialized_path.rb, line 141 def generate_ancestry(ancestor_ids) ancestor_ids.join(ANCESTRY_DELIMITER) end
parent_id_before_last_save()
click to toggle source
# File lib/ancestry/materialized_path.rb, line 113 def parent_id_before_last_save ancestry_was = send("#{self.ancestry_base_class.ancestry_column}#{BEFORE_LAST_SAVE_SUFFIX}") return if ancestry_was == ROOT parse_ancestry_column(ancestry_was).last end
parse_ancestry_column(obj)
click to toggle source
# File lib/ancestry/materialized_path.rb, line 135 def parse_ancestry_column(obj) return [] if obj == ROOT obj_ids = obj.split(ANCESTRY_DELIMITER) 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 121 def sibling_of?(node) self.read_attribute(self.ancestry_base_class.ancestry_column) == node.read_attribute(self.ancestry_base_class.ancestry_column) end