| 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] |
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 177
177: def clear_old_versions
178: return if self.class.max_version_limit.blank?
179: excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit
180: if excess_baggage > 0
181: sql = "DELETE FROM #{self.class.versioned_table_name} WHERE version <= #{excess_baggage} AND #{self.class.versioned_foreign_key} = #{self.id}"
182: self.class.versioned_class.connection.execute sql
183: end
184: end
Clones a model. Used when saving a new version or reverting a model’s version.
# File lib/acts_as_versioned.rb, line 241
241: def clone_versioned_model(orig_model, new_model)
242: self.versioned_attributes.each do |key|
243: new_model.send("#{key}=", orig_model.attributes[key]) if orig_model.attribute_present?(key)
244: end
245:
246: if orig_model.is_a?(self.class.versioned_class)
247: new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column]
248: elsif new_model.is_a?(self.class.versioned_class)
249: new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
250: end
251: end
If called with no parameters, gets whether the current model is dirty and needs to be versioned. If called with a single parameter, gets whether the parameter is currently dirty.
# File lib/acts_as_versioned.rb, line 234
234: def dirty?(attr_name = nil)
235: attr_name.nil? ?
236: (!self.class.track_changed_attributes or (changed_attributes and changed_attributes.length > 0)) :
237: (changed_attributes and changed_attributes.include?(attr_name.to_s))
238: end
Finds a specific version of this model.
# File lib/acts_as_versioned.rb, line 187
187: def find_version(version)
188: return version if version.is_a?(self.class.versioned_class)
189: return nil if version.is_a?(ActiveRecord::Base)
190: find_versions(:conditions => ['version = ?', version], :limit => 1).first
191: end
Finds versions of this model. Takes an options hash like find
# File lib/acts_as_versioned.rb, line 194
194: def find_versions(options = {})
195: versions.find(:all, {:order => 'version'}.merge(options))
196: 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 199
199: def revert_to(version)
200: if version.is_a?(self.class.versioned_class)
201: return false unless version.send(self.class.versioned_foreign_key) == self.id and !version.new_record?
202: else
203: return false unless version = find_version(version)
204: end
205: self.clone_versioned_model(version, self)
206: self.send("#{self.class.version_column}=", version.version)
207: true
208: 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 212
212: def revert_to!(version)
213: revert_to(version) ? save_without_revision : false
214: end
Saves a version of the model if applicable
# File lib/acts_as_versioned.rb, line 162
162: def save_version
163: save_version_on_create if save_version?
164: end
Checks whether a new version shall be saved or not. Calls version_condition_met? and dirty?.
# File lib/acts_as_versioned.rb, line 254
254: def save_version?
255: version_condition_met? and dirty?
256: 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 167
167: def save_version_on_create
168: rev = self.class.versioned_class.new
169: self.clone_versioned_model(self, rev)
170: rev.version = send(self.class.version_column)
171: rev.send("#{self.class.versioned_foreign_key}=", self.id)
172: rev.save
173: 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 217
217: def save_without_revision
218: old_lock_value = ActiveRecord::Base.lock_optimistically
219: ActiveRecord::Base.lock_optimistically = false if old_lock_value
220: disable_acts_as_versioned_callbacks
221: save_result = self.save
222: enable_acts_as_versioned_callbacks
223: ActiveRecord::Base.lock_optimistically = true if old_lock_value
224: save_result
225: 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 260
260: def version_condition_met?
261: case
262: when version_condition.is_a?(Symbol)
263: send(version_condition)
264: when version_condition.respond_to?(:call) && (version_condition.arity == 1 || version_condition.arity == -1)
265: version_condition.call(self)
266: else
267: version_condition
268: end
269: end