Commit 3d6c66d50936b34772c0e47e394d46efe24cffd1
Committed by
Antonio Terceiro
1 parent
5c9d49f0
Exists in
master
and in
28 other branches
Making the user unit test pass
Showing
12 changed files
with
259 additions
and
240 deletions
Show diff stats
db/schema.rb
1 | # This file is auto-generated from the current state of the database. Instead of editing this file, | 1 | # This file is auto-generated from the current state of the database. Instead of editing this file, |
2 | -# please use the migrations feature of ActiveRecord to incrementally modify your database, and | 2 | +# please use the migrations feature of Active Record to incrementally modify your database, and |
3 | # then regenerate this schema definition. | 3 | # then regenerate this schema definition. |
4 | # | 4 | # |
5 | # Note that this schema.rb definition is the authoritative source for your database schema. If you need | 5 | # Note that this schema.rb definition is the authoritative source for your database schema. If you need |
@@ -131,8 +131,8 @@ ActiveRecord::Schema.define(:version => 69) do | @@ -131,8 +131,8 @@ ActiveRecord::Schema.define(:version => 69) do | ||
131 | t.boolean "virtual", :default => false | 131 | t.boolean "virtual", :default => false |
132 | end | 132 | end |
133 | 133 | ||
134 | - add_index "categories_profiles", ["category_id"], :name => "index_categories_profiles_on_category_id" | ||
135 | add_index "categories_profiles", ["profile_id"], :name => "index_categories_profiles_on_profile_id" | 134 | add_index "categories_profiles", ["profile_id"], :name => "index_categories_profiles_on_profile_id" |
135 | + add_index "categories_profiles", ["category_id"], :name => "index_categories_profiles_on_category_id" | ||
136 | 136 | ||
137 | create_table "comments", :force => true do |t| | 137 | create_table "comments", :force => true do |t| |
138 | t.string "title" | 138 | t.string "title" |
@@ -211,8 +211,8 @@ ActiveRecord::Schema.define(:version => 69) do | @@ -211,8 +211,8 @@ ActiveRecord::Schema.define(:version => 69) do | ||
211 | t.datetime "updated_at" | 211 | t.datetime "updated_at" |
212 | end | 212 | end |
213 | 213 | ||
214 | - add_index "product_categorizations", ["category_id"], :name => "index_product_categorizations_on_category_id" | ||
215 | add_index "product_categorizations", ["product_id"], :name => "index_product_categorizations_on_product_id" | 214 | add_index "product_categorizations", ["product_id"], :name => "index_product_categorizations_on_product_id" |
215 | + add_index "product_categorizations", ["category_id"], :name => "index_product_categorizations_on_category_id" | ||
216 | 216 | ||
217 | create_table "products", :force => true do |t| | 217 | create_table "products", :force => true do |t| |
218 | t.integer "enterprise_id" | 218 | t.integer "enterprise_id" |
test/unit/user_test.rb
@@ -164,7 +164,8 @@ class UserTest < Test::Unit::TestCase | @@ -164,7 +164,8 @@ class UserTest < Test::Unit::TestCase | ||
164 | 164 | ||
165 | def test_should_encrypt_password_with_salted_sha1 | 165 | def test_should_encrypt_password_with_salted_sha1 |
166 | user = User.new(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test') | 166 | user = User.new(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test') |
167 | - user.build_person(person_data) | 167 | +#TODO UPGRADE Leandro: I comment this code. The user model already create a person model |
168 | +# user.build_person(person_data) | ||
168 | user.stubs(:salt).returns('testsalt') | 169 | user.stubs(:salt).returns('testsalt') |
169 | user.save! | 170 | user.save! |
170 | 171 | ||
@@ -295,7 +296,8 @@ class UserTest < Test::Unit::TestCase | @@ -295,7 +296,8 @@ class UserTest < Test::Unit::TestCase | ||
295 | protected | 296 | protected |
296 | def new_user(options = {}) | 297 | def new_user(options = {}) |
297 | user = User.new({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options)) | 298 | user = User.new({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options)) |
298 | - user.build_person(person_data) | 299 | +#TODO UPGRADE Leandro: I comment this code. The user model already create a person model |
300 | +# user.build_person(person_data) | ||
299 | user.save | 301 | user.save |
300 | user | 302 | user |
301 | end | 303 | end |
vendor/plugins/acts_as_versioned/CHANGELOG
1 | -*SVN* (version numbers are overrated) | 1 | +*GIT* (version numbers are overrated) |
2 | + | ||
3 | +* (16 Jun 2008) Backwards Compatibility is overrated (big updates for rails 2.1) | ||
4 | + | ||
5 | + * Use ActiveRecord 2.1's dirty attribute checking instead [Asa Calow] | ||
6 | + * Remove last traces of #non_versioned_fields | ||
7 | + * Remove AR::Base.find_version and AR::Base.find_versions, rely on AR association proxies and named_scope | ||
8 | + * Remove #versions_count, rely on AR association counter caching. | ||
9 | + * Remove #versioned_attributes, basically the same as AR::Base.versioned_columns | ||
2 | 10 | ||
3 | * (5 Oct 2006) Allow customization of #versions association options [Dan Peterson] | 11 | * (5 Oct 2006) Allow customization of #versions association options [Dan Peterson] |
4 | 12 |
vendor/plugins/acts_as_versioned/Rakefile
vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb
@@ -66,7 +66,7 @@ module ActiveRecord #:nodoc: | @@ -66,7 +66,7 @@ module ActiveRecord #:nodoc: | ||
66 | # | 66 | # |
67 | # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options | 67 | # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options |
68 | module Versioned | 68 | module Versioned |
69 | - CALLBACKS = [:set_new_version, :save_version_on_create, :save_version?, :clear_changed_attributes] | 69 | + CALLBACKS = [:set_new_version, :save_version, :save_version?] |
70 | def self.included(base) # :nodoc: | 70 | def self.included(base) # :nodoc: |
71 | base.extend ClassMethods | 71 | base.extend ClassMethods |
72 | end | 72 | end |
@@ -95,12 +95,7 @@ module ActiveRecord #:nodoc: | @@ -95,12 +95,7 @@ module ActiveRecord #:nodoc: | ||
95 | # end | 95 | # end |
96 | # | 96 | # |
97 | # * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes | 97 | # * <tt>if_changed</tt> - Simple way of specifying attributes that are required to be changed before saving a model. This takes |
98 | - # either a symbol or array of symbols. WARNING - This will attempt to overwrite any attribute setters you may have. | ||
99 | - # Use this instead if you want to write your own attribute setters (and ignore if_changed): | ||
100 | - # | ||
101 | - # def name=(new_name) | ||
102 | - # write_changed_attribute :name, new_name | ||
103 | - # end | 98 | + # either a symbol or array of symbols. |
104 | # | 99 | # |
105 | # * <tt>extend</tt> - Lets you specify a module to be mixed in both the original and versioned models. You can also just pass a block | 100 | # * <tt>extend</tt> - Lets you specify a module to be mixed in both the original and versioned models. You can also just pass a block |
106 | # to create an anonymous mixin: | 101 | # to create an anonymous mixin: |
@@ -148,7 +143,7 @@ module ActiveRecord #:nodoc: | @@ -148,7 +143,7 @@ module ActiveRecord #:nodoc: | ||
148 | # # that create_table does | 143 | # # that create_table does |
149 | # Post.create_versioned_table | 144 | # Post.create_versioned_table |
150 | # end | 145 | # end |
151 | - # | 146 | + # |
152 | # def self.down | 147 | # def self.down |
153 | # Post.drop_versioned_table | 148 | # Post.drop_versioned_table |
154 | # end | 149 | # end |
@@ -172,21 +167,10 @@ module ActiveRecord #:nodoc: | @@ -172,21 +167,10 @@ module ActiveRecord #:nodoc: | ||
172 | return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods) | 167 | return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods) |
173 | 168 | ||
174 | send :include, ActiveRecord::Acts::Versioned::ActMethods | 169 | send :include, ActiveRecord::Acts::Versioned::ActMethods |
175 | - | ||
176 | - cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column, | ||
177 | - :version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name, :non_versioned_columns, | ||
178 | - :version_association_options | ||
179 | - | ||
180 | - # legacy | ||
181 | - alias_method :non_versioned_fields, :non_versioned_columns | ||
182 | - alias_method :non_versioned_fields=, :non_versioned_columns= | ||
183 | 170 | ||
184 | - class << self | ||
185 | - alias_method :non_versioned_fields, :non_versioned_columns | ||
186 | - alias_method :non_versioned_fields=, :non_versioned_columns= | ||
187 | - end | ||
188 | - | ||
189 | - send :attr_accessor, :changed_attributes | 171 | + cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column, |
172 | + :version_column, :max_version_limit, :track_altered_attributes, :version_condition, :version_sequence_name, :non_versioned_columns, | ||
173 | + :version_association_options, :version_if_changed | ||
190 | 174 | ||
191 | self.versioned_class_name = options[:class_name] || "Version" | 175 | self.versioned_class_name = options[:class_name] || "Version" |
192 | self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key | 176 | self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key |
@@ -196,11 +180,10 @@ module ActiveRecord #:nodoc: | @@ -196,11 +180,10 @@ module ActiveRecord #:nodoc: | ||
196 | self.version_sequence_name = options[:sequence_name] | 180 | self.version_sequence_name = options[:sequence_name] |
197 | self.max_version_limit = options[:limit].to_i | 181 | self.max_version_limit = options[:limit].to_i |
198 | self.version_condition = options[:if] || true | 182 | self.version_condition = options[:if] || true |
199 | - self.non_versioned_columns = [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column] | 183 | + self.non_versioned_columns = [self.primary_key, inheritance_column, self.version_column, 'lock_version', versioned_inheritance_column] + options[:non_versioned_columns].to_a.map(&:to_s) |
200 | self.version_association_options = { | 184 | self.version_association_options = { |
201 | :class_name => "#{self.to_s}::#{versioned_class_name}", | 185 | :class_name => "#{self.to_s}::#{versioned_class_name}", |
202 | :foreign_key => versioned_foreign_key, | 186 | :foreign_key => versioned_foreign_key, |
203 | - :order => 'version', | ||
204 | :dependent => :delete_all | 187 | :dependent => :delete_all |
205 | }.merge(options[:association_options] || {}) | 188 | }.merge(options[:association_options] || {}) |
206 | 189 | ||
@@ -209,40 +192,34 @@ module ActiveRecord #:nodoc: | @@ -209,40 +192,34 @@ module ActiveRecord #:nodoc: | ||
209 | silence_warnings do | 192 | silence_warnings do |
210 | self.const_set(extension_module_name, Module.new(&extension)) | 193 | self.const_set(extension_module_name, Module.new(&extension)) |
211 | end | 194 | end |
212 | - | 195 | + |
213 | options[:extend] = self.const_get(extension_module_name) | 196 | options[:extend] = self.const_get(extension_module_name) |
214 | end | 197 | end |
215 | 198 | ||
216 | - class_eval do | 199 | + class_eval <<-CLASS_METHODS |
217 | has_many :versions, version_association_options do | 200 | has_many :versions, version_association_options do |
218 | # finds earliest version of this record | 201 | # finds earliest version of this record |
219 | def earliest | 202 | def earliest |
220 | - @earliest ||= find(:first) | 203 | + @earliest ||= find(:first, :order => '#{version_column}') |
221 | end | 204 | end |
222 | - | 205 | + |
223 | # find latest version of this record | 206 | # find latest version of this record |
224 | def latest | 207 | def latest |
225 | - @latest ||= find(:first, :order => 'version desc') | 208 | + @latest ||= find(:first, :order => '#{version_column} desc') |
226 | end | 209 | end |
227 | end | 210 | end |
228 | before_save :set_new_version | 211 | before_save :set_new_version |
229 | - after_create :save_version_on_create | ||
230 | - after_update :save_version | 212 | + after_save :save_version |
231 | after_save :clear_old_versions | 213 | after_save :clear_old_versions |
232 | - after_save :clear_changed_attributes | ||
233 | - | 214 | + |
234 | unless options[:if_changed].nil? | 215 | unless options[:if_changed].nil? |
235 | - self.track_changed_attributes = true | 216 | + self.track_altered_attributes = true |
236 | options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array) | 217 | options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array) |
237 | - options[:if_changed].each do |attr_name| | ||
238 | - define_method("#{attr_name}=") do |value| | ||
239 | - write_changed_attribute attr_name, value | ||
240 | - end | ||
241 | - end | 218 | + self.version_if_changed = options[:if_changed].map(&:to_s) |
242 | end | 219 | end |
243 | - | 220 | + |
244 | include options[:extend] if options[:extend].is_a?(Module) | 221 | include options[:extend] if options[:extend].is_a?(Module) |
245 | - end | 222 | + CLASS_METHODS |
246 | 223 | ||
247 | # create the dynamic versioned model | 224 | # create the dynamic versioned model |
248 | const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do | 225 | const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do |
@@ -252,22 +229,26 @@ module ActiveRecord #:nodoc: | @@ -252,22 +229,26 @@ module ActiveRecord #:nodoc: | ||
252 | find :first, :order => 'version desc', | 229 | find :first, :order => 'version desc', |
253 | :conditions => ["#{original_class.versioned_foreign_key} = ? and version < ?", version.send(original_class.versioned_foreign_key), version.version] | 230 | :conditions => ["#{original_class.versioned_foreign_key} = ? and version < ?", version.send(original_class.versioned_foreign_key), version.version] |
254 | end | 231 | end |
255 | - | 232 | + |
256 | # find first version after the given version. | 233 | # find first version after the given version. |
257 | def self.after(version) | 234 | def self.after(version) |
258 | find :first, :order => 'version', | 235 | find :first, :order => 'version', |
259 | :conditions => ["#{original_class.versioned_foreign_key} = ? and version > ?", version.send(original_class.versioned_foreign_key), version.version] | 236 | :conditions => ["#{original_class.versioned_foreign_key} = ? and version > ?", version.send(original_class.versioned_foreign_key), version.version] |
260 | end | 237 | end |
261 | - | 238 | + |
262 | def previous | 239 | def previous |
263 | self.class.before(self) | 240 | self.class.before(self) |
264 | end | 241 | end |
265 | - | 242 | + |
266 | def next | 243 | def next |
267 | self.class.after(self) | 244 | self.class.after(self) |
268 | end | 245 | end |
246 | + | ||
247 | + def versions_count | ||
248 | + page.version | ||
249 | + end | ||
269 | end | 250 | end |
270 | - | 251 | + |
271 | versioned_class.cattr_accessor :original_class | 252 | versioned_class.cattr_accessor :original_class |
272 | versioned_class.original_class = self | 253 | versioned_class.original_class = self |
273 | versioned_class.set_table_name versioned_table_name | 254 | versioned_class.set_table_name versioned_table_name |
@@ -278,24 +259,22 @@ module ActiveRecord #:nodoc: | @@ -278,24 +259,22 @@ module ActiveRecord #:nodoc: | ||
278 | versioned_class.set_sequence_name version_sequence_name if version_sequence_name | 259 | versioned_class.set_sequence_name version_sequence_name if version_sequence_name |
279 | end | 260 | end |
280 | end | 261 | end |
281 | - | 262 | + |
282 | module ActMethods | 263 | module ActMethods |
283 | def self.included(base) # :nodoc: | 264 | def self.included(base) # :nodoc: |
284 | base.extend ClassMethods | 265 | base.extend ClassMethods |
285 | end | 266 | end |
286 | - | ||
287 | - # Saves a version of the model if applicable | ||
288 | - def save_version | ||
289 | - save_version_on_create if save_version? | ||
290 | - end | ||
291 | - | 267 | + |
292 | # Saves a version of the model in the versioned table. This is called in the after_save callback by default | 268 | # Saves a version of the model in the versioned table. This is called in the after_save callback by default |
293 | - def save_version_on_create | ||
294 | - rev = self.class.versioned_class.new | ||
295 | - self.clone_versioned_model(self, rev) | ||
296 | - rev.version = send(self.class.version_column) | ||
297 | - rev.send("#{self.class.versioned_foreign_key}=", self.id) | ||
298 | - rev.save | 269 | + def save_version |
270 | + if @saving_version | ||
271 | + @saving_version = nil | ||
272 | + rev = self.class.versioned_class.new | ||
273 | + clone_versioned_model(self, rev) | ||
274 | + rev.send("#{self.class.version_column}=", send(self.class.version_column)) | ||
275 | + rev.send("#{self.class.versioned_foreign_key}=", id) | ||
276 | + rev.save | ||
277 | + end | ||
299 | end | 278 | end |
300 | 279 | ||
301 | # Clears old revisions if a limit is set with the :limit option in <tt>acts_as_versioned</tt>. | 280 | # Clears old revisions if a limit is set with the :limit option in <tt>acts_as_versioned</tt>. |
@@ -304,24 +283,23 @@ module ActiveRecord #:nodoc: | @@ -304,24 +283,23 @@ module ActiveRecord #:nodoc: | ||
304 | return if self.class.max_version_limit == 0 | 283 | return if self.class.max_version_limit == 0 |
305 | excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit | 284 | excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit |
306 | if excess_baggage > 0 | 285 | if excess_baggage > 0 |
307 | - sql = "DELETE FROM #{self.class.versioned_table_name} WHERE version <= #{excess_baggage} AND #{self.class.versioned_foreign_key} = #{self.id}" | ||
308 | - self.class.versioned_class.connection.execute sql | 286 | + self.class.versioned_class.delete_all ["#{self.class.version_column} <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id] |
309 | end | 287 | end |
310 | end | 288 | end |
311 | 289 | ||
312 | # Reverts a model to a given version. Takes either a version number or an instance of the versioned model | 290 | # Reverts a model to a given version. Takes either a version number or an instance of the versioned model |
313 | def revert_to(version) | 291 | def revert_to(version) |
314 | if version.is_a?(self.class.versioned_class) | 292 | if version.is_a?(self.class.versioned_class) |
315 | - return false unless version.send(self.class.versioned_foreign_key) == self.id and !version.new_record? | 293 | + return false unless version.send(self.class.versioned_foreign_key) == id and !version.new_record? |
316 | else | 294 | else |
317 | - return false unless version = versions.find_by_version(version) | 295 | + return false unless version = versions.send("find_by_#{self.class.version_column}", version) |
318 | end | 296 | end |
319 | self.clone_versioned_model(version, self) | 297 | self.clone_versioned_model(version, self) |
320 | - self.send("#{self.class.version_column}=", version.version) | 298 | + send("#{self.class.version_column}=", version.send(self.class.version_column)) |
321 | true | 299 | true |
322 | end | 300 | end |
323 | 301 | ||
324 | - # Reverts a model to a given version and saves the model. | 302 | + # Reverts a model to a given version and saves the model. |
325 | # Takes either a version number or an instance of the versioned model | 303 | # Takes either a version number or an instance of the versioned model |
326 | def revert_to!(version) | 304 | def revert_to!(version) |
327 | revert_to(version) ? save_without_revision : false | 305 | revert_to(version) ? save_without_revision : false |
@@ -342,41 +320,29 @@ module ActiveRecord #:nodoc: | @@ -342,41 +320,29 @@ module ActiveRecord #:nodoc: | ||
342 | end | 320 | end |
343 | end | 321 | end |
344 | end | 322 | end |
345 | - | ||
346 | - # Returns an array of attribute keys that are versioned. See non_versioned_columns | ||
347 | - def versioned_attributes | ||
348 | - self.attributes.keys.select { |k| !self.class.non_versioned_columns.include?(k) } | ||
349 | - end | ||
350 | 323 | ||
351 | - # If called with no parameters, gets whether the current model has changed and needs to be versioned. | ||
352 | - # If called with a single parameter, gets whether the parameter has changed. | ||
353 | - def changed?(attr_name = nil) | ||
354 | - attr_name.nil? ? | ||
355 | - (!self.class.track_changed_attributes || (changed_attributes && changed_attributes.length > 0)) : | ||
356 | - (changed_attributes && changed_attributes.include?(attr_name.to_s)) | 324 | + def altered? |
325 | + track_altered_attributes ? (version_if_changed - changed).length < version_if_changed.length : changed? | ||
357 | end | 326 | end |
358 | - | ||
359 | - # keep old dirty? method | ||
360 | - alias_method :dirty?, :changed? | ||
361 | - | 327 | + |
362 | # Clones a model. Used when saving a new version or reverting a model's version. | 328 | # Clones a model. Used when saving a new version or reverting a model's version. |
363 | def clone_versioned_model(orig_model, new_model) | 329 | def clone_versioned_model(orig_model, new_model) |
364 | - self.versioned_attributes.each do |key| | ||
365 | - new_model.send("#{key}=", orig_model.send(key)) if orig_model.has_attribute?(key) | 330 | + self.class.versioned_columns.each do |col| |
331 | + new_model.send("#{col.name}=", orig_model.send(col.name)) if orig_model.has_attribute?(col.name) | ||
366 | end | 332 | end |
367 | - | 333 | + |
368 | if orig_model.is_a?(self.class.versioned_class) | 334 | if orig_model.is_a?(self.class.versioned_class) |
369 | new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column] | 335 | new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column] |
370 | elsif new_model.is_a?(self.class.versioned_class) | 336 | elsif new_model.is_a?(self.class.versioned_class) |
371 | new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column] | 337 | new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column] |
372 | end | 338 | end |
373 | end | 339 | end |
374 | - | 340 | + |
375 | # Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>. | 341 | # Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>. |
376 | def save_version? | 342 | def save_version? |
377 | - version_condition_met? && changed? | 343 | + version_condition_met? && altered? |
378 | end | 344 | end |
379 | - | 345 | + |
380 | # Checks condition set in the :if option to check whether a revision should be created or not. Override this for | 346 | # Checks condition set in the :if option to check whether a revision should be created or not. Override this for |
381 | # custom version condition checking. | 347 | # custom version condition checking. |
382 | def version_condition_met? | 348 | def version_condition_met? |
@@ -387,7 +353,7 @@ module ActiveRecord #:nodoc: | @@ -387,7 +353,7 @@ module ActiveRecord #:nodoc: | ||
387 | version_condition.call(self) | 353 | version_condition.call(self) |
388 | else | 354 | else |
389 | version_condition | 355 | version_condition |
390 | - end | 356 | + end |
391 | end | 357 | end |
392 | 358 | ||
393 | # Executes the block with the versioning callbacks disabled. | 359 | # Executes the block with the versioning callbacks disabled. |
@@ -412,50 +378,24 @@ module ActiveRecord #:nodoc: | @@ -412,50 +378,24 @@ module ActiveRecord #:nodoc: | ||
412 | 378 | ||
413 | def empty_callback() end #:nodoc: | 379 | def empty_callback() end #:nodoc: |
414 | 380 | ||
415 | - protected | 381 | + protected |
416 | # sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version. | 382 | # sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version. |
417 | def set_new_version | 383 | def set_new_version |
418 | - self.send("#{self.class.version_column}=", self.next_version) if new_record? || (!locking_enabled? && save_version?) | 384 | + @saving_version = new_record? || save_version? |
385 | + self.send("#{self.class.version_column}=", next_version) if new_record? || (!locking_enabled? && save_version?) | ||
419 | end | 386 | end |
420 | - | 387 | + |
421 | # Gets the next available version for the current record, or 1 for a new record | 388 | # Gets the next available version for the current record, or 1 for a new record |
422 | def next_version | 389 | def next_version |
423 | - return 1 if new_record? | ||
424 | - (versions.calculate(:max, :version) || 0) + 1 | ||
425 | - end | ||
426 | - | ||
427 | - # clears current changed attributes. Called after save. | ||
428 | - def clear_changed_attributes | ||
429 | - self.changed_attributes = [] | ||
430 | - end | ||
431 | - | ||
432 | - def write_changed_attribute(attr_name, attr_value) | ||
433 | - # Convert to db type for comparison. Avoids failing Float<=>String comparisons. | ||
434 | - attr_value_for_db = self.class.columns_hash[attr_name.to_s].type_cast(attr_value) | ||
435 | - (self.changed_attributes ||= []) << attr_name.to_s unless self.changed?(attr_name) || self.send(attr_name) == attr_value_for_db | ||
436 | - write_attribute(attr_name, attr_value_for_db) | 390 | + (new_record? ? 0 : versions.calculate(:max, version_column).to_i) + 1 |
437 | end | 391 | end |
438 | 392 | ||
439 | module ClassMethods | 393 | module ClassMethods |
440 | - # Finds a specific version of a specific row of this model | ||
441 | - def find_version(id, version) | ||
442 | - find_versions(id, | ||
443 | - :conditions => ["#{versioned_foreign_key} = ? AND version = ?", id, version], | ||
444 | - :limit => 1).first | ||
445 | - end | ||
446 | - | ||
447 | - # Finds versions of a specific model. Takes an options hash like <tt>find</tt> | ||
448 | - def find_versions(id, options = {}) | ||
449 | - versioned_class.find :all, { | ||
450 | - :conditions => ["#{versioned_foreign_key} = ?", id], | ||
451 | - :order => 'version' }.merge(options) | ||
452 | - end | ||
453 | - | ||
454 | # Returns an array of columns that are versioned. See non_versioned_columns | 394 | # Returns an array of columns that are versioned. See non_versioned_columns |
455 | def versioned_columns | 395 | def versioned_columns |
456 | - self.columns.select { |c| !non_versioned_columns.include?(c.name) } | 396 | + @versioned_columns ||= columns.select { |c| !non_versioned_columns.include?(c.name) } |
457 | end | 397 | end |
458 | - | 398 | + |
459 | # Returns an instance of the dynamic versioned model | 399 | # Returns an instance of the dynamic versioned model |
460 | def versioned_class | 400 | def versioned_class |
461 | const_get versioned_class_name | 401 | const_get versioned_class_name |
@@ -464,43 +404,42 @@ module ActiveRecord #:nodoc: | @@ -464,43 +404,42 @@ module ActiveRecord #:nodoc: | ||
464 | # Rake migration task to create the versioned table using options passed to acts_as_versioned | 404 | # Rake migration task to create the versioned table using options passed to acts_as_versioned |
465 | def create_versioned_table(create_table_options = {}) | 405 | def create_versioned_table(create_table_options = {}) |
466 | # create version column in main table if it does not exist | 406 | # create version column in main table if it does not exist |
467 | - if !self.content_columns.find { |c| %w(version lock_version).include? c.name } | ||
468 | - self.connection.add_column table_name, :version, :integer | 407 | + if !self.content_columns.find { |c| [version_column.to_s, 'lock_version'].include? c.name } |
408 | + self.connection.add_column table_name, version_column, :integer | ||
409 | + self.reset_column_information | ||
469 | end | 410 | end |
411 | + | ||
412 | + return if connection.table_exists?(versioned_table_name) | ||
470 | 413 | ||
471 | self.connection.create_table(versioned_table_name, create_table_options) do |t| | 414 | self.connection.create_table(versioned_table_name, create_table_options) do |t| |
472 | t.column versioned_foreign_key, :integer | 415 | t.column versioned_foreign_key, :integer |
473 | - t.column :version, :integer | 416 | + t.column version_column, :integer |
474 | end | 417 | end |
475 | - | ||
476 | - updated_col = nil | 418 | + |
477 | self.versioned_columns.each do |col| | 419 | self.versioned_columns.each do |col| |
478 | - updated_col = col if !updated_col && %(updated_at updated_on).include?(col.name) | ||
479 | self.connection.add_column versioned_table_name, col.name, col.type, | 420 | self.connection.add_column versioned_table_name, col.name, col.type, |
480 | - :limit => col.limit, | ||
481 | - :default => col.default, | ||
482 | - :scale => col.scale, | 421 | + :limit => col.limit, |
422 | + :default => col.default, | ||
423 | + :scale => col.scale, | ||
483 | :precision => col.precision | 424 | :precision => col.precision |
484 | end | 425 | end |
485 | - | 426 | + |
486 | if type_col = self.columns_hash[inheritance_column] | 427 | if type_col = self.columns_hash[inheritance_column] |
487 | self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type, | 428 | self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type, |
488 | - :limit => type_col.limit, | ||
489 | - :default => type_col.default, | ||
490 | - :scale => type_col.scale, | 429 | + :limit => type_col.limit, |
430 | + :default => type_col.default, | ||
431 | + :scale => type_col.scale, | ||
491 | :precision => type_col.precision | 432 | :precision => type_col.precision |
492 | end | 433 | end |
493 | - | ||
494 | - if updated_col.nil? | ||
495 | - self.connection.add_column versioned_table_name, :updated_at, :timestamp | ||
496 | - end | 434 | + |
435 | + self.connection.add_index versioned_table_name, versioned_foreign_key | ||
497 | end | 436 | end |
498 | - | 437 | + |
499 | # Rake migration task to drop the versioned table | 438 | # Rake migration task to drop the versioned table |
500 | def drop_versioned_table | 439 | def drop_versioned_table |
501 | self.connection.drop_table versioned_table_name | 440 | self.connection.drop_table versioned_table_name |
502 | end | 441 | end |
503 | - | 442 | + |
504 | # Executes the block with the versioning callbacks disabled. | 443 | # Executes the block with the versioning callbacks disabled. |
505 | # | 444 | # |
506 | # Foo.without_revision do | 445 | # Foo.without_revision do |
@@ -532,14 +471,16 @@ module ActiveRecord #:nodoc: | @@ -532,14 +471,16 @@ module ActiveRecord #:nodoc: | ||
532 | def without_locking(&block) | 471 | def without_locking(&block) |
533 | current = ActiveRecord::Base.lock_optimistically | 472 | current = ActiveRecord::Base.lock_optimistically |
534 | ActiveRecord::Base.lock_optimistically = false if current | 473 | ActiveRecord::Base.lock_optimistically = false if current |
535 | - result = block.call | ||
536 | - ActiveRecord::Base.lock_optimistically = true if current | ||
537 | - result | ||
538 | - end | 474 | + begin |
475 | + block.call | ||
476 | + ensure | ||
477 | + ActiveRecord::Base.lock_optimistically = true if current | ||
478 | + end | ||
479 | + end | ||
539 | end | 480 | end |
540 | end | 481 | end |
541 | end | 482 | end |
542 | end | 483 | end |
543 | end | 484 | end |
544 | 485 | ||
545 | -ActiveRecord::Base.send :include, ActiveRecord::Acts::Versioned | ||
546 | \ No newline at end of file | 486 | \ No newline at end of file |
487 | +ActiveRecord::Base.send :include, ActiveRecord::Acts::Versioned |
vendor/plugins/acts_as_versioned/test/abstract_unit.rb
@@ -10,11 +10,18 @@ rescue LoadError | @@ -10,11 +10,18 @@ rescue LoadError | ||
10 | require 'rubygems' | 10 | require 'rubygems' |
11 | retry | 11 | retry |
12 | end | 12 | end |
13 | + | ||
14 | +begin | ||
15 | + require 'ruby-debug' | ||
16 | + Debugger.start | ||
17 | +rescue LoadError | ||
18 | +end | ||
19 | + | ||
13 | require 'acts_as_versioned' | 20 | require 'acts_as_versioned' |
14 | 21 | ||
15 | config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) | 22 | config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) |
16 | ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") | 23 | ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") |
17 | -ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite']} | 24 | +ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']} |
18 | ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) | 25 | ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) |
19 | 26 | ||
20 | load(File.dirname(__FILE__) + "/schema.rb") | 27 | load(File.dirname(__FILE__) + "/schema.rb") |
vendor/plugins/acts_as_versioned/test/fixtures/landmarks.yml
vendor/plugins/acts_as_versioned/test/fixtures/locked_pages_revisions.yml
@@ -2,26 +2,26 @@ welcome_1: | @@ -2,26 +2,26 @@ welcome_1: | ||
2 | id: 1 | 2 | id: 1 |
3 | page_id: 1 | 3 | page_id: 1 |
4 | title: Welcome to the weblg | 4 | title: Welcome to the weblg |
5 | - version: 23 | 5 | + lock_version: 23 |
6 | version_type: LockedPage | 6 | version_type: LockedPage |
7 | 7 | ||
8 | welcome_2: | 8 | welcome_2: |
9 | id: 2 | 9 | id: 2 |
10 | page_id: 1 | 10 | page_id: 1 |
11 | title: Welcome to the weblog | 11 | title: Welcome to the weblog |
12 | - version: 24 | 12 | + lock_version: 24 |
13 | version_type: LockedPage | 13 | version_type: LockedPage |
14 | 14 | ||
15 | thinking_1: | 15 | thinking_1: |
16 | id: 3 | 16 | id: 3 |
17 | page_id: 2 | 17 | page_id: 2 |
18 | title: So I was thinking!!! | 18 | title: So I was thinking!!! |
19 | - version: 23 | 19 | + lock_version: 23 |
20 | version_type: SpecialLockedPage | 20 | version_type: SpecialLockedPage |
21 | 21 | ||
22 | thinking_2: | 22 | thinking_2: |
23 | id: 4 | 23 | id: 4 |
24 | page_id: 2 | 24 | page_id: 2 |
25 | title: So I was thinking | 25 | title: So I was thinking |
26 | - version: 24 | 26 | + lock_version: 24 |
27 | version_type: SpecialLockedPage | 27 | version_type: SpecialLockedPage |
vendor/plugins/acts_as_versioned/test/fixtures/pages.yml
@@ -4,4 +4,5 @@ welcome: | @@ -4,4 +4,5 @@ welcome: | ||
4 | body: Such a lovely day | 4 | body: Such a lovely day |
5 | version: 24 | 5 | version: 24 |
6 | author_id: 1 | 6 | author_id: 1 |
7 | - revisor_id: 1 | ||
8 | \ No newline at end of file | 7 | \ No newline at end of file |
8 | + revisor_id: 1 | ||
9 | + created_on: "2008-01-01 00:00:00" | ||
9 | \ No newline at end of file | 10 | \ No newline at end of file |
vendor/plugins/acts_as_versioned/test/migration_test.rb
@@ -8,10 +8,15 @@ if ActiveRecord::Base.connection.supports_migrations? | @@ -8,10 +8,15 @@ if ActiveRecord::Base.connection.supports_migrations? | ||
8 | 8 | ||
9 | class MigrationTest < Test::Unit::TestCase | 9 | class MigrationTest < Test::Unit::TestCase |
10 | self.use_transactional_fixtures = false | 10 | self.use_transactional_fixtures = false |
11 | - def teardown | ||
12 | - ActiveRecord::Base.connection.initialize_schema_information | ||
13 | - ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0" | ||
14 | - | 11 | + def teardown |
12 | + if ActiveRecord::Base.connection.respond_to?(:initialize_schema_information) | ||
13 | + ActiveRecord::Base.connection.initialize_schema_information | ||
14 | + ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0" | ||
15 | + else | ||
16 | + ActiveRecord::Base.connection.initialize_schema_migrations_table | ||
17 | + ActiveRecord::Base.connection.assume_migrated_upto_version(0) | ||
18 | + end | ||
19 | + | ||
15 | Thing.connection.drop_table "things" rescue nil | 20 | Thing.connection.drop_table "things" rescue nil |
16 | Thing.connection.drop_table "thing_versions" rescue nil | 21 | Thing.connection.drop_table "thing_versions" rescue nil |
17 | Thing.reset_column_information | 22 | Thing.reset_column_information |
vendor/plugins/acts_as_versioned/test/schema.rb
@@ -3,6 +3,7 @@ ActiveRecord::Schema.define(:version => 0) do | @@ -3,6 +3,7 @@ ActiveRecord::Schema.define(:version => 0) do | ||
3 | t.column :version, :integer | 3 | t.column :version, :integer |
4 | t.column :title, :string, :limit => 255 | 4 | t.column :title, :string, :limit => 255 |
5 | t.column :body, :text | 5 | t.column :body, :text |
6 | + t.column :created_on, :datetime | ||
6 | t.column :updated_on, :datetime | 7 | t.column :updated_on, :datetime |
7 | t.column :author_id, :integer | 8 | t.column :author_id, :integer |
8 | t.column :revisor_id, :integer | 9 | t.column :revisor_id, :integer |
@@ -13,11 +14,14 @@ ActiveRecord::Schema.define(:version => 0) do | @@ -13,11 +14,14 @@ ActiveRecord::Schema.define(:version => 0) do | ||
13 | t.column :version, :integer | 14 | t.column :version, :integer |
14 | t.column :title, :string, :limit => 255 | 15 | t.column :title, :string, :limit => 255 |
15 | t.column :body, :text | 16 | t.column :body, :text |
17 | + t.column :created_on, :datetime | ||
16 | t.column :updated_on, :datetime | 18 | t.column :updated_on, :datetime |
17 | t.column :author_id, :integer | 19 | t.column :author_id, :integer |
18 | t.column :revisor_id, :integer | 20 | t.column :revisor_id, :integer |
19 | end | 21 | end |
20 | 22 | ||
23 | + add_index :page_versions, [:page_id, :version], :unique => true | ||
24 | + | ||
21 | create_table :authors, :force => true do |t| | 25 | create_table :authors, :force => true do |t| |
22 | t.column :page_id, :integer | 26 | t.column :page_id, :integer |
23 | t.column :name, :string | 27 | t.column :name, :string |
@@ -26,16 +30,20 @@ ActiveRecord::Schema.define(:version => 0) do | @@ -26,16 +30,20 @@ ActiveRecord::Schema.define(:version => 0) do | ||
26 | create_table :locked_pages, :force => true do |t| | 30 | create_table :locked_pages, :force => true do |t| |
27 | t.column :lock_version, :integer | 31 | t.column :lock_version, :integer |
28 | t.column :title, :string, :limit => 255 | 32 | t.column :title, :string, :limit => 255 |
33 | + t.column :body, :text | ||
29 | t.column :type, :string, :limit => 255 | 34 | t.column :type, :string, :limit => 255 |
30 | end | 35 | end |
31 | 36 | ||
32 | create_table :locked_pages_revisions, :force => true do |t| | 37 | create_table :locked_pages_revisions, :force => true do |t| |
33 | t.column :page_id, :integer | 38 | t.column :page_id, :integer |
34 | - t.column :version, :integer | 39 | + t.column :lock_version, :integer |
35 | t.column :title, :string, :limit => 255 | 40 | t.column :title, :string, :limit => 255 |
41 | + t.column :body, :text | ||
36 | t.column :version_type, :string, :limit => 255 | 42 | t.column :version_type, :string, :limit => 255 |
37 | t.column :updated_at, :datetime | 43 | t.column :updated_at, :datetime |
38 | end | 44 | end |
45 | + | ||
46 | + add_index :locked_pages_revisions, [:page_id, :lock_version], :unique => true | ||
39 | 47 | ||
40 | create_table :widgets, :force => true do |t| | 48 | create_table :widgets, :force => true do |t| |
41 | t.column :name, :string, :limit => 50 | 49 | t.column :name, :string, :limit => 50 |
@@ -51,10 +59,13 @@ ActiveRecord::Schema.define(:version => 0) do | @@ -51,10 +59,13 @@ ActiveRecord::Schema.define(:version => 0) do | ||
51 | t.column :updated_at, :datetime | 59 | t.column :updated_at, :datetime |
52 | end | 60 | end |
53 | 61 | ||
62 | + add_index :widget_versions, [:widget_id, :version], :unique => true | ||
63 | + | ||
54 | create_table :landmarks, :force => true do |t| | 64 | create_table :landmarks, :force => true do |t| |
55 | t.column :name, :string | 65 | t.column :name, :string |
56 | t.column :latitude, :float | 66 | t.column :latitude, :float |
57 | t.column :longitude, :float | 67 | t.column :longitude, :float |
68 | + t.column :doesnt_trigger_version,:string | ||
58 | t.column :version, :integer | 69 | t.column :version, :integer |
59 | end | 70 | end |
60 | 71 | ||
@@ -63,6 +74,9 @@ ActiveRecord::Schema.define(:version => 0) do | @@ -63,6 +74,9 @@ ActiveRecord::Schema.define(:version => 0) do | ||
63 | t.column :name, :string | 74 | t.column :name, :string |
64 | t.column :latitude, :float | 75 | t.column :latitude, :float |
65 | t.column :longitude, :float | 76 | t.column :longitude, :float |
77 | + t.column :doesnt_trigger_version,:string | ||
66 | t.column :version, :integer | 78 | t.column :version, :integer |
67 | end | 79 | end |
80 | + | ||
81 | + add_index :landmark_versions, [:landmark_id, :version], :unique => true | ||
68 | end | 82 | end |
vendor/plugins/acts_as_versioned/test/versioned_test.rb
@@ -17,13 +17,13 @@ class VersionedTest < Test::Unit::TestCase | @@ -17,13 +17,13 @@ class VersionedTest < Test::Unit::TestCase | ||
17 | def test_saves_without_revision | 17 | def test_saves_without_revision |
18 | p = pages(:welcome) | 18 | p = pages(:welcome) |
19 | old_versions = p.versions.count | 19 | old_versions = p.versions.count |
20 | - | 20 | + |
21 | p.save_without_revision | 21 | p.save_without_revision |
22 | - | 22 | + |
23 | p.without_revision do | 23 | p.without_revision do |
24 | p.update_attributes :title => 'changed' | 24 | p.update_attributes :title => 'changed' |
25 | end | 25 | end |
26 | - | 26 | + |
27 | assert_equal old_versions, p.versions.count | 27 | assert_equal old_versions, p.versions.count |
28 | end | 28 | end |
29 | 29 | ||
@@ -31,8 +31,8 @@ class VersionedTest < Test::Unit::TestCase | @@ -31,8 +31,8 @@ class VersionedTest < Test::Unit::TestCase | ||
31 | p = pages(:welcome) | 31 | p = pages(:welcome) |
32 | assert_equal 24, p.version | 32 | assert_equal 24, p.version |
33 | assert_equal 'Welcome to the weblog', p.title | 33 | assert_equal 'Welcome to the weblog', p.title |
34 | - | ||
35 | - assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23" | 34 | + |
35 | + assert p.revert_to!(23), "Couldn't revert to 23" | ||
36 | assert_equal 23, p.version | 36 | assert_equal 23, p.version |
37 | assert_equal 'Welcome to the weblg', p.title | 37 | assert_equal 'Welcome to the weblg', p.title |
38 | end | 38 | end |
@@ -58,12 +58,12 @@ class VersionedTest < Test::Unit::TestCase | @@ -58,12 +58,12 @@ class VersionedTest < Test::Unit::TestCase | ||
58 | p = pages(:welcome) | 58 | p = pages(:welcome) |
59 | assert_equal 24, p.version | 59 | assert_equal 24, p.version |
60 | assert_equal 'Welcome to the weblog', p.title | 60 | assert_equal 'Welcome to the weblog', p.title |
61 | - | ||
62 | - assert p.revert_to!(p.versions.first), "Couldn't revert to 23" | 61 | + |
62 | + assert p.revert_to!(p.versions.find_by_version(23)), "Couldn't revert to 23" | ||
63 | assert_equal 23, p.version | 63 | assert_equal 23, p.version |
64 | assert_equal 'Welcome to the weblg', p.title | 64 | assert_equal 'Welcome to the weblg', p.title |
65 | end | 65 | end |
66 | - | 66 | + |
67 | def test_rollback_fails_with_invalid_revision | 67 | def test_rollback_fails_with_invalid_revision |
68 | p = locked_pages(:welcome) | 68 | p = locked_pages(:welcome) |
69 | assert !p.revert_to!(locked_pages(:thinking)) | 69 | assert !p.revert_to!(locked_pages(:thinking)) |
@@ -75,27 +75,27 @@ class VersionedTest < Test::Unit::TestCase | @@ -75,27 +75,27 @@ class VersionedTest < Test::Unit::TestCase | ||
75 | assert_equal 1, p.versions.size | 75 | assert_equal 1, p.versions.size |
76 | assert_instance_of LockedPage.versioned_class, p.versions.first | 76 | assert_instance_of LockedPage.versioned_class, p.versions.first |
77 | end | 77 | end |
78 | - | 78 | + |
79 | def test_rollback_with_version_number_with_options | 79 | def test_rollback_with_version_number_with_options |
80 | p = locked_pages(:welcome) | 80 | p = locked_pages(:welcome) |
81 | assert_equal 'Welcome to the weblog', p.title | 81 | assert_equal 'Welcome to the weblog', p.title |
82 | assert_equal 'LockedPage', p.versions.first.version_type | 82 | assert_equal 'LockedPage', p.versions.first.version_type |
83 | - | ||
84 | - assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23" | 83 | + |
84 | + assert p.revert_to!(p.versions.first.lock_version), "Couldn't revert to 23" | ||
85 | assert_equal 'Welcome to the weblg', p.title | 85 | assert_equal 'Welcome to the weblg', p.title |
86 | assert_equal 'LockedPage', p.versions.first.version_type | 86 | assert_equal 'LockedPage', p.versions.first.version_type |
87 | end | 87 | end |
88 | - | 88 | + |
89 | def test_rollback_with_version_class_with_options | 89 | def test_rollback_with_version_class_with_options |
90 | p = locked_pages(:welcome) | 90 | p = locked_pages(:welcome) |
91 | assert_equal 'Welcome to the weblog', p.title | 91 | assert_equal 'Welcome to the weblog', p.title |
92 | assert_equal 'LockedPage', p.versions.first.version_type | 92 | assert_equal 'LockedPage', p.versions.first.version_type |
93 | - | 93 | + |
94 | assert p.revert_to!(p.versions.first), "Couldn't revert to 1" | 94 | assert p.revert_to!(p.versions.first), "Couldn't revert to 1" |
95 | assert_equal 'Welcome to the weblg', p.title | 95 | assert_equal 'Welcome to the weblg', p.title |
96 | assert_equal 'LockedPage', p.versions.first.version_type | 96 | assert_equal 'LockedPage', p.versions.first.version_type |
97 | end | 97 | end |
98 | - | 98 | + |
99 | def test_saves_versioned_copy_with_sti | 99 | def test_saves_versioned_copy_with_sti |
100 | p = SpecialLockedPage.create! :title => 'first title' | 100 | p = SpecialLockedPage.create! :title => 'first title' |
101 | assert !p.new_record? | 101 | assert !p.new_record? |
@@ -103,12 +103,12 @@ class VersionedTest < Test::Unit::TestCase | @@ -103,12 +103,12 @@ class VersionedTest < Test::Unit::TestCase | ||
103 | assert_instance_of LockedPage.versioned_class, p.versions.first | 103 | assert_instance_of LockedPage.versioned_class, p.versions.first |
104 | assert_equal 'SpecialLockedPage', p.versions.first.version_type | 104 | assert_equal 'SpecialLockedPage', p.versions.first.version_type |
105 | end | 105 | end |
106 | - | 106 | + |
107 | def test_rollback_with_version_number_with_sti | 107 | def test_rollback_with_version_number_with_sti |
108 | p = locked_pages(:thinking) | 108 | p = locked_pages(:thinking) |
109 | assert_equal 'So I was thinking', p.title | 109 | assert_equal 'So I was thinking', p.title |
110 | - | ||
111 | - assert p.revert_to!(p.versions.first.version), "Couldn't revert to 1" | 110 | + |
111 | + assert p.revert_to!(p.versions.first.lock_version), "Couldn't revert to 1" | ||
112 | assert_equal 'So I was thinking!!!', p.title | 112 | assert_equal 'So I was thinking!!!', p.title |
113 | assert_equal 'SpecialLockedPage', p.versions.first.version_type | 113 | assert_equal 'SpecialLockedPage', p.versions.first.version_type |
114 | end | 114 | end |
@@ -116,11 +116,11 @@ class VersionedTest < Test::Unit::TestCase | @@ -116,11 +116,11 @@ class VersionedTest < Test::Unit::TestCase | ||
116 | def test_lock_version_works_with_versioning | 116 | def test_lock_version_works_with_versioning |
117 | p = locked_pages(:thinking) | 117 | p = locked_pages(:thinking) |
118 | p2 = LockedPage.find(p.id) | 118 | p2 = LockedPage.find(p.id) |
119 | - | 119 | + |
120 | p.title = 'fresh title' | 120 | p.title = 'fresh title' |
121 | p.save | 121 | p.save |
122 | assert_equal 2, p.versions.size # limit! | 122 | assert_equal 2, p.versions.size # limit! |
123 | - | 123 | + |
124 | assert_raises(ActiveRecord::StaleObjectError) do | 124 | assert_raises(ActiveRecord::StaleObjectError) do |
125 | p2.title = 'stale title' | 125 | p2.title = 'stale title' |
126 | p2.save | 126 | p2.save |
@@ -130,13 +130,13 @@ class VersionedTest < Test::Unit::TestCase | @@ -130,13 +130,13 @@ class VersionedTest < Test::Unit::TestCase | ||
130 | def test_version_if_condition | 130 | def test_version_if_condition |
131 | p = Page.create! :title => "title" | 131 | p = Page.create! :title => "title" |
132 | assert_equal 1, p.version | 132 | assert_equal 1, p.version |
133 | - | 133 | + |
134 | Page.feeling_good = false | 134 | Page.feeling_good = false |
135 | p.save | 135 | p.save |
136 | assert_equal 1, p.version | 136 | assert_equal 1, p.version |
137 | Page.feeling_good = true | 137 | Page.feeling_good = true |
138 | end | 138 | end |
139 | - | 139 | + |
140 | def test_version_if_condition2 | 140 | def test_version_if_condition2 |
141 | # set new if condition | 141 | # set new if condition |
142 | Page.class_eval do | 142 | Page.class_eval do |
@@ -144,40 +144,40 @@ class VersionedTest < Test::Unit::TestCase | @@ -144,40 +144,40 @@ class VersionedTest < Test::Unit::TestCase | ||
144 | alias_method :old_feeling_good, :feeling_good? | 144 | alias_method :old_feeling_good, :feeling_good? |
145 | alias_method :feeling_good?, :new_feeling_good | 145 | alias_method :feeling_good?, :new_feeling_good |
146 | end | 146 | end |
147 | - | 147 | + |
148 | p = Page.create! :title => "title" | 148 | p = Page.create! :title => "title" |
149 | assert_equal 1, p.version # version does not increment | 149 | assert_equal 1, p.version # version does not increment |
150 | - assert_equal 1, p.versions(true).size | ||
151 | - | 150 | + assert_equal 1, p.versions.count |
151 | + | ||
152 | p.update_attributes(:title => 'new title') | 152 | p.update_attributes(:title => 'new title') |
153 | assert_equal 1, p.version # version does not increment | 153 | assert_equal 1, p.version # version does not increment |
154 | - assert_equal 1, p.versions(true).size | ||
155 | - | 154 | + assert_equal 1, p.versions.count |
155 | + | ||
156 | p.update_attributes(:title => 'a title') | 156 | p.update_attributes(:title => 'a title') |
157 | assert_equal 2, p.version | 157 | assert_equal 2, p.version |
158 | - assert_equal 2, p.versions(true).size | ||
159 | - | 158 | + assert_equal 2, p.versions.count |
159 | + | ||
160 | # reset original if condition | 160 | # reset original if condition |
161 | Page.class_eval { alias_method :feeling_good?, :old_feeling_good } | 161 | Page.class_eval { alias_method :feeling_good?, :old_feeling_good } |
162 | end | 162 | end |
163 | - | 163 | + |
164 | def test_version_if_condition_with_block | 164 | def test_version_if_condition_with_block |
165 | # set new if condition | 165 | # set new if condition |
166 | old_condition = Page.version_condition | 166 | old_condition = Page.version_condition |
167 | Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' } | 167 | Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' } |
168 | - | 168 | + |
169 | p = Page.create! :title => "title" | 169 | p = Page.create! :title => "title" |
170 | assert_equal 1, p.version # version does not increment | 170 | assert_equal 1, p.version # version does not increment |
171 | - assert_equal 1, p.versions(true).size | ||
172 | - | 171 | + assert_equal 1, p.versions.count |
172 | + | ||
173 | p.update_attributes(:title => 'a title') | 173 | p.update_attributes(:title => 'a title') |
174 | assert_equal 1, p.version # version does not increment | 174 | assert_equal 1, p.version # version does not increment |
175 | - assert_equal 1, p.versions(true).size | ||
176 | - | 175 | + assert_equal 1, p.versions.count |
176 | + | ||
177 | p.update_attributes(:title => 'b title') | 177 | p.update_attributes(:title => 'b title') |
178 | assert_equal 2, p.version | 178 | assert_equal 2, p.version |
179 | - assert_equal 2, p.versions(true).size | ||
180 | - | 179 | + assert_equal 2, p.versions.count |
180 | + | ||
181 | # reset original if condition | 181 | # reset original if condition |
182 | Page.version_condition = old_condition | 182 | Page.version_condition = old_condition |
183 | end | 183 | end |
@@ -187,7 +187,10 @@ class VersionedTest < Test::Unit::TestCase | @@ -187,7 +187,10 @@ class VersionedTest < Test::Unit::TestCase | ||
187 | p.save | 187 | p.save |
188 | p.save | 188 | p.save |
189 | 5.times do |i| | 189 | 5.times do |i| |
190 | - assert_page_title p, i | 190 | + p.title = "title#{i}" |
191 | + p.save | ||
192 | + assert_equal "title#{i}", p.title | ||
193 | + assert_equal (i+2), p.version | ||
191 | end | 194 | end |
192 | end | 195 | end |
193 | 196 | ||
@@ -196,33 +199,31 @@ class VersionedTest < Test::Unit::TestCase | @@ -196,33 +199,31 @@ class VersionedTest < Test::Unit::TestCase | ||
196 | p.update_attributes(:title => "title1") | 199 | p.update_attributes(:title => "title1") |
197 | p.update_attributes(:title => "title2") | 200 | p.update_attributes(:title => "title2") |
198 | 5.times do |i| | 201 | 5.times do |i| |
199 | - assert_page_title p, i, :lock_version | 202 | + p.title = "title#{i}" |
203 | + p.save | ||
204 | + assert_equal "title#{i}", p.title | ||
205 | + assert_equal (i+4), p.lock_version | ||
200 | assert p.versions(true).size <= 2, "locked version can only store 2 versions" | 206 | assert p.versions(true).size <= 2, "locked version can only store 2 versions" |
201 | end | 207 | end |
202 | end | 208 | end |
203 | - | ||
204 | - def test_track_changed_attributes_default_value | ||
205 | - assert !Page.track_changed_attributes | ||
206 | - assert LockedPage.track_changed_attributes | ||
207 | - assert SpecialLockedPage.track_changed_attributes | ||
208 | - end | ||
209 | - | ||
210 | - def test_version_order | ||
211 | - assert_equal 23, pages(:welcome).versions.first.version | ||
212 | - assert_equal 24, pages(:welcome).versions.last.version | 209 | + |
210 | + def test_track_altered_attributes_default_value | ||
211 | + assert !Page.track_altered_attributes | ||
212 | + assert LockedPage.track_altered_attributes | ||
213 | + assert SpecialLockedPage.track_altered_attributes | ||
213 | end | 214 | end |
214 | - | ||
215 | - def test_track_changed_attributes | 215 | + |
216 | + def test_track_altered_attributes | ||
216 | p = LockedPage.create! :title => "title" | 217 | p = LockedPage.create! :title => "title" |
217 | assert_equal 1, p.lock_version | 218 | assert_equal 1, p.lock_version |
218 | assert_equal 1, p.versions(true).size | 219 | assert_equal 1, p.versions(true).size |
219 | - | ||
220 | - p.title = 'title' | 220 | + |
221 | + p.body = 'whoa' | ||
221 | assert !p.save_version? | 222 | assert !p.save_version? |
222 | p.save | 223 | p.save |
223 | assert_equal 2, p.lock_version # still increments version because of optimistic locking | 224 | assert_equal 2, p.lock_version # still increments version because of optimistic locking |
224 | assert_equal 1, p.versions(true).size | 225 | assert_equal 1, p.versions(true).size |
225 | - | 226 | + |
226 | p.title = 'updated title' | 227 | p.title = 'updated title' |
227 | assert p.save_version? | 228 | assert p.save_version? |
228 | p.save | 229 | p.save |
@@ -235,22 +236,15 @@ class VersionedTest < Test::Unit::TestCase | @@ -235,22 +236,15 @@ class VersionedTest < Test::Unit::TestCase | ||
235 | assert_equal 4, p.lock_version | 236 | assert_equal 4, p.lock_version |
236 | assert_equal 2, p.versions(true).size # version 1 deleted | 237 | assert_equal 2, p.versions(true).size # version 1 deleted |
237 | end | 238 | end |
238 | - | ||
239 | - def assert_page_title(p, i, version_field = :version) | ||
240 | - p.title = "title#{i}" | ||
241 | - p.save | ||
242 | - assert_equal "title#{i}", p.title | ||
243 | - assert_equal (i+4), p.send(version_field) | ||
244 | - end | ||
245 | - | 239 | + |
246 | def test_find_versions | 240 | def test_find_versions |
247 | - assert_equal 2, locked_pages(:welcome).versions.size | ||
248 | - assert_equal 1, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%weblog%']).length | ||
249 | - assert_equal 2, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%web%']).length | ||
250 | - assert_equal 0, locked_pages(:thinking).versions.find(:all, :conditions => ['title LIKE ?', '%web%']).length | ||
251 | - assert_equal 2, locked_pages(:welcome).versions.length | 241 | + assert_equal 1, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%weblog%']).size |
252 | end | 242 | end |
253 | - | 243 | + |
244 | + def test_find_version | ||
245 | + assert_equal page_versions(:welcome_1), pages(:welcome).versions.find_by_version(23) | ||
246 | + end | ||
247 | + | ||
254 | def test_with_sequence | 248 | def test_with_sequence |
255 | assert_equal 'widgets_seq', Widget.versioned_class.sequence_name | 249 | assert_equal 'widgets_seq', Widget.versioned_class.sequence_name |
256 | 3.times { Widget.create! :name => 'new widget' } | 250 | 3.times { Widget.create! :name => 'new widget' } |
@@ -265,25 +259,24 @@ class VersionedTest < Test::Unit::TestCase | @@ -265,25 +259,24 @@ class VersionedTest < Test::Unit::TestCase | ||
265 | def test_has_many_through_with_custom_association | 259 | def test_has_many_through_with_custom_association |
266 | assert_equal [authors(:caged), authors(:mly)], pages(:welcome).revisors | 260 | assert_equal [authors(:caged), authors(:mly)], pages(:welcome).revisors |
267 | end | 261 | end |
268 | - | 262 | + |
269 | def test_referential_integrity | 263 | def test_referential_integrity |
270 | pages(:welcome).destroy | 264 | pages(:welcome).destroy |
271 | assert_equal 0, Page.count | 265 | assert_equal 0, Page.count |
272 | assert_equal 0, Page::Version.count | 266 | assert_equal 0, Page::Version.count |
273 | end | 267 | end |
274 | - | 268 | + |
275 | def test_association_options | 269 | def test_association_options |
276 | association = Page.reflect_on_association(:versions) | 270 | association = Page.reflect_on_association(:versions) |
277 | options = association.options | 271 | options = association.options |
278 | assert_equal :delete_all, options[:dependent] | 272 | assert_equal :delete_all, options[:dependent] |
279 | - assert_equal 'version', options[:order] | ||
280 | - | 273 | + |
281 | association = Widget.reflect_on_association(:versions) | 274 | association = Widget.reflect_on_association(:versions) |
282 | options = association.options | 275 | options = association.options |
283 | assert_equal :nullify, options[:dependent] | 276 | assert_equal :nullify, options[:dependent] |
284 | assert_equal 'version desc', options[:order] | 277 | assert_equal 'version desc', options[:order] |
285 | assert_equal 'widget_id', options[:foreign_key] | 278 | assert_equal 'widget_id', options[:foreign_key] |
286 | - | 279 | + |
287 | widget = Widget.create! :name => 'new widget' | 280 | widget = Widget.create! :name => 'new widget' |
288 | assert_equal 1, Widget.count | 281 | assert_equal 1, Widget.count |
289 | assert_equal 1, Widget.versioned_class.count | 282 | assert_equal 1, Widget.versioned_class.count |
@@ -297,12 +290,12 @@ class VersionedTest < Test::Unit::TestCase | @@ -297,12 +290,12 @@ class VersionedTest < Test::Unit::TestCase | ||
297 | page_version = page.versions.last | 290 | page_version = page.versions.last |
298 | assert_equal page, page_version.page | 291 | assert_equal page, page_version.page |
299 | end | 292 | end |
300 | - | ||
301 | - def test_unchanged_attributes | 293 | + |
294 | + def test_unaltered_attributes | ||
302 | landmarks(:washington).attributes = landmarks(:washington).attributes.except("id") | 295 | landmarks(:washington).attributes = landmarks(:washington).attributes.except("id") |
303 | assert !landmarks(:washington).changed? | 296 | assert !landmarks(:washington).changed? |
304 | end | 297 | end |
305 | - | 298 | + |
306 | def test_unchanged_string_attributes | 299 | def test_unchanged_string_attributes |
307 | landmarks(:washington).attributes = landmarks(:washington).attributes.except("id").inject({}) { |params, (key, value)| params.update(key => value.to_s) } | 300 | landmarks(:washington).attributes = landmarks(:washington).attributes.except("id").inject({}) { |params, (key, value)| params.update(key => value.to_s) } |
308 | assert !landmarks(:washington).changed? | 301 | assert !landmarks(:washington).changed? |
@@ -311,18 +304,67 @@ class VersionedTest < Test::Unit::TestCase | @@ -311,18 +304,67 @@ class VersionedTest < Test::Unit::TestCase | ||
311 | def test_should_find_earliest_version | 304 | def test_should_find_earliest_version |
312 | assert_equal page_versions(:welcome_1), pages(:welcome).versions.earliest | 305 | assert_equal page_versions(:welcome_1), pages(:welcome).versions.earliest |
313 | end | 306 | end |
314 | - | 307 | + |
315 | def test_should_find_latest_version | 308 | def test_should_find_latest_version |
316 | assert_equal page_versions(:welcome_2), pages(:welcome).versions.latest | 309 | assert_equal page_versions(:welcome_2), pages(:welcome).versions.latest |
317 | end | 310 | end |
318 | - | 311 | + |
319 | def test_should_find_previous_version | 312 | def test_should_find_previous_version |
320 | assert_equal page_versions(:welcome_1), page_versions(:welcome_2).previous | 313 | assert_equal page_versions(:welcome_1), page_versions(:welcome_2).previous |
321 | assert_equal page_versions(:welcome_1), pages(:welcome).versions.before(page_versions(:welcome_2)) | 314 | assert_equal page_versions(:welcome_1), pages(:welcome).versions.before(page_versions(:welcome_2)) |
322 | end | 315 | end |
323 | - | 316 | + |
324 | def test_should_find_next_version | 317 | def test_should_find_next_version |
325 | assert_equal page_versions(:welcome_2), page_versions(:welcome_1).next | 318 | assert_equal page_versions(:welcome_2), page_versions(:welcome_1).next |
326 | assert_equal page_versions(:welcome_2), pages(:welcome).versions.after(page_versions(:welcome_1)) | 319 | assert_equal page_versions(:welcome_2), pages(:welcome).versions.after(page_versions(:welcome_1)) |
327 | end | 320 | end |
321 | + | ||
322 | + def test_should_find_version_count | ||
323 | + assert_equal 2, pages(:welcome).versions.size | ||
324 | + end | ||
325 | + | ||
326 | + def test_if_changed_creates_version_if_a_listed_column_is_changed | ||
327 | + landmarks(:washington).name = "Washington" | ||
328 | + assert landmarks(:washington).changed? | ||
329 | + assert landmarks(:washington).altered? | ||
330 | + end | ||
331 | + | ||
332 | + def test_if_changed_creates_version_if_all_listed_columns_are_changed | ||
333 | + landmarks(:washington).name = "Washington" | ||
334 | + landmarks(:washington).latitude = 1.0 | ||
335 | + landmarks(:washington).longitude = 1.0 | ||
336 | + assert landmarks(:washington).changed? | ||
337 | + assert landmarks(:washington).altered? | ||
338 | + end | ||
339 | + | ||
340 | + def test_if_changed_does_not_create_new_version_if_unlisted_column_is_changed | ||
341 | + landmarks(:washington).doesnt_trigger_version = "This should not trigger version" | ||
342 | + assert landmarks(:washington).changed? | ||
343 | + assert !landmarks(:washington).altered? | ||
344 | + end | ||
345 | + | ||
346 | + def test_without_locking_temporarily_disables_optimistic_locking | ||
347 | + enabled1 = false | ||
348 | + block_called = false | ||
349 | + | ||
350 | + ActiveRecord::Base.lock_optimistically = true | ||
351 | + LockedPage.without_locking do | ||
352 | + enabled1 = ActiveRecord::Base.lock_optimistically | ||
353 | + block_called = true | ||
354 | + end | ||
355 | + enabled2 = ActiveRecord::Base.lock_optimistically | ||
356 | + | ||
357 | + assert block_called | ||
358 | + assert !enabled1 | ||
359 | + assert enabled2 | ||
360 | + end | ||
361 | + | ||
362 | + def test_without_locking_reverts_optimistic_locking_settings_if_block_raises_exception | ||
363 | + assert_raises(RuntimeError) do | ||
364 | + LockedPage.without_locking do | ||
365 | + raise RuntimeError, "oh noes" | ||
366 | + end | ||
367 | + end | ||
368 | + assert ActiveRecord::Base.lock_optimistically | ||
369 | + end | ||
328 | end | 370 | end |
329 | \ No newline at end of file | 371 | \ No newline at end of file |