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.



268
269
270
271
# File 'lib/replicate/active_record.rb', line 268

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

Class Method Details

.load_replicant(type, id, attrs) ⇒ Object



293
294
295
296
297
298
# File 'lib/replicate/active_record.rb', line 293

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



276
277
278
279
280
281
282
283
284
285
# File 'lib/replicate/active_record.rb', line 276

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



287
288
289
290
291
# File 'lib/replicate/active_record.rb', line 287

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



273
274
# File 'lib/replicate/active_record.rb', line 273

def id
end