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

Defined in:
lib/acts_as_versioned.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

:nodoc:



283
284
285
# File 'lib/acts_as_versioned.rb', line 283

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

Instance Method Details

#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.



308
309
310
311
312
313
314
315
# File 'lib/acts_as_versioned.rb', line 308

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.



366
367
368
369
370
371
372
373
374
375
376
# File 'lib/acts_as_versioned.rb', line 366

def clone_versioned_model(orig_model, new_model)
  self.versioned_attributes.each do |key|
    new_model.send("#{key}=", orig_model.send(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

#empty_callbackObject

:nodoc:



416
# File 'lib/acts_as_versioned.rb', line 416

def empty_callback() end

#find_version(version = nil) ⇒ Object

Finds a specific version of this record



288
289
290
# File 'lib/acts_as_versioned.rb', line 288

def find_version(version = nil)
  self.class.find_version(id, version)
end

#revert_to(version) ⇒ Object

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



322
323
324
325
326
327
328
329
330
331
# File 'lib/acts_as_versioned.rb', line 322

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 = versions.find_by_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



335
336
337
# File 'lib/acts_as_versioned.rb', line 335

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

#save_versionObject

Saves a version of the model if applicable



293
294
295
# File 'lib/acts_as_versioned.rb', line 293

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)


379
380
381
# File 'lib/acts_as_versioned.rb', line 379

def save_version?
  version_condition_met? && should_version?
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



298
299
300
301
302
303
304
# File 'lib/acts_as_versioned.rb', line 298

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.



340
341
342
343
344
345
# File 'lib/acts_as_versioned.rb', line 340

def save_without_revision
  save_without_revision!
  true
rescue
  false
end

#save_without_revision!Object



347
348
349
350
351
352
353
# File 'lib/acts_as_versioned.rb', line 347

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

#should_version?Boolean

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

Returns:

  • (Boolean)


361
362
363
# File 'lib/acts_as_versioned.rb', line 361

def should_version?
  (!self.class.track_changed_attributes || (tracked_attributes & self.changed).length > 0 )
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)


385
386
387
388
389
390
391
392
393
394
# File 'lib/acts_as_versioned.rb', line 385

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



356
357
358
# File 'lib/acts_as_versioned.rb', line 356

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

#versions_countObject



317
318
319
# File 'lib/acts_as_versioned.rb', line 317

def versions_count
  version
end

#without_locking(&block) ⇒ Object

Turns off optimistic locking for the duration of the block

@foo.without_locking do
  @foo.save
end


412
413
414
# File 'lib/acts_as_versioned.rb', line 412

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


402
403
404
# File 'lib/acts_as_versioned.rb', line 402

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