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.



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/replicate/active_record.rb', line 221

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.



199
200
201
202
# File 'lib/replicate/active_record.rb', line 199

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.



132
133
134
135
# File 'lib/replicate/active_record.rb', line 132

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.



138
139
140
# File 'lib/replicate/active_record.rb', line 138

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.



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/replicate/active_record.rb', line 242

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.



208
209
210
211
212
213
214
215
# File 'lib/replicate/active_record.rb', line 208

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.



161
162
163
164
# File 'lib/replicate/active_record.rb', line 161

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.



167
168
169
170
# File 'lib/replicate/active_record.rb', line 167

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.



146
147
148
149
# File 'lib/replicate/active_record.rb', line 146

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



155
156
157
# File 'lib/replicate/active_record.rb', line 155

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.



176
177
178
179
# File 'lib/replicate/active_record.rb', line 176

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



185
186
187
# File 'lib/replicate/active_record.rb', line 185

def replicate_omit_attributes=(attribute_names)
  @replicate_omit_attributes = attribute_names
end