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.



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# File 'lib/replicate/active_record.rb', line 278

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
  if ::ActiveRecord::VERSION::MAJOR >= 3
    instance.save :validate => false
  else
    instance.save false
  end

  [instance.id, 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.



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

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.



189
190
191
192
# File 'lib/replicate/active_record.rb', line 189

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.



195
196
197
# File 'lib/replicate/active_record.rb', line 195

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.



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/replicate/active_record.rb', line 299

def replicate_disable_callbacks(instance)
  if ::ActiveRecord::VERSION::MAJOR >= 3
    # AR 3.1.x
    def instance.run_callbacks(*args); yield; end

    # AR 3.0.x
    def instance._run_save_callbacks(*args); yield; end
    def instance._run_create_callbacks(*args); yield; end
    def instance._run_update_callbacks(*args); yield; end
  else
    # AR 2.x
    def instance.callback(*args)
    end
    def instance.record_timestamps
      false
    end
  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.



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

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
  find(:first, :conditions => conditions)
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.



218
219
220
221
# File 'lib/replicate/active_record.rb', line 218

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.



224
225
226
227
# File 'lib/replicate/active_record.rb', line 224

def replicate_id=(boolean)
  self.replicate_natural_key = [:id] 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.



203
204
205
206
# File 'lib/replicate/active_record.rb', line 203

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



212
213
214
# File 'lib/replicate/active_record.rb', line 212

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.



233
234
235
236
# File 'lib/replicate/active_record.rb', line 233

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



242
243
244
# File 'lib/replicate/active_record.rb', line 242

def replicate_omit_attributes=(attribute_names)
  @replicate_omit_attributes = attribute_names
end