Module: ActsAsAliased::InstanceMethods

Defined in:
lib/acts_as_aliased.rb

Instance Method Summary collapse

Instance Method Details

#to_alias!(aliased) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/acts_as_aliased.rb', line 35

def to_alias! aliased
  raise "Cannot create alias for myself" if aliased == self
  self.class.transaction do

    # Move references to this instance from the provided associations to the
    # newly aliased one
    a = ::ActsAsAliased::Alias.create(aliased: aliased, name: self[column])
    associations.each do |association|
      klass = association.to_s.classify.constantize
      key   = self.class.to_s.foreign_key
      klass.where(key => id).update_all(key => aliased.id)
    end

    # Move references to this instance to the newly aliased one
    Alias.where("aliased_type = ? AND aliased_id = ?", self.class.to_s, self.id).update_all(aliased_id: aliased.id)

    # Poof!
    self.destroy
    return a
  end
end