module Ancestry::MaterializedPath::InstanceMethods
Public Instance Methods
ancestor_ids()
click to toggle source
# File lib/ancestry/materialized_path.rb, line 96 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 91 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 104 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 100 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 86 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 123 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
parent_id_before_last_save()
click to toggle source
# File lib/ancestry/materialized_path.rb, line 108 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
sibling_of?(node)
click to toggle source
optimization - better to go directly to column and avoid parsing
# File lib/ancestry/materialized_path.rb, line 116 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 132 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