Module: Ancestry::MaterializedPath::InstanceMethods

Defined in:
lib/ancestry/materialized_path.rb

Instance Method Summary collapse

Instance Method Details

#ancestor_idsObject



170
171
172
# File 'lib/ancestry/materialized_path.rb', line 170

def ancestor_ids
  self.class.parse_ancestry_column(read_attribute(self.class.ancestry_column))
end

#ancestor_ids=(value) ⇒ Object



166
167
168
# File 'lib/ancestry/materialized_path.rb', line 166

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

#ancestor_ids_before_last_saveObject



178
179
180
# File 'lib/ancestry/materialized_path.rb', line 178

def ancestor_ids_before_last_save
  self.class.parse_ancestry_column(attribute_before_last_save(self.class.ancestry_column))
end

#ancestor_ids_in_databaseObject



174
175
176
# File 'lib/ancestry/materialized_path.rb', line 174

def ancestor_ids_in_database
  self.class.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

Returns:

  • (Boolean)


161
162
163
# File 'lib/ancestry/materialized_path.rb', line 161

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


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

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

  [attribute_in_database(self.class.ancestry_column), id].compact.join(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.



210
211
212
213
214
215
216
# File 'lib/ancestry/materialized_path.rb', line 210

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

  [attribute_before_last_save(self.class.ancestry_column), id].compact.join(self.class.ancestry_delimiter)
end

#parent_id_before_last_saveObject



186
187
188
# File 'lib/ancestry/materialized_path.rb', line 186

def parent_id_before_last_save
  self.class.parse_ancestry_column(attribute_before_last_save(self.class.ancestry_column)).last
end

#parent_id_in_databaseObject



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

def parent_id_in_database
  self.class.parse_ancestry_column(attribute_in_database(self.class.ancestry_column)).last
end

#sibling_of?(node) ⇒ Boolean

optimization - better to go directly to column and avoid parsing

Returns:

  • (Boolean)


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

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