Module: Replicate::AR::ClassMethods

Defined in:
lib/replicate/active_record.rb

Overview

Mixin for the ActiveRecord class.

Instance Method Summary collapse

Instance Method Details

#create_or_update_replicant(instance, attributes) ⇒ Object

Update an AR object’s attributes and persist to the database without running validations or callbacks.

Returns the [id, object] tuple for the newly replicated objected.



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

def create_or_update_replicant(instance, attributes)
  # write replicated attributes to the instance
  attributes.each do |key, value|
    next if key == primary_key and not replicate_id
    instance.send :write_attribute, key, value
  end

  # save the instance bypassing all callbacks and validations
  replicate_disable_callbacks instance
  instance.save :validate => false

  [instance.send(instance.class.primary_key), instance]
end

#load_replicant(type, id, attributes) ⇒ Object

Load an individual record into the database. If the models defines a replicate_natural_key then an existing record will be updated if found instead of a new record being created.

type - Model class name as a String. id - Primary key id of the record on the dump system. This must be

translated to the local system and stored in the keymap.

attrs - Hash of attributes to set on the new record.

Returns the ActiveRecord object instance for the new record.



254
255
256
257
# File 'lib/replicate/active_record.rb', line 254

def load_replicant(type, id, attributes)
  instance = replicate_find_existing_record(attributes) || new
  create_or_update_replicant instance, attributes
end

#replicate_associations(*names) ⇒ Object

Set and retrieve list of association names that should be dumped when objects of this class are dumped. This method may be called multiple times to add associations.



187
188
189
190
# File 'lib/replicate/active_record.rb', line 187

def replicate_associations(*names)
  self.replicate_associations += names if names.any?
  @replicate_associations || superclass.replicate_associations
end

#replicate_associations=(names) ⇒ Object

Set the list of association names to dump to the specific set of values.



193
194
195
# File 'lib/replicate/active_record.rb', line 193

def replicate_associations=(names)
  @replicate_associations = names.uniq.map { |name| name.to_sym }
end

#replicate_disable_callbacks(instance) ⇒ Object

Disable all callbacks on an ActiveRecord::Base instance. Only the instance is effected. There is no way to re-enable callbacks once they’ve been disabled on an object.



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

def replicate_disable_callbacks(instance)
  def instance.run_callbacks(*args)
    yield if block_given?
  end
end

#replicate_find_existing_record(attributes) ⇒ Object

Locate an existing record using the replicate_natural_key attribute values.

Returns the existing record if found, nil otherwise.



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

def replicate_find_existing_record(attributes)
  return if replicate_natural_key.empty?
  conditions = {}
  replicate_natural_key.each do |attribute_name|
    conditions[attribute_name] = attributes[attribute_name.to_s]
  end
  where(conditions).first
end

#replicate_id(boolean = nil) ⇒ Object

Set or retrieve whether replicated object should keep its original id. When not set, replicated objects will be created with new id.



216
217
218
219
# File 'lib/replicate/active_record.rb', line 216

def replicate_id(boolean=nil)
  self.replicate_id = boolean unless boolean.nil?
  @replicate_id.nil? ? superclass.replicate_id : @replicate_id
end

#replicate_id=(boolean) ⇒ Object

Set flag for replicating original id.



222
223
224
225
# File 'lib/replicate/active_record.rb', line 222

def replicate_id=(boolean)
  self.replicate_natural_key = [self.primary_key.to_sym] if boolean
  @replicate_id = boolean
end

#replicate_natural_key(*attribute_names) ⇒ Object

Compound key used during load to locate existing objects for update. When no natural key is defined, objects are created new.

attribute_names - Macro style setter.



201
202
203
204
# File 'lib/replicate/active_record.rb', line 201

def replicate_natural_key(*attribute_names)
  self.replicate_natural_key = attribute_names if attribute_names.any?
  @replicate_natural_key || superclass.replicate_natural_key
end

#replicate_natural_key=(attribute_names) ⇒ Object

Set the compound key used to locate existing objects for update when loading. When not set, loading will always create new records.

attribute_names - Array of attribute name symbols



210
211
212
# File 'lib/replicate/active_record.rb', line 210

def replicate_natural_key=(attribute_names)
  @replicate_natural_key = attribute_names
end

#replicate_omit_attributes(*attribute_names) ⇒ Object

Set which, if any, attributes should not be dumped. Also works for associations.

attribute_names - Macro style setter.



231
232
233
234
# File 'lib/replicate/active_record.rb', line 231

def replicate_omit_attributes(*attribute_names)
  self.replicate_omit_attributes = attribute_names if attribute_names.any?
  @replicate_omit_attributes || superclass.replicate_omit_attributes
end

#replicate_omit_attributes=(attribute_names) ⇒ Object

Set which, if any, attributes should not be dumped. Also works for associations.

attribute_names - Array of attribute name symbols



240
241
242
# File 'lib/replicate/active_record.rb', line 240

def replicate_omit_attributes=(attribute_names)
  @replicate_omit_attributes = attribute_names
end