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.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/replicate/active_record.rb', line 209

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.



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

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.



121
122
123
124
# File 'lib/replicate/active_record.rb', line 121

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.



127
128
129
# File 'lib/replicate/active_record.rb', line 127

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.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/replicate/active_record.rb', line 230

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.



196
197
198
199
200
201
202
203
# File 'lib/replicate/active_record.rb', line 196

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.



150
151
152
153
# File 'lib/replicate/active_record.rb', line 150

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.



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

def replicate_id=(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.



135
136
137
138
# File 'lib/replicate/active_record.rb', line 135

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



144
145
146
# File 'lib/replicate/active_record.rb', line 144

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.



164
165
166
167
# File 'lib/replicate/active_record.rb', line 164

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



173
174
175
# File 'lib/replicate/active_record.rb', line 173

def replicate_omit_attributes=(attribute_names)
  @replicate_omit_attributes = attribute_names
end