Module: ActiveRecord::Acts::Versioned::ActMethods
- Defined in:
- lib/rtiss_acts_as_versioned.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
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
:nodoc:.
-
#find_newest_version ⇒ Object
# Alte finder-Syntax ab Rails 5 nicht mehr verwendbar.
- #find_version(version) ⇒ Object
- #highest_version ⇒ 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.
-
#save_version(save_this = false, deleted_flag = false, restored_from_version = nil) ⇒ 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(perform_validation = true) ⇒ Object
Temporarily turns off Optimistic Locking while saving.
- #save_without_revision! ⇒ Object
- #set_deleted_flag ⇒ 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.
Class Method Details
.included(base) ⇒ Object
:nodoc:
315 316 317 |
# File 'lib/rtiss_acts_as_versioned.rb', line 315 def self.included(base) # :nodoc: base.extend ClassMethods end |
Instance Method Details
#altered? ⇒ Boolean
407 408 409 |
# File 'lib/rtiss_acts_as_versioned.rb', line 407 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.
353 354 355 356 357 358 359 |
# File 'lib/rtiss_acts_as_versioned.rb', line 353 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
420 421 422 423 424 425 426 |
# File 'lib/rtiss_acts_as_versioned.rb', line 420 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.
412 413 414 415 416 417 418 |
# File 'lib/rtiss_acts_as_versioned.rb', line 412 def clone_versioned_model(orig_model, new_model) self.class.versioned_columns.each do |col| new_model.send("#{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
:nodoc:
466 |
# File 'lib/rtiss_acts_as_versioned.rb', line 466 def empty_callback() end |
#find_newest_version ⇒ Object
# Alte finder-Syntax ab Rails 5 nicht mehr verwendbar. # Assoziation versions stattdessen verwenden
def find_versions(*args)
return [] if self.id.nil?
= args.
version_condition = "#{self.class.versioned_foreign_key} = #{self.id}"
if [:conditions]
[:conditions] += " and #{version_condition}"
else
[:conditions] = version_condition
end
if args.first.is_a?(Symbol)
versions.find(args.first, )
else # TODO: is_a?(Fixnum)
versions.find()
end
end
489 490 491 492 493 |
# File 'lib/rtiss_acts_as_versioned.rb', line 489 def find_newest_version return nil if self.id.nil? self.class.versioned_class.where("#{self.class.versioned_foreign_key} = #{self.id}").order("version DESC").first end |
#find_version(version) ⇒ Object
504 505 506 507 508 509 510 |
# File 'lib/rtiss_acts_as_versioned.rb', line 504 def find_version(version) return nil if self.id.nil? ret = self.class.versioned_class.where("#{self.class.versioned_foreign_key} = #{self.id} and #{self.class.version_column}=#{version}").first raise "find_version: version #{version} not found in database" unless ret ret end |
#highest_version ⇒ Object
495 496 497 498 499 500 501 502 |
# File 'lib/rtiss_acts_as_versioned.rb', line 495 def highest_version v = find_newest_version if v then v.version else -1 end end |
#revert_to(version) ⇒ Object
Reverts a model to a given version. Takes either a version number or an instance of the versioned model
362 363 364 365 366 367 368 369 370 371 372 373 374 |
# File 'lib/rtiss_acts_as_versioned.rb', line 362 def revert_to(version) if version.is_a?(self.class.versioned_class) @reverted_from = version.send(self.class.version_column) return false unless version.send(self.class.versioned_foreign_key) == id and !version.new_record? else @reverted_from = version version = versions.where(self.class.version_column => version).first return false unless version end self.clone_versioned_model(version, self) send("#{self.class.version_column}=", version.send(self.class.version_column)) 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
378 379 380 381 382 383 384 385 386 |
# File 'lib/rtiss_acts_as_versioned.rb', line 378 def revert_to!(version) if revert_to(version) set_new_version save_without_revision save_version(true, false, @reverted_from) else false end end |
#save_version(save_this = false, deleted_flag = false, restored_from_version = nil) ⇒ Object
Saves a version of the model in the versioned table. This is called in the after_save callback by default
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/rtiss_acts_as_versioned.rb', line 320 def save_version(save_this=false, deleted_flag=false, restored_from_version=nil) if @saving_version || save_this @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) rev.send("#{self.class.deleted_in_original_table_flag}=", deleted_flag) rev.send("#{self.class.record_restored_column}=", restored_from_version) if rev.respond_to? :updated_at= rev.updated_at = Time.now end rev.save end end |
#save_version? ⇒ Boolean
Checks whether a new version shall be saved or not. Calls version_condition_met? and changed?.
429 430 431 |
# File 'lib/rtiss_acts_as_versioned.rb', line 429 def save_version? version_condition_met? && altered? end |
#save_without_revision(perform_validation = true) ⇒ Object
Temporarily turns off Optimistic Locking while saving. Used when reverting so that a new version is not created.
389 390 391 392 393 394 395 396 397 |
# File 'lib/rtiss_acts_as_versioned.rb', line 389 def save_without_revision(perform_validation = true) ret = false without_locking do without_revision do ret = save(:validate => perform_validation) end end return ret end |
#save_without_revision! ⇒ Object
399 400 401 402 403 404 405 |
# File 'lib/rtiss_acts_as_versioned.rb', line 399 def save_without_revision! without_locking do without_revision do save! end end end |
#set_deleted_flag ⇒ Object
336 337 338 339 340 341 342 343 344 345 346 347 348 349 |
# File 'lib/rtiss_acts_as_versioned.rb', line 336 def set_deleted_flag return if self.id.nil? rev = self.class.versioned_class.new clone_versioned_model(self, rev) rev.send("#{self.class.version_column}=", highest_version+1) rev.send("#{self.class.versioned_foreign_key}=", id) rev.send("#{self.class.deleted_in_original_table_flag}=", true) rev.send("#{self.class.record_restored_column}=", nil) if rev.respond_to? :updated_at= rev.updated_at = Time.now end rev.save 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.
435 436 437 438 439 440 441 442 443 444 |
# File 'lib/rtiss_acts_as_versioned.rb', line 435 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
462 463 464 |
# File 'lib/rtiss_acts_as_versioned.rb', line 462 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
452 453 454 |
# File 'lib/rtiss_acts_as_versioned.rb', line 452 def without_revision(&block) self.class.without_revision(&block) end |