Module: ActiveRecord::Acts::Versioned::Behaviors
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/acts_as_versioned.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
- #altered? ⇒ Boolean
-
#clear_old_versions ⇒ Object
Clears old revisions if a limit is set with the :limit option in
acts_as_versioned
. - #clone_inheritance_column(orig_model, new_model) ⇒ Object
-
#clone_versioned_model(orig_model, new_model) ⇒ Object
Clones a model.
- #empty_callback ⇒ Object
-
#revert_to(version) ⇒ Object
Reverts a model to a given version.
-
#revert_to!(version) ⇒ Object
Reverts a model to a given version and saves the model.
- #revert_to_published_version(rev) ⇒ Object
-
#save_version ⇒ Object
Saves a version of the model in the versioned table.
-
#save_version? ⇒ Boolean
Checks whether a new version shall be saved or not.
-
#save_without_revision ⇒ Object
Temporarily turns off Optimistic Locking while saving.
- #save_without_revision! ⇒ Object
-
#version_condition_met? ⇒ Boolean
Checks condition set in the :if option to check whether a revision should be created or not.
-
#without_locking(&block) ⇒ Object
Turns off optimistic locking for the duration of the block.
-
#without_revision(&block) ⇒ Object
Executes the block with the versioning callbacks disabled.
Instance Method Details
#altered? ⇒ Boolean
351 352 353 |
# File 'lib/acts_as_versioned.rb', line 351 def altered? track_altered_attributes ? (version_if_changed - changed).length < version_if_changed.length : changed? end |
#clear_old_versions ⇒ Object
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 |
# 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 self.class.versioned_class.delete_all ["#{self.class.version_column} <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id] end end |
#clone_inheritance_column(orig_model, new_model) ⇒ Object
364 365 366 367 368 369 370 |
# File 'lib/acts_as_versioned.rb', line 364 def clone_inheritance_column(orig_model, new_model) if orig_model.is_a?(self.class.versioned_class) && new_model.class.column_names.include?(new_model.class.inheritance_column.to_s) 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.class.column_names.include?(self.class.versioned_inheritance_column.to_s) new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column] 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.
356 357 358 359 360 361 362 |
# File 'lib/acts_as_versioned.rb', line 356 def clone_versioned_model(orig_model, new_model) self.class.versioned_columns.each do |col| new_model[col.name] = orig_model.send(col.name) if orig_model.has_attribute?(col.name) end clone_inheritance_column(orig_model, new_model) end |
#empty_callback ⇒ Object
410 411 |
# File 'lib/acts_as_versioned.rb', line 410 def empty_callback() end |
#revert_to(version) ⇒ Object
Reverts a model to a given version. Takes either a version number or an instance of the versioned model
317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/acts_as_versioned.rb', line 317 def revert_to(version) if version.is_a?(self.class.versioned_class) return false unless version.send(self.class.versioned_foreign_key) == id and !version.new_record? else return false unless version = versions.where(self.class.version_column => version).first end self.clone_versioned_model(version, self) send("#{self.class.version_column}=", version.send(self.class.version_column)) self.loaded_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
331 332 333 |
# File 'lib/acts_as_versioned.rb', line 331 def revert_to!(version) revert_to(version) ? save_without_revision : false end |
#revert_to_published_version(rev) ⇒ Object
296 297 298 299 300 301 302 303 304 |
# File 'lib/acts_as_versioned.rb', line 296 def revert_to_published_version(rev) if rev.respond_to?(self.class.version_status_column) && ["revision", "submitted_for_approval"].include?(rev.send(self.class.version_status_column)) published_version = self.versions.where(:version_status => 'published').order("version DESC").first unless published_version.blank? self.reload self.revert_to!(published_version) end end end |
#save_version ⇒ Object
Saves a version of the model in the versioned table. This is called in the after_save callback by default
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/acts_as_versioned.rb', line 277 def save_version if @saving_version @saving_version = nil rev = self.class.versioned_class.new clone_versioned_model(self, rev) rev.send("#{self.class.version_column}=", send(self.class.version_column)) rev.send("#{self.class.versioned_foreign_key}=", id) if rev.respond_to?(self.class.version_status_column) rev.send("#{self.class.version_status_column}=", self._publish_status) if self.respond_to?(:_publish_status) rev.send("#{self.class.version_status_column}=", self.state) if self.respond_to?(:state) && rev.send(self.class.version_status_column).blank? end if rev.respond_to?(self.class.version_user_id_column) rev.send("#{self.class.version_user_id_column}=", self.current_user_id) if self.respond_to?(:current_user_id) end rev.save revert_to_published_version(rev) end end |
#save_version? ⇒ Boolean
Checks whether a new version shall be saved or not. Calls version_condition_met?
and changed?
.
373 374 375 |
# File 'lib/acts_as_versioned.rb', line 373 def save_version? version_condition_met? && altered? || (self.respond_to?(:_publish_status) && (self.state_changed?) ) end |
#save_without_revision ⇒ Object
Temporarily turns off Optimistic Locking while saving. Used when reverting so that a new version is not created.
336 337 338 339 340 341 |
# File 'lib/acts_as_versioned.rb', line 336 def save_without_revision save_without_revision! true rescue false end |
#save_without_revision! ⇒ Object
343 344 345 346 347 348 349 |
# File 'lib/acts_as_versioned.rb', line 343 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.
379 380 381 382 383 384 385 386 387 388 |
# File 'lib/acts_as_versioned.rb', line 379 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 |
#without_locking(&block) ⇒ Object
Turns off optimistic locking for the duration of the block
@foo.without_locking do
@foo.save
end
406 407 408 |
# File 'lib/acts_as_versioned.rb', line 406 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
396 397 398 |
# File 'lib/acts_as_versioned.rb', line 396 def without_revision(&block) self.class.without_revision(&block) end |