Module: Ancestry::MaterializedPath::InstanceMethods

Defined in:
lib/ancestry/materialized_path.rb

Instance Method Summary collapse

Instance Method Details

#ancestor_idsObject



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_saveObject



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_databaseObject



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

Returns:

  • (Boolean)


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_ancestryObject

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_saveObject



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_saveObject



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_databaseObject



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

Returns:

  • (Boolean)


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