| Module | ActiveRecord::Acts::Versioned::ActMethods |
| In: |
lib/acts_as_versioned.rb
|
| ACTS_AS_VERSIONED_CALLBACKS | = | [:set_new_version, :save_version_on_create, :save_version, :clear_changed_attributes] |
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.
# File lib/acts_as_versioned.rb, line 243
243: def changed?(attr_name = nil)
244: attr_name.nil? ?
245: (!self.class.track_changed_attributes or (changed_attributes and changed_attributes.length > 0)) :
246: (changed_attributes and changed_attributes.include?(attr_name.to_s))
247: end
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.
# File lib/acts_as_versioned.rb, line 186
186: def clear_old_versions
187: return if self.class.max_version_limit.blank?
188: excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit
189: if excess_baggage > 0
190: sql = "DELETE FROM #{self.class.versioned_table_name} WHERE version <= #{excess_baggage} AND #{self.class.versioned_foreign_key} = #{self.id}"
191: self.class.versioned_class.connection.execute sql
192: end
193: end
Clones a model. Used when saving a new version or reverting a model’s version.
# File lib/acts_as_versioned.rb, line 253
253: def clone_versioned_model(orig_model, new_model)
254: self.versioned_attributes.each do |key|
255: new_model.send("#{key}=", orig_model.attributes[key]) if orig_model.attribute_present?(key)
256: end
257:
258: if orig_model.is_a?(self.class.versioned_class)
259: new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column]
260: elsif new_model.is_a?(self.class.versioned_class)
261: new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
262: end
263: end
Finds a specific version of this model.
# File lib/acts_as_versioned.rb, line 196
196: def find_version(version)
197: return version if version.is_a?(self.class.versioned_class)
198: return nil if version.is_a?(ActiveRecord::Base)
199: find_versions(:conditions => ['version = ?', version], :limit => 1).first
200: end
Finds versions of this model. Takes an options hash like find
# File lib/acts_as_versioned.rb, line 203
203: def find_versions(options = {})
204: versions.find(:all, options)
205: end
Reverts a model to a given version. Takes either a version number or an instance of the versioned model
# File lib/acts_as_versioned.rb, line 208
208: def revert_to(version)
209: if version.is_a?(self.class.versioned_class)
210: return false unless version.send(self.class.versioned_foreign_key) == self.id and !version.new_record?
211: else
212: return false unless version = find_version(version)
213: end
214: self.clone_versioned_model(version, self)
215: self.send("#{self.class.version_column}=", version.version)
216: true
217: end
Reverts a model to a given version and saves the model. Takes either a version number or an instance of the versioned model
# File lib/acts_as_versioned.rb, line 221
221: def revert_to!(version)
222: revert_to(version) ? save_without_revision : false
223: end
Saves a version of the model if applicable
# File lib/acts_as_versioned.rb, line 171
171: def save_version
172: save_version_on_create if save_version?
173: end
Checks whether a new version shall be saved or not. Calls version_condition_met? and changed?.
# File lib/acts_as_versioned.rb, line 266
266: def save_version?
267: version_condition_met? and changed?
268: end
Saves a version of the model in the versioned table. This is called in the after_save callback by default
# File lib/acts_as_versioned.rb, line 176
176: def save_version_on_create
177: rev = self.class.versioned_class.new
178: self.clone_versioned_model(self, rev)
179: rev.version = send(self.class.version_column)
180: rev.send("#{self.class.versioned_foreign_key}=", self.id)
181: rev.save
182: end
Temporarily turns off Optimistic Locking while saving. Used when reverting so that a new version is not created.
# File lib/acts_as_versioned.rb, line 226
226: def save_without_revision
227: old_lock_value = ActiveRecord::Base.lock_optimistically
228: ActiveRecord::Base.lock_optimistically = false if old_lock_value
229: disable_acts_as_versioned_callbacks
230: save_result = self.save
231: enable_acts_as_versioned_callbacks
232: ActiveRecord::Base.lock_optimistically = true if old_lock_value
233: save_result
234: end
Checks condition set in the :if option to check whether a revision should be created or not. Override this for custom version condition checking.
# File lib/acts_as_versioned.rb, line 272
272: def version_condition_met?
273: case
274: when version_condition.is_a?(Symbol)
275: send(version_condition)
276: when version_condition.respond_to?(:call) && (version_condition.arity == 1 || version_condition.arity == -1)
277: version_condition.call(self)
278: else
279: version_condition
280: end
281: end