Alias for AR functions when is extended with this module
# File vendor/plugins/ubiquo_versions/lib/ubiquo_versions/extensions/active_record.rb, line 95
95: def self.extended(klass)
96: klass.class_eval do
97: class << self
98: alias_method_chain :find, :current_version
99: alias_method_chain :count, :current_version
100: end
101: end
102: end
Adds :current_version => true to versionable models unless explicitly said :version option
# File vendor/plugins/ubiquo_versions/lib/ubiquo_versions/extensions/active_record.rb, line 80
80: def count_with_current_version(*args)
81: if @versionable
82: from_version = @find_versions_from_version
83: @find_versions_from_version = nil
84: options = args.extract_options!
85: prepare_options_for_version!(options, from_version)
86:
87: count_without_current_version(args.first || :all, options)
88: else
89: count_without_current_version(*args)
90: end
91:
92: end
Adds :current_version => true to versionable models unless explicitly said :version option
# File vendor/plugins/ubiquo_versions/lib/ubiquo_versions/extensions/active_record.rb, line 66
66: def find_with_current_version(*args)
67: if @versionable
68: from_version = @find_versions_from_version
69: @find_versions_from_version = nil
70: options = args.extract_options!
71: prepare_options_for_version!(options, from_version)
72:
73: find_without_current_version(args.first, options)
74: else
75: find_without_current_version(*args)
76: end
77: end
Given a set of “find” options, this method will update it as follows:
if a :version option exists, and is an integer, a :version_number condition will be added
if a :version option exists, and the value :all, a condition to filter by
version will not be added
by default, a condition to find by :is_current_version = true is added.
If the from_version attribute is an instance of this model, it will look for versions created with
from_version as the original version.
# File vendor/plugins/ubiquo_versions/lib/ubiquo_versions/extensions/active_record.rb, line 113
113: def prepare_options_for_version!(options, from_version)
114: v = options.delete(:version)
115:
116: case v
117: when Fixnum
118: options[:conditions] = merge_conditions(options[:conditions], {:version_number => v})
119: when :all
120: # do nothing...
121: else # not an expected version set. Acts as :last
122: unless from_version
123: options[:conditions] = merge_conditions(options[:conditions], {:is_current_version => true})
124: else
125: options[:conditions] = merge_conditions(options[:conditions],
126: ["#{self.table_name}.content_id = ? AND #{self.table_name}.id != ? AND #{self.table_name}.parent_version = ?",
127: from_version.content_id,
128: from_version.id,
129: from_version.parent_version
130: ]
131: )
132: end
133: end
134: options
135: end
Class method for ActiveRecord that states that a model is versionable
EXAMPLE:
versionable :max_amount => 5
possible options:
:max_amount => number of versions that will be stored as a maximum.
When this limit is reached, the system starts
deleting older versions as required
# File vendor/plugins/ubiquo_versions/lib/ubiquo_versions/extensions/active_record.rb, line 25
25: def versionable(options = {})
26: @versionable = true
27: @versionable_options = options
28: # version_number should not be copied between instances if a model is translatable
29: if respond_to?(:add_translatable_attributes)
30: add_translatable_attributes(:version_number, :is_current_version, :parent_version)
31: end
32:
33: # version_number constitute a translatable scope (should not update old versions)
34: if respond_to?(:add_translatable_scope)
35: add_translatable_scope(
36: lambda do |element|
37: condition = sanitize_sql_for_conditions ["#{self.table_name}.is_current_version = ?", true]
38: # a new version record with old information doesn't have related translations
39: condition += " AND 1=0 " unless element.is_current_version
40: condition
41: end
42: )
43: end
44: named_scope :versions, lambda{ |version|
45: @find_versions_from_version = version
46: {}
47: }
48:
49: # Apply versions named scope to any possible already loaded subclass
50: subclasses.each do |klass|
51: klass.scopes[:versions] = scopes[:versions]
52: end
53:
54: define_method("versions") do
55: self.class.versions(self)
56: end
57:
58: define_method('restore') do |old_version_id|
59: old_version = self.class.find(old_version_id, :version => :all)
60: restored_attributes = old_version.instance_variable_get('@attributes')
61: self.update_attributes restored_attributes.merge(:is_current_version => true)
62: end
63: end
Used to execute a block that would create a version without this effect Note that it will disable versionable just for one time, so the block should only contain one versionable-firing event
# File vendor/plugins/ubiquo_versions/lib/ubiquo_versions/extensions/active_record.rb, line 140
140: def without_versionable
141: @versionable_disabled = true
142: yield
143: end
Disabled; run with $DEBUG to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.