Module: Ancestry::MaterializedPath::InstanceMethods
- Defined in:
- lib/ancestry/materialized_path.rb
Instance Method Summary collapse
- #ancestor_ids ⇒ Object
- #ancestor_ids=(value) ⇒ Object
- #ancestor_ids_before_last_save ⇒ Object
- #ancestor_ids_in_database ⇒ Object
-
#ancestors? ⇒ Boolean
(also: #has_parent?)
optimization - better to go directly to column and avoid parsing.
-
#child_ancestry ⇒ Object
private (public so class methods can find it) The ancestry value for this record’s children (before save) This is technically child_ancestry_was.
- #child_ancestry_before_save ⇒ Object
- #generate_ancestry(ancestor_ids) ⇒ Object
- #parent_id_before_last_save ⇒ Object
- #parent_id_in_database ⇒ Object
- #parse_ancestry_column(obj) ⇒ Object
-
#sibling_of?(node) ⇒ Boolean
optimization - better to go directly to column and avoid parsing.
Instance Method Details
#ancestor_ids ⇒ Object
123 124 125 |
# 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) ⇒ Object
119 120 121 |
# 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 ⇒ Object
131 132 133 |
# 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 ⇒ Object
127 128 129 |
# 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? ⇒ Boolean Also known as: has_parent?
optimization - better to go directly to column and avoid parsing
114 115 116 |
# 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 |
#child_ancestry ⇒ Object
private (public so class methods can find it) The ancestry value for this record’s children (before save) This is technically child_ancestry_was
151 152 153 154 155 |
# 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 ⇒ Object
157 158 159 160 161 |
# 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) ⇒ Object
169 170 171 172 173 174 175 |
# 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 |
#parent_id_before_last_save ⇒ Object
139 140 141 |
# 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 ⇒ Object
135 136 137 |
# 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) ⇒ Object
163 164 165 166 167 |
# 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) ⇒ Boolean
optimization - better to go directly to column and avoid parsing
144 145 146 |
# 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 |