Module: Ancestry::MaterializedPath::InstanceMethods

Defined in:
lib/ancestry/materialized_path.rb

Instance Method Summary collapse

Instance Method Details

#ancestor_idsObject



192
193
194
# File 'lib/ancestry/materialized_path.rb', line 192

def ancestor_ids
  MaterializedPath.parse(read_attribute(self.class.ancestry_column), self.class.ancestry_root, self.class.ancestry_delimiter, self.class.primary_key_is_an_integer?)
end

#ancestor_ids=(value) ⇒ Object



188
189
190
# File 'lib/ancestry/materialized_path.rb', line 188

def ancestor_ids=(value)
  write_attribute(self.class.ancestry_column, self.class.generate_ancestry(value))
end

#ancestor_ids_before_last_saveObject



200
201
202
# File 'lib/ancestry/materialized_path.rb', line 200

def ancestor_ids_before_last_save
  MaterializedPath.parse(attribute_before_last_save(self.class.ancestry_column), self.class.ancestry_root, self.class.ancestry_delimiter, self.class.primary_key_is_an_integer?)
end

#ancestor_ids_in_databaseObject



196
197
198
# File 'lib/ancestry/materialized_path.rb', line 196

def ancestor_ids_in_database
  MaterializedPath.parse(attribute_in_database(self.class.ancestry_column), self.class.ancestry_root, self.class.ancestry_delimiter, self.class.primary_key_is_an_integer?)
end

#ancestors?Boolean Also known as: has_parent?

optimization - better to go directly to column and avoid parsing

Returns:

  • (Boolean)


183
184
185
# File 'lib/ancestry/materialized_path.rb', line 183

def ancestors?
  read_attribute(self.class.ancestry_column) != self.class.ancestry_root
end

#child_ancestryObject

The ancestry value for this record's children This can also be thought of as the ancestry value for the path If this is a new record, it has no id, and it is not valid. NOTE: This could have been called child_ancestry_in_database the child records were created from the version in the database



222
223
224
225
226
# File 'lib/ancestry/materialized_path.rb', line 222

def child_ancestry
  raise(Ancestry::AncestryException, I18n.t("ancestry.no_child_for_new_record")) if new_record?

  MaterializedPath.child_ancestry_value(attribute_in_database(self.class.ancestry_column), id, self.class.ancestry_delimiter)
end

#child_ancestry_before_last_saveObject

The ancestry value for this record's old children Currently used in an after_update via unscoped_descendants_before_last_save to find the old children and bring them along (or to ) This is not valid in a new record's after_save.



232
233
234
235
236
237
238
# File 'lib/ancestry/materialized_path.rb', line 232

def child_ancestry_before_last_save
  if new_record? || (respond_to?(:previously_new_record?) && previously_new_record?)
    raise Ancestry::AncestryException, I18n.t("ancestry.no_child_for_new_record")
  end

  MaterializedPath.child_ancestry_value(attribute_before_last_save(self.class.ancestry_column), id, self.class.ancestry_delimiter)
end

#parent_id_before_last_saveObject



208
209
210
# File 'lib/ancestry/materialized_path.rb', line 208

def parent_id_before_last_save
  MaterializedPath.parse(attribute_before_last_save(self.class.ancestry_column), self.class.ancestry_root, self.class.ancestry_delimiter, self.class.primary_key_is_an_integer?).last
end

#parent_id_in_databaseObject



204
205
206
# File 'lib/ancestry/materialized_path.rb', line 204

def parent_id_in_database
  MaterializedPath.parse(attribute_in_database(self.class.ancestry_column), self.class.ancestry_root, self.class.ancestry_delimiter, self.class.primary_key_is_an_integer?).last
end

#sibling_of?(node) ⇒ Boolean

optimization - better to go directly to column and avoid parsing

Returns:

  • (Boolean)


213
214
215
# File 'lib/ancestry/materialized_path.rb', line 213

def sibling_of?(node)
  read_attribute(self.class.ancestry_column) == node.read_attribute(node.class.ancestry_column)
end