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.



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/replicate/active_record.rb', line 176

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.



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

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.



105
106
107
108
# File 'lib/replicate/active_record.rb', line 105

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.



111
112
113
# File 'lib/replicate/active_record.rb', line 111

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.



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/replicate/active_record.rb', line 197

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.



163
164
165
166
167
168
169
170
# File 'lib/replicate/active_record.rb', line 163

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.



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

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.



140
141
142
# File 'lib/replicate/active_record.rb', line 140

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.



119
120
121
122
# File 'lib/replicate/active_record.rb', line 119

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



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

def replicate_natural_key=(attribute_names)
  @replicate_natural_key = attribute_names
end