Module: ActiveRecord::Acts::Versioned::ActMethods

Defined in:
lib/junebug/ext/acts_as_versioned.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



237
238
239
# File 'lib/junebug/ext/acts_as_versioned.rb', line 237

def self.included(base) # :nodoc:
  base.extend ClassMethods
end

Instance Method Details

#changed?(attr_name = nil) ⇒ Boolean Also known as: dirty?

If called with no parameters, gets whether the current model has changed and needs to be versioned. If called with a single parameter, gets whether the parameter has changed.

Returns:

  • (Boolean)


319
320
321
322
323
# File 'lib/junebug/ext/acts_as_versioned.rb', line 319

def changed?(attr_name = nil)
  attr_name.nil? ?
    (!self.class.track_changed_attributes || (changed_attributes && changed_attributes.length > 0)) :
    (changed_attributes && changed_attributes.include?(attr_name.to_s))
end

#clear_old_versionsObject

Clears old revisions if a limit is set with the :limit option in acts_as_versioned. Override this method to set your own criteria for clearing old versions.



257
258
259
260
261
262
263
264
# File 'lib/junebug/ext/acts_as_versioned.rb', line 257

def clear_old_versions
  return if self.class.max_version_limit == 0
  excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit
  if excess_baggage > 0
    sql = "DELETE FROM #{self.class.versioned_table_name} WHERE version <= #{excess_baggage} AND #{self.class.versioned_foreign_key} = #{self.id}"
    self.class.versioned_class.connection.execute sql
  end
end

#clone_versioned_model(orig_model, new_model) ⇒ Object

Clones a model. Used when saving a new version or reverting a model’s version.



329
330
331
332
333
334
335
336
337
338
339
# File 'lib/junebug/ext/acts_as_versioned.rb', line 329

def clone_versioned_model(orig_model, new_model)
  self.versioned_attributes.each do |key|
    new_model.send("#{key}=", orig_model.attributes[key]) if orig_model.has_attribute?(key)
  end
  
  if orig_model.is_a?(self.class.versioned_class)
    new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column]
  elsif new_model.is_a?(self.class.versioned_class)
    new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
  end
end

#find_version(version) ⇒ Object

Finds a specific version of this model.



267
268
269
270
271
# File 'lib/junebug/ext/acts_as_versioned.rb', line 267

def find_version(version)
  return version if version.is_a?(self.class.versioned_class)
  return nil if version.is_a?(ActiveRecord::Base)
  find_versions(:conditions => ['version = ?', version], :limit => 1).first
end

#find_versions(options = {}) ⇒ Object

Finds versions of this model. Takes an options hash like find



274
275
276
# File 'lib/junebug/ext/acts_as_versioned.rb', line 274

def find_versions(options = {})
  versions.find(:all, options)
end

#revert_to(version) ⇒ Object

Reverts a model to a given version. Takes either a version number or an instance of the versioned model



279
280
281
282
283
284
285
286
287
288
# File 'lib/junebug/ext/acts_as_versioned.rb', line 279

def revert_to(version)
  if version.is_a?(self.class.versioned_class)
    return false unless version.send(self.class.versioned_foreign_key) == self.id and !version.new_record?
  else
    return false unless version = find_version(version)
  end
  self.clone_versioned_model(version, self)
  self.send("#{self.class.version_column}=", version.version)
  true
end

#revert_to!(version) ⇒ Object

Reverts a model to a given version and saves the model.

Takes either a version number or an instance of the versioned model



292
293
294
# File 'lib/junebug/ext/acts_as_versioned.rb', line 292

def revert_to!(version)
  revert_to(version) ? save_without_revision : false
end

#save_versionObject

Saves a version of the model if applicable



242
243
244
# File 'lib/junebug/ext/acts_as_versioned.rb', line 242

def save_version
  save_version_on_create if save_version?
end

#save_version?Boolean

Checks whether a new version shall be saved or not. Calls version_condition_met? and changed?.

Returns:

  • (Boolean)


342
343
344
# File 'lib/junebug/ext/acts_as_versioned.rb', line 342

def save_version?
  version_condition_met? && changed?
end

#save_version_on_createObject

Saves a version of the model in the versioned table. This is called in the after_save callback by default



247
248
249
250
251
252
253
# File 'lib/junebug/ext/acts_as_versioned.rb', line 247

def save_version_on_create
  rev = self.class.versioned_class.new
  self.clone_versioned_model(self, rev)
  rev.version = send(self.class.version_column)
  rev.send("#{self.class.versioned_foreign_key}=", self.id)
  rev.save
end

#save_without_revisionObject

Temporarily turns off Optimistic Locking while saving. Used when reverting so that a new version is not created.



297
298
299
300
301
302
# File 'lib/junebug/ext/acts_as_versioned.rb', line 297

def save_without_revision
  save_without_revision!
  true
rescue
  false
end

#save_without_revision!Object



304
305
306
307
308
309
310
# File 'lib/junebug/ext/acts_as_versioned.rb', line 304

def save_without_revision!
  without_locking do
    without_revision do
      save!
    end
  end
end

#version_condition_met?Boolean

Checks condition set in the :if option to check whether a revision should be created or not. Override this for custom version condition checking.

Returns:

  • (Boolean)


348
349
350
351
352
353
354
355
356
357
# File 'lib/junebug/ext/acts_as_versioned.rb', line 348

def version_condition_met?
  case
  when version_condition.is_a?(Symbol)
    send(version_condition)
  when version_condition.respond_to?(:call) && (version_condition.arity == 1 || version_condition.arity == -1)
    version_condition.call(self)
  else
    version_condition
  end          
end

#versioned_attributesObject

Returns an array of attribute keys that are versioned. See non_versioned_columns



313
314
315
# File 'lib/junebug/ext/acts_as_versioned.rb', line 313

def versioned_attributes
  self.attributes.keys.select { |k| !self.class.non_versioned_columns.include?(k) }
end

#without_locking(&block) ⇒ Object

Turns off optimistic locking for the duration of the block

@foo.without_locking do
  @foo.save
end


375
376
377
# File 'lib/junebug/ext/acts_as_versioned.rb', line 375

def without_locking(&block)
  self.class.without_locking(&block)
end

#without_revision(&block) ⇒ Object

Executes the block with the versioning callbacks disabled.

@foo.without_revision do
  @foo.save
end


365
366
367
# File 'lib/junebug/ext/acts_as_versioned.rb', line 365

def without_revision(&block)
  self.class.without_revision(&block)
end