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

Defined in:
lib/plugins/acts_as_versioned/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:


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

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.


367
368
369
370
371
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 367

def changed?(attr_name = nil)
  attr_name.nil? ?
    (!self.class.track_altered_attributes || (altered_attributes && altered_attributes.length > 0)) :
    (altered_attributes && altered_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.


313
314
315
316
317
318
319
320
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 313

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.


377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 377

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.respond_to?(key)
  end

					if self.class.columns_hash.include?(self.class.inheritance_column)
    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
end

#empty_callbackObject

:nodoc:


429
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 429

def empty_callback() end

#find_version(version = nil) ⇒ Object

Finds a specific version of this record


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

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


327
328
329
330
331
332
333
334
335
336
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 327

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


340
341
342
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 340

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

#save_versionObject

Saves a version of the model if applicable


298
299
300
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 298

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


392
393
394
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 392

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


303
304
305
306
307
308
309
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 303

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.


345
346
347
348
349
350
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 345

def save_without_revision
  save_without_revision!
  true
rescue
  false
end

#save_without_revision!Object


352
353
354
355
356
357
358
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 352

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.


398
399
400
401
402
403
404
405
406
407
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 398

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


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

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

#versions_countObject


322
323
324
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 322

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

425
426
427
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 425

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

415
416
417
# File 'lib/plugins/acts_as_versioned/lib/acts_as_versioned.rb', line 415

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