Class: Replicate::AR::Habtm

Inherits:
Object
  • Object
show all
Defined in:
lib/replicate/active_record.rb

Overview

Special object used to dump the list of associated ids for a has_and_belongs_to_many association. The object includes attributes for locating the source object and writing the list of ids to the appropriate association method.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, reflection) ⇒ Habtm

Returns a new instance of Habtm.



256
257
258
259
# File 'lib/replicate/active_record.rb', line 256

def initialize(object, reflection)
  @object = object
  @reflection = reflection
end

Class Method Details

.load_replicant(type, id, attrs) ⇒ Object



281
282
283
284
285
286
# File 'lib/replicate/active_record.rb', line 281

def self.load_replicant(type, id, attrs)
  object = attrs['class'].constantize.find(attrs['id'])
  ids    = attrs['collection']
  object.__send__("#{attrs['ref_name'].to_s.singularize}_ids=", ids)
  [id, new(object, nil)]
end

Instance Method Details

#attributesObject



264
265
266
267
268
269
270
271
272
273
# File 'lib/replicate/active_record.rb', line 264

def attributes
  ids = @object.__send__("#{@reflection.name.to_s.singularize}_ids")
  {
    'id'         => [:id, @object.class.to_s, @object.id],
    'class'      => @object.class.to_s,
    'ref_class'  => @reflection.klass.to_s,
    'ref_name'   => @reflection.name.to_s,
    'collection' => [:id, @reflection.klass.to_s, ids]
  }
end

#dump_replicant(dumper) ⇒ Object



275
276
277
278
279
# File 'lib/replicate/active_record.rb', line 275

def dump_replicant(dumper)
  type = self.class.name
  id   = "#{@object.class.to_s}:#{@reflection.name}:#{@object.id}"
  dumper.write type, id, attributes, self
end

#idObject



261
262
# File 'lib/replicate/active_record.rb', line 261

def id
end