Commit 3d6c66d50936b34772c0e47e394d46efe24cffd1

Authored by Leandro Santos
Committed by Antonio Terceiro
1 parent 5c9d49f0

Making the user unit test pass

db/schema.rb
1 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 3 # then regenerate this schema definition.
4 4 #
5 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 131 t.boolean "virtual", :default => false
132 132 end
133 133  
134   - add_index "categories_profiles", ["category_id"], :name => "index_categories_profiles_on_category_id"
135 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 137 create_table "comments", :force => true do |t|
138 138 t.string "title"
... ... @@ -211,8 +211,8 @@ ActiveRecord::Schema.define(:version => 69) do
211 211 t.datetime "updated_at"
212 212 end
213 213  
214   - add_index "product_categorizations", ["category_id"], :name => "index_product_categorizations_on_category_id"
215 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 217 create_table "products", :force => true do |t|
218 218 t.integer "enterprise_id"
... ...
test/unit/user_test.rb
... ... @@ -164,7 +164,8 @@ class UserTest < Test::Unit::TestCase
164 164  
165 165 def test_should_encrypt_password_with_salted_sha1
166 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 169 user.stubs(:salt).returns('testsalt')
169 170 user.save!
170 171  
... ... @@ -295,7 +296,8 @@ class UserTest < Test::Unit::TestCase
295 296 protected
296 297 def new_user(options = {})
297 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 301 user.save
300 302 user
301 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 11 * (5 Oct 2006) Allow customization of #versions association options [Dan Peterson]
4 12  
... ...
vendor/plugins/acts_as_versioned/Rakefile
1 1 require 'rubygems'
2 2  
3   -Gem::manage_gems
4   -
5 3 require 'rake/rdoctask'
6 4 require 'rake/packagetask'
7 5 require 'rake/gempackagetask'
... ...
vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb
... ... @@ -66,7 +66,7 @@ module ActiveRecord #:nodoc:
66 66 #
67 67 # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options
68 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 70 def self.included(base) # :nodoc:
71 71 base.extend ClassMethods
72 72 end
... ... @@ -95,12 +95,7 @@ module ActiveRecord #:nodoc:
95 95 # end
96 96 #
97 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 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 101 # to create an anonymous mixin:
... ... @@ -148,7 +143,7 @@ module ActiveRecord #:nodoc:
148 143 # # that create_table does
149 144 # Post.create_versioned_table
150 145 # end
151   - #
  146 + #
152 147 # def self.down
153 148 # Post.drop_versioned_table
154 149 # end
... ... @@ -172,21 +167,10 @@ module ActiveRecord #:nodoc:
172 167 return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods)
173 168  
174 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 175 self.versioned_class_name = options[:class_name] || "Version"
192 176 self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key
... ... @@ -196,11 +180,10 @@ module ActiveRecord #:nodoc:
196 180 self.version_sequence_name = options[:sequence_name]
197 181 self.max_version_limit = options[:limit].to_i
198 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 184 self.version_association_options = {
201 185 :class_name => "#{self.to_s}::#{versioned_class_name}",
202 186 :foreign_key => versioned_foreign_key,
203   - :order => 'version',
204 187 :dependent => :delete_all
205 188 }.merge(options[:association_options] || {})
206 189  
... ... @@ -209,40 +192,34 @@ module ActiveRecord #:nodoc:
209 192 silence_warnings do
210 193 self.const_set(extension_module_name, Module.new(&extension))
211 194 end
212   -
  195 +
213 196 options[:extend] = self.const_get(extension_module_name)
214 197 end
215 198  
216   - class_eval do
  199 + class_eval <<-CLASS_METHODS
217 200 has_many :versions, version_association_options do
218 201 # finds earliest version of this record
219 202 def earliest
220   - @earliest ||= find(:first)
  203 + @earliest ||= find(:first, :order => '#{version_column}')
221 204 end
222   -
  205 +
223 206 # find latest version of this record
224 207 def latest
225   - @latest ||= find(:first, :order => 'version desc')
  208 + @latest ||= find(:first, :order => '#{version_column} desc')
226 209 end
227 210 end
228 211 before_save :set_new_version
229   - after_create :save_version_on_create
230   - after_update :save_version
  212 + after_save :save_version
231 213 after_save :clear_old_versions
232   - after_save :clear_changed_attributes
233   -
  214 +
234 215 unless options[:if_changed].nil?
235   - self.track_changed_attributes = true
  216 + self.track_altered_attributes = true
236 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 219 end
243   -
  220 +
244 221 include options[:extend] if options[:extend].is_a?(Module)
245   - end
  222 + CLASS_METHODS
246 223  
247 224 # create the dynamic versioned model
248 225 const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do
... ... @@ -252,22 +229,26 @@ module ActiveRecord #:nodoc:
252 229 find :first, :order => 'version desc',
253 230 :conditions => ["#{original_class.versioned_foreign_key} = ? and version < ?", version.send(original_class.versioned_foreign_key), version.version]
254 231 end
255   -
  232 +
256 233 # find first version after the given version.
257 234 def self.after(version)
258 235 find :first, :order => 'version',
259 236 :conditions => ["#{original_class.versioned_foreign_key} = ? and version > ?", version.send(original_class.versioned_foreign_key), version.version]
260 237 end
261   -
  238 +
262 239 def previous
263 240 self.class.before(self)
264 241 end
265   -
  242 +
266 243 def next
267 244 self.class.after(self)
268 245 end
  246 +
  247 + def versions_count
  248 + page.version
  249 + end
269 250 end
270   -
  251 +
271 252 versioned_class.cattr_accessor :original_class
272 253 versioned_class.original_class = self
273 254 versioned_class.set_table_name versioned_table_name
... ... @@ -278,24 +259,22 @@ module ActiveRecord #:nodoc:
278 259 versioned_class.set_sequence_name version_sequence_name if version_sequence_name
279 260 end
280 261 end
281   -
  262 +
282 263 module ActMethods
283 264 def self.included(base) # :nodoc:
284 265 base.extend ClassMethods
285 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 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 278 end
300 279  
301 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 283 return if self.class.max_version_limit == 0
305 284 excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit
306 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 287 end
310 288 end
311 289  
312 290 # Reverts a model to a given version. Takes either a version number or an instance of the versioned model
313 291 def revert_to(version)
314 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 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 296 end
319 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 299 true
322 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 303 # Takes either a version number or an instance of the versioned model
326 304 def revert_to!(version)
327 305 revert_to(version) ? save_without_revision : false
... ... @@ -342,41 +320,29 @@ module ActiveRecord #:nodoc:
342 320 end
343 321 end
344 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 326 end
358   -
359   - # keep old dirty? method
360   - alias_method :dirty?, :changed?
361   -
  327 +
362 328 # Clones a model. Used when saving a new version or reverting a model's version.
363 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 332 end
367   -
  333 +
368 334 if orig_model.is_a?(self.class.versioned_class)
369 335 new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column]
370 336 elsif new_model.is_a?(self.class.versioned_class)
371 337 new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
372 338 end
373 339 end
374   -
  340 +
375 341 # Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>.
376 342 def save_version?
377   - version_condition_met? && changed?
  343 + version_condition_met? && altered?
378 344 end
379   -
  345 +
380 346 # Checks condition set in the :if option to check whether a revision should be created or not. Override this for
381 347 # custom version condition checking.
382 348 def version_condition_met?
... ... @@ -387,7 +353,7 @@ module ActiveRecord #:nodoc:
387 353 version_condition.call(self)
388 354 else
389 355 version_condition
390   - end
  356 + end
391 357 end
392 358  
393 359 # Executes the block with the versioning callbacks disabled.
... ... @@ -412,50 +378,24 @@ module ActiveRecord #:nodoc:
412 378  
413 379 def empty_callback() end #:nodoc:
414 380  
415   - protected
  381 + protected
416 382 # sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version.
417 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 386 end
420   -
  387 +
421 388 # Gets the next available version for the current record, or 1 for a new record
422 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 391 end
438 392  
439 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 394 # Returns an array of columns that are versioned. See non_versioned_columns
455 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 397 end
458   -
  398 +
459 399 # Returns an instance of the dynamic versioned model
460 400 def versioned_class
461 401 const_get versioned_class_name
... ... @@ -464,43 +404,42 @@ module ActiveRecord #:nodoc:
464 404 # Rake migration task to create the versioned table using options passed to acts_as_versioned
465 405 def create_versioned_table(create_table_options = {})
466 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 410 end
  411 +
  412 + return if connection.table_exists?(versioned_table_name)
470 413  
471 414 self.connection.create_table(versioned_table_name, create_table_options) do |t|
472 415 t.column versioned_foreign_key, :integer
473   - t.column :version, :integer
  416 + t.column version_column, :integer
474 417 end
475   -
476   - updated_col = nil
  418 +
477 419 self.versioned_columns.each do |col|
478   - updated_col = col if !updated_col && %(updated_at updated_on).include?(col.name)
479 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 424 :precision => col.precision
484 425 end
485   -
  426 +
486 427 if type_col = self.columns_hash[inheritance_column]
487 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 432 :precision => type_col.precision
492 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 436 end
498   -
  437 +
499 438 # Rake migration task to drop the versioned table
500 439 def drop_versioned_table
501 440 self.connection.drop_table versioned_table_name
502 441 end
503   -
  442 +
504 443 # Executes the block with the versioning callbacks disabled.
505 444 #
506 445 # Foo.without_revision do
... ... @@ -532,14 +471,16 @@ module ActiveRecord #:nodoc:
532 471 def without_locking(&block)
533 472 current = ActiveRecord::Base.lock_optimistically
534 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 480 end
540 481 end
541 482 end
542 483 end
543 484 end
544 485  
545   -ActiveRecord::Base.send :include, ActiveRecord::Acts::Versioned
546 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 10 require 'rubygems'
11 11 retry
12 12 end
  13 +
  14 +begin
  15 + require 'ruby-debug'
  16 + Debugger.start
  17 +rescue LoadError
  18 +end
  19 +
13 20 require 'acts_as_versioned'
14 21  
15 22 config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
16 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 25 ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
19 26  
20 27 load(File.dirname(__FILE__) + "/schema.rb")
... ...
vendor/plugins/acts_as_versioned/test/fixtures/landmarks.yml
... ... @@ -3,4 +3,5 @@ washington:
3 3 name: Washington, D.C.
4 4 latitude: 38.895
5 5 longitude: -77.036667
  6 + doesnt_trigger_version: This is not important
6 7 version: 1
... ...
vendor/plugins/acts_as_versioned/test/fixtures/locked_pages_revisions.yml
... ... @@ -2,26 +2,26 @@ welcome_1:
2 2 id: 1
3 3 page_id: 1
4 4 title: Welcome to the weblg
5   - version: 23
  5 + lock_version: 23
6 6 version_type: LockedPage
7 7  
8 8 welcome_2:
9 9 id: 2
10 10 page_id: 1
11 11 title: Welcome to the weblog
12   - version: 24
  12 + lock_version: 24
13 13 version_type: LockedPage
14 14  
15 15 thinking_1:
16 16 id: 3
17 17 page_id: 2
18 18 title: So I was thinking!!!
19   - version: 23
  19 + lock_version: 23
20 20 version_type: SpecialLockedPage
21 21  
22 22 thinking_2:
23 23 id: 4
24 24 page_id: 2
25 25 title: So I was thinking
26   - version: 24
  26 + lock_version: 24
27 27 version_type: SpecialLockedPage
... ...
vendor/plugins/acts_as_versioned/test/fixtures/pages.yml
... ... @@ -4,4 +4,5 @@ welcome:
4 4 body: Such a lovely day
5 5 version: 24
6 6 author_id: 1
7   - revisor_id: 1
8 7 \ No newline at end of file
  8 + revisor_id: 1
  9 + created_on: "2008-01-01 00:00:00"
9 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 8  
9 9 class MigrationTest < Test::Unit::TestCase
10 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 20 Thing.connection.drop_table "things" rescue nil
16 21 Thing.connection.drop_table "thing_versions" rescue nil
17 22 Thing.reset_column_information
... ...
vendor/plugins/acts_as_versioned/test/schema.rb
... ... @@ -3,6 +3,7 @@ ActiveRecord::Schema.define(:version =&gt; 0) do
3 3 t.column :version, :integer
4 4 t.column :title, :string, :limit => 255
5 5 t.column :body, :text
  6 + t.column :created_on, :datetime
6 7 t.column :updated_on, :datetime
7 8 t.column :author_id, :integer
8 9 t.column :revisor_id, :integer
... ... @@ -13,11 +14,14 @@ ActiveRecord::Schema.define(:version =&gt; 0) do
13 14 t.column :version, :integer
14 15 t.column :title, :string, :limit => 255
15 16 t.column :body, :text
  17 + t.column :created_on, :datetime
16 18 t.column :updated_on, :datetime
17 19 t.column :author_id, :integer
18 20 t.column :revisor_id, :integer
19 21 end
20 22  
  23 + add_index :page_versions, [:page_id, :version], :unique => true
  24 +
21 25 create_table :authors, :force => true do |t|
22 26 t.column :page_id, :integer
23 27 t.column :name, :string
... ... @@ -26,16 +30,20 @@ ActiveRecord::Schema.define(:version =&gt; 0) do
26 30 create_table :locked_pages, :force => true do |t|
27 31 t.column :lock_version, :integer
28 32 t.column :title, :string, :limit => 255
  33 + t.column :body, :text
29 34 t.column :type, :string, :limit => 255
30 35 end
31 36  
32 37 create_table :locked_pages_revisions, :force => true do |t|
33 38 t.column :page_id, :integer
34   - t.column :version, :integer
  39 + t.column :lock_version, :integer
35 40 t.column :title, :string, :limit => 255
  41 + t.column :body, :text
36 42 t.column :version_type, :string, :limit => 255
37 43 t.column :updated_at, :datetime
38 44 end
  45 +
  46 + add_index :locked_pages_revisions, [:page_id, :lock_version], :unique => true
39 47  
40 48 create_table :widgets, :force => true do |t|
41 49 t.column :name, :string, :limit => 50
... ... @@ -51,10 +59,13 @@ ActiveRecord::Schema.define(:version =&gt; 0) do
51 59 t.column :updated_at, :datetime
52 60 end
53 61  
  62 + add_index :widget_versions, [:widget_id, :version], :unique => true
  63 +
54 64 create_table :landmarks, :force => true do |t|
55 65 t.column :name, :string
56 66 t.column :latitude, :float
57 67 t.column :longitude, :float
  68 + t.column :doesnt_trigger_version,:string
58 69 t.column :version, :integer
59 70 end
60 71  
... ... @@ -63,6 +74,9 @@ ActiveRecord::Schema.define(:version =&gt; 0) do
63 74 t.column :name, :string
64 75 t.column :latitude, :float
65 76 t.column :longitude, :float
  77 + t.column :doesnt_trigger_version,:string
66 78 t.column :version, :integer
67 79 end
  80 +
  81 + add_index :landmark_versions, [:landmark_id, :version], :unique => true
68 82 end
... ...
vendor/plugins/acts_as_versioned/test/versioned_test.rb
... ... @@ -17,13 +17,13 @@ class VersionedTest &lt; Test::Unit::TestCase
17 17 def test_saves_without_revision
18 18 p = pages(:welcome)
19 19 old_versions = p.versions.count
20   -
  20 +
21 21 p.save_without_revision
22   -
  22 +
23 23 p.without_revision do
24 24 p.update_attributes :title => 'changed'
25 25 end
26   -
  26 +
27 27 assert_equal old_versions, p.versions.count
28 28 end
29 29  
... ... @@ -31,8 +31,8 @@ class VersionedTest &lt; Test::Unit::TestCase
31 31 p = pages(:welcome)
32 32 assert_equal 24, p.version
33 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 36 assert_equal 23, p.version
37 37 assert_equal 'Welcome to the weblg', p.title
38 38 end
... ... @@ -58,12 +58,12 @@ class VersionedTest &lt; Test::Unit::TestCase
58 58 p = pages(:welcome)
59 59 assert_equal 24, p.version
60 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 63 assert_equal 23, p.version
64 64 assert_equal 'Welcome to the weblg', p.title
65 65 end
66   -
  66 +
67 67 def test_rollback_fails_with_invalid_revision
68 68 p = locked_pages(:welcome)
69 69 assert !p.revert_to!(locked_pages(:thinking))
... ... @@ -75,27 +75,27 @@ class VersionedTest &lt; Test::Unit::TestCase
75 75 assert_equal 1, p.versions.size
76 76 assert_instance_of LockedPage.versioned_class, p.versions.first
77 77 end
78   -
  78 +
79 79 def test_rollback_with_version_number_with_options
80 80 p = locked_pages(:welcome)
81 81 assert_equal 'Welcome to the weblog', p.title
82 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 85 assert_equal 'Welcome to the weblg', p.title
86 86 assert_equal 'LockedPage', p.versions.first.version_type
87 87 end
88   -
  88 +
89 89 def test_rollback_with_version_class_with_options
90 90 p = locked_pages(:welcome)
91 91 assert_equal 'Welcome to the weblog', p.title
92 92 assert_equal 'LockedPage', p.versions.first.version_type
93   -
  93 +
94 94 assert p.revert_to!(p.versions.first), "Couldn't revert to 1"
95 95 assert_equal 'Welcome to the weblg', p.title
96 96 assert_equal 'LockedPage', p.versions.first.version_type
97 97 end
98   -
  98 +
99 99 def test_saves_versioned_copy_with_sti
100 100 p = SpecialLockedPage.create! :title => 'first title'
101 101 assert !p.new_record?
... ... @@ -103,12 +103,12 @@ class VersionedTest &lt; Test::Unit::TestCase
103 103 assert_instance_of LockedPage.versioned_class, p.versions.first
104 104 assert_equal 'SpecialLockedPage', p.versions.first.version_type
105 105 end
106   -
  106 +
107 107 def test_rollback_with_version_number_with_sti
108 108 p = locked_pages(:thinking)
109 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 112 assert_equal 'So I was thinking!!!', p.title
113 113 assert_equal 'SpecialLockedPage', p.versions.first.version_type
114 114 end
... ... @@ -116,11 +116,11 @@ class VersionedTest &lt; Test::Unit::TestCase
116 116 def test_lock_version_works_with_versioning
117 117 p = locked_pages(:thinking)
118 118 p2 = LockedPage.find(p.id)
119   -
  119 +
120 120 p.title = 'fresh title'
121 121 p.save
122 122 assert_equal 2, p.versions.size # limit!
123   -
  123 +
124 124 assert_raises(ActiveRecord::StaleObjectError) do
125 125 p2.title = 'stale title'
126 126 p2.save
... ... @@ -130,13 +130,13 @@ class VersionedTest &lt; Test::Unit::TestCase
130 130 def test_version_if_condition
131 131 p = Page.create! :title => "title"
132 132 assert_equal 1, p.version
133   -
  133 +
134 134 Page.feeling_good = false
135 135 p.save
136 136 assert_equal 1, p.version
137 137 Page.feeling_good = true
138 138 end
139   -
  139 +
140 140 def test_version_if_condition2
141 141 # set new if condition
142 142 Page.class_eval do
... ... @@ -144,40 +144,40 @@ class VersionedTest &lt; Test::Unit::TestCase
144 144 alias_method :old_feeling_good, :feeling_good?
145 145 alias_method :feeling_good?, :new_feeling_good
146 146 end
147   -
  147 +
148 148 p = Page.create! :title => "title"
149 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 152 p.update_attributes(:title => 'new title')
153 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 156 p.update_attributes(:title => 'a title')
157 157 assert_equal 2, p.version
158   - assert_equal 2, p.versions(true).size
159   -
  158 + assert_equal 2, p.versions.count
  159 +
160 160 # reset original if condition
161 161 Page.class_eval { alias_method :feeling_good?, :old_feeling_good }
162 162 end
163   -
  163 +
164 164 def test_version_if_condition_with_block
165 165 # set new if condition
166 166 old_condition = Page.version_condition
167 167 Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' }
168   -
  168 +
169 169 p = Page.create! :title => "title"
170 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 173 p.update_attributes(:title => 'a title')
174 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 177 p.update_attributes(:title => 'b title')
178 178 assert_equal 2, p.version
179   - assert_equal 2, p.versions(true).size
180   -
  179 + assert_equal 2, p.versions.count
  180 +
181 181 # reset original if condition
182 182 Page.version_condition = old_condition
183 183 end
... ... @@ -187,7 +187,10 @@ class VersionedTest &lt; Test::Unit::TestCase
187 187 p.save
188 188 p.save
189 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 194 end
192 195 end
193 196  
... ... @@ -196,33 +199,31 @@ class VersionedTest &lt; Test::Unit::TestCase
196 199 p.update_attributes(:title => "title1")
197 200 p.update_attributes(:title => "title2")
198 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 206 assert p.versions(true).size <= 2, "locked version can only store 2 versions"
201 207 end
202 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 214 end
214   -
215   - def test_track_changed_attributes
  215 +
  216 + def test_track_altered_attributes
216 217 p = LockedPage.create! :title => "title"
217 218 assert_equal 1, p.lock_version
218 219 assert_equal 1, p.versions(true).size
219   -
220   - p.title = 'title'
  220 +
  221 + p.body = 'whoa'
221 222 assert !p.save_version?
222 223 p.save
223 224 assert_equal 2, p.lock_version # still increments version because of optimistic locking
224 225 assert_equal 1, p.versions(true).size
225   -
  226 +
226 227 p.title = 'updated title'
227 228 assert p.save_version?
228 229 p.save
... ... @@ -235,22 +236,15 @@ class VersionedTest &lt; Test::Unit::TestCase
235 236 assert_equal 4, p.lock_version
236 237 assert_equal 2, p.versions(true).size # version 1 deleted
237 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 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 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 248 def test_with_sequence
255 249 assert_equal 'widgets_seq', Widget.versioned_class.sequence_name
256 250 3.times { Widget.create! :name => 'new widget' }
... ... @@ -265,25 +259,24 @@ class VersionedTest &lt; Test::Unit::TestCase
265 259 def test_has_many_through_with_custom_association
266 260 assert_equal [authors(:caged), authors(:mly)], pages(:welcome).revisors
267 261 end
268   -
  262 +
269 263 def test_referential_integrity
270 264 pages(:welcome).destroy
271 265 assert_equal 0, Page.count
272 266 assert_equal 0, Page::Version.count
273 267 end
274   -
  268 +
275 269 def test_association_options
276 270 association = Page.reflect_on_association(:versions)
277 271 options = association.options
278 272 assert_equal :delete_all, options[:dependent]
279   - assert_equal 'version', options[:order]
280   -
  273 +
281 274 association = Widget.reflect_on_association(:versions)
282 275 options = association.options
283 276 assert_equal :nullify, options[:dependent]
284 277 assert_equal 'version desc', options[:order]
285 278 assert_equal 'widget_id', options[:foreign_key]
286   -
  279 +
287 280 widget = Widget.create! :name => 'new widget'
288 281 assert_equal 1, Widget.count
289 282 assert_equal 1, Widget.versioned_class.count
... ... @@ -297,12 +290,12 @@ class VersionedTest &lt; Test::Unit::TestCase
297 290 page_version = page.versions.last
298 291 assert_equal page, page_version.page
299 292 end
300   -
301   - def test_unchanged_attributes
  293 +
  294 + def test_unaltered_attributes
302 295 landmarks(:washington).attributes = landmarks(:washington).attributes.except("id")
303 296 assert !landmarks(:washington).changed?
304 297 end
305   -
  298 +
306 299 def test_unchanged_string_attributes
307 300 landmarks(:washington).attributes = landmarks(:washington).attributes.except("id").inject({}) { |params, (key, value)| params.update(key => value.to_s) }
308 301 assert !landmarks(:washington).changed?
... ... @@ -311,18 +304,67 @@ class VersionedTest &lt; Test::Unit::TestCase
311 304 def test_should_find_earliest_version
312 305 assert_equal page_versions(:welcome_1), pages(:welcome).versions.earliest
313 306 end
314   -
  307 +
315 308 def test_should_find_latest_version
316 309 assert_equal page_versions(:welcome_2), pages(:welcome).versions.latest
317 310 end
318   -
  311 +
319 312 def test_should_find_previous_version
320 313 assert_equal page_versions(:welcome_1), page_versions(:welcome_2).previous
321 314 assert_equal page_versions(:welcome_1), pages(:welcome).versions.before(page_versions(:welcome_2))
322 315 end
323   -
  316 +
324 317 def test_should_find_next_version
325 318 assert_equal page_versions(:welcome_2), page_versions(:welcome_1).next
326 319 assert_equal page_versions(:welcome_2), pages(:welcome).versions.after(page_versions(:welcome_1))
327 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 370 end
329 371 \ No newline at end of file
... ...