From 3d6c66d50936b34772c0e47e394d46efe24cffd1 Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Thu, 18 Jun 2009 23:31:01 -0300 Subject: [PATCH] Making the user unit test pass --- db/schema.rb | 6 +++--- test/unit/user_test.rb | 6 ++++-- vendor/plugins/acts_as_versioned/CHANGELOG | 10 +++++++++- vendor/plugins/acts_as_versioned/Rakefile | 2 -- vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb | 227 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------------------------------------------------------------------------------- vendor/plugins/acts_as_versioned/test/abstract_unit.rb | 9 ++++++++- vendor/plugins/acts_as_versioned/test/fixtures/landmarks.yml | 1 + vendor/plugins/acts_as_versioned/test/fixtures/locked_pages_revisions.yml | 8 ++++---- vendor/plugins/acts_as_versioned/test/fixtures/pages.yml | 3 ++- vendor/plugins/acts_as_versioned/test/migration_test.rb | 13 +++++++++---- vendor/plugins/acts_as_versioned/test/schema.rb | 16 +++++++++++++++- vendor/plugins/acts_as_versioned/test/versioned_test.rb | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------------------------------------------------ 12 files changed, 259 insertions(+), 240 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index aaf835c..bf8a6f9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,5 +1,5 @@ # This file is auto-generated from the current state of the database. Instead of editing this file, -# please use the migrations feature of ActiveRecord to incrementally modify your database, and +# please use the migrations feature of Active Record to incrementally modify your database, and # then regenerate this schema definition. # # 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 t.boolean "virtual", :default => false end - add_index "categories_profiles", ["category_id"], :name => "index_categories_profiles_on_category_id" add_index "categories_profiles", ["profile_id"], :name => "index_categories_profiles_on_profile_id" + add_index "categories_profiles", ["category_id"], :name => "index_categories_profiles_on_category_id" create_table "comments", :force => true do |t| t.string "title" @@ -211,8 +211,8 @@ ActiveRecord::Schema.define(:version => 69) do t.datetime "updated_at" end - add_index "product_categorizations", ["category_id"], :name => "index_product_categorizations_on_category_id" add_index "product_categorizations", ["product_id"], :name => "index_product_categorizations_on_product_id" + add_index "product_categorizations", ["category_id"], :name => "index_product_categorizations_on_category_id" create_table "products", :force => true do |t| t.integer "enterprise_id" diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 165dda0..911cb0b 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -164,7 +164,8 @@ class UserTest < Test::Unit::TestCase def test_should_encrypt_password_with_salted_sha1 user = User.new(:login => 'lalala', :email => 'lalala@example.com', :password => 'test', :password_confirmation => 'test') - user.build_person(person_data) +#TODO UPGRADE Leandro: I comment this code. The user model already create a person model +# user.build_person(person_data) user.stubs(:salt).returns('testsalt') user.save! @@ -295,7 +296,8 @@ class UserTest < Test::Unit::TestCase protected def new_user(options = {}) user = User.new({ :login => 'quire', :email => 'quire@example.com', :password => 'quire', :password_confirmation => 'quire' }.merge(options)) - user.build_person(person_data) +#TODO UPGRADE Leandro: I comment this code. The user model already create a person model +# user.build_person(person_data) user.save user end diff --git a/vendor/plugins/acts_as_versioned/CHANGELOG b/vendor/plugins/acts_as_versioned/CHANGELOG index a5d339c..01882d7 100644 --- a/vendor/plugins/acts_as_versioned/CHANGELOG +++ b/vendor/plugins/acts_as_versioned/CHANGELOG @@ -1,4 +1,12 @@ -*SVN* (version numbers are overrated) +*GIT* (version numbers are overrated) + +* (16 Jun 2008) Backwards Compatibility is overrated (big updates for rails 2.1) + + * Use ActiveRecord 2.1's dirty attribute checking instead [Asa Calow] + * Remove last traces of #non_versioned_fields + * Remove AR::Base.find_version and AR::Base.find_versions, rely on AR association proxies and named_scope + * Remove #versions_count, rely on AR association counter caching. + * Remove #versioned_attributes, basically the same as AR::Base.versioned_columns * (5 Oct 2006) Allow customization of #versions association options [Dan Peterson] diff --git a/vendor/plugins/acts_as_versioned/Rakefile b/vendor/plugins/acts_as_versioned/Rakefile index 3ae69e9..e557a1b 100644 --- a/vendor/plugins/acts_as_versioned/Rakefile +++ b/vendor/plugins/acts_as_versioned/Rakefile @@ -1,7 +1,5 @@ require 'rubygems' -Gem::manage_gems - require 'rake/rdoctask' require 'rake/packagetask' require 'rake/gempackagetask' diff --git a/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb b/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb index b69abbe..e7a8ce7 100644 --- a/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb +++ b/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb @@ -66,7 +66,7 @@ module ActiveRecord #:nodoc: # # See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options module Versioned - CALLBACKS = [:set_new_version, :save_version_on_create, :save_version?, :clear_changed_attributes] + CALLBACKS = [:set_new_version, :save_version, :save_version?] def self.included(base) # :nodoc: base.extend ClassMethods end @@ -95,12 +95,7 @@ module ActiveRecord #:nodoc: # end # # * if_changed - Simple way of specifying attributes that are required to be changed before saving a model. This takes - # either a symbol or array of symbols. WARNING - This will attempt to overwrite any attribute setters you may have. - # Use this instead if you want to write your own attribute setters (and ignore if_changed): - # - # def name=(new_name) - # write_changed_attribute :name, new_name - # end + # either a symbol or array of symbols. # # * extend - Lets you specify a module to be mixed in both the original and versioned models. You can also just pass a block # to create an anonymous mixin: @@ -148,7 +143,7 @@ module ActiveRecord #:nodoc: # # that create_table does # Post.create_versioned_table # end - # + # # def self.down # Post.drop_versioned_table # end @@ -172,21 +167,10 @@ module ActiveRecord #:nodoc: return if self.included_modules.include?(ActiveRecord::Acts::Versioned::ActMethods) send :include, ActiveRecord::Acts::Versioned::ActMethods - - cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column, - :version_column, :max_version_limit, :track_changed_attributes, :version_condition, :version_sequence_name, :non_versioned_columns, - :version_association_options - - # legacy - alias_method :non_versioned_fields, :non_versioned_columns - alias_method :non_versioned_fields=, :non_versioned_columns= - class << self - alias_method :non_versioned_fields, :non_versioned_columns - alias_method :non_versioned_fields=, :non_versioned_columns= - end - - send :attr_accessor, :changed_attributes + cattr_accessor :versioned_class_name, :versioned_foreign_key, :versioned_table_name, :versioned_inheritance_column, + :version_column, :max_version_limit, :track_altered_attributes, :version_condition, :version_sequence_name, :non_versioned_columns, + :version_association_options, :version_if_changed self.versioned_class_name = options[:class_name] || "Version" self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key @@ -196,11 +180,10 @@ module ActiveRecord #:nodoc: self.version_sequence_name = options[:sequence_name] self.max_version_limit = options[:limit].to_i self.version_condition = options[:if] || true - self.non_versioned_columns = [self.primary_key, inheritance_column, 'version', 'lock_version', versioned_inheritance_column] + 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) self.version_association_options = { :class_name => "#{self.to_s}::#{versioned_class_name}", :foreign_key => versioned_foreign_key, - :order => 'version', :dependent => :delete_all }.merge(options[:association_options] || {}) @@ -209,40 +192,34 @@ module ActiveRecord #:nodoc: silence_warnings do self.const_set(extension_module_name, Module.new(&extension)) end - + options[:extend] = self.const_get(extension_module_name) end - class_eval do + class_eval <<-CLASS_METHODS has_many :versions, version_association_options do # finds earliest version of this record def earliest - @earliest ||= find(:first) + @earliest ||= find(:first, :order => '#{version_column}') end - + # find latest version of this record def latest - @latest ||= find(:first, :order => 'version desc') + @latest ||= find(:first, :order => '#{version_column} desc') end end before_save :set_new_version - after_create :save_version_on_create - after_update :save_version + after_save :save_version after_save :clear_old_versions - after_save :clear_changed_attributes - + unless options[:if_changed].nil? - self.track_changed_attributes = true + self.track_altered_attributes = true options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array) - options[:if_changed].each do |attr_name| - define_method("#{attr_name}=") do |value| - write_changed_attribute attr_name, value - end - end + self.version_if_changed = options[:if_changed].map(&:to_s) end - + include options[:extend] if options[:extend].is_a?(Module) - end + CLASS_METHODS # create the dynamic versioned model const_set(versioned_class_name, Class.new(ActiveRecord::Base)).class_eval do @@ -252,22 +229,26 @@ module ActiveRecord #:nodoc: find :first, :order => 'version desc', :conditions => ["#{original_class.versioned_foreign_key} = ? and version < ?", version.send(original_class.versioned_foreign_key), version.version] end - + # find first version after the given version. def self.after(version) find :first, :order => 'version', :conditions => ["#{original_class.versioned_foreign_key} = ? and version > ?", version.send(original_class.versioned_foreign_key), version.version] end - + def previous self.class.before(self) end - + def next self.class.after(self) end + + def versions_count + page.version + end end - + versioned_class.cattr_accessor :original_class versioned_class.original_class = self versioned_class.set_table_name versioned_table_name @@ -278,24 +259,22 @@ module ActiveRecord #:nodoc: versioned_class.set_sequence_name version_sequence_name if version_sequence_name end end - + module ActMethods def self.included(base) # :nodoc: base.extend ClassMethods end - - # Saves a version of the model if applicable - def save_version - save_version_on_create if save_version? - end - + # Saves a version of the model in the versioned table. This is called in the after_save callback by default - def save_version_on_create - rev = self.class.versioned_class.new - self.clone_versioned_model(self, rev) - rev.version = send(self.class.version_column) - rev.send("#{self.class.versioned_foreign_key}=", self.id) - rev.save + def save_version + if @saving_version + @saving_version = nil + rev = self.class.versioned_class.new + clone_versioned_model(self, rev) + rev.send("#{self.class.version_column}=", send(self.class.version_column)) + rev.send("#{self.class.versioned_foreign_key}=", id) + rev.save + end end # Clears old revisions if a limit is set with the :limit option in acts_as_versioned. @@ -304,24 +283,23 @@ module ActiveRecord #:nodoc: return if self.class.max_version_limit == 0 excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit if excess_baggage > 0 - sql = "DELETE FROM #{self.class.versioned_table_name} WHERE version <= #{excess_baggage} AND #{self.class.versioned_foreign_key} = #{self.id}" - self.class.versioned_class.connection.execute sql + self.class.versioned_class.delete_all ["#{self.class.version_column} <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id] end end # Reverts a model to a given version. Takes either a version number or an instance of the versioned model def revert_to(version) if version.is_a?(self.class.versioned_class) - return false unless version.send(self.class.versioned_foreign_key) == self.id and !version.new_record? + return false unless version.send(self.class.versioned_foreign_key) == id and !version.new_record? else - return false unless version = versions.find_by_version(version) + return false unless version = versions.send("find_by_#{self.class.version_column}", version) end self.clone_versioned_model(version, self) - self.send("#{self.class.version_column}=", version.version) + send("#{self.class.version_column}=", version.send(self.class.version_column)) true end - # Reverts a model to a given version and saves the model. + # Reverts a model to a given version and saves the model. # Takes either a version number or an instance of the versioned model def revert_to!(version) revert_to(version) ? save_without_revision : false @@ -342,41 +320,29 @@ module ActiveRecord #:nodoc: end end end - - # Returns an array of attribute keys that are versioned. See non_versioned_columns - def versioned_attributes - self.attributes.keys.select { |k| !self.class.non_versioned_columns.include?(k) } - end - # If called with no parameters, gets whether the current model has changed and needs to be versioned. - # If called with a single parameter, gets whether the parameter has changed. - def changed?(attr_name = nil) - attr_name.nil? ? - (!self.class.track_changed_attributes || (changed_attributes && changed_attributes.length > 0)) : - (changed_attributes && changed_attributes.include?(attr_name.to_s)) + def altered? + track_altered_attributes ? (version_if_changed - changed).length < version_if_changed.length : changed? end - - # keep old dirty? method - alias_method :dirty?, :changed? - + # Clones a model. Used when saving a new version or reverting a model's version. def clone_versioned_model(orig_model, new_model) - self.versioned_attributes.each do |key| - new_model.send("#{key}=", orig_model.send(key)) if orig_model.has_attribute?(key) + self.class.versioned_columns.each do |col| + new_model.send("#{col.name}=", orig_model.send(col.name)) if orig_model.has_attribute?(col.name) end - + if orig_model.is_a?(self.class.versioned_class) new_model[new_model.class.inheritance_column] = orig_model[self.class.versioned_inheritance_column] elsif new_model.is_a?(self.class.versioned_class) new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column] end end - + # Checks whether a new version shall be saved or not. Calls version_condition_met? and changed?. def save_version? - version_condition_met? && changed? + version_condition_met? && altered? end - + # Checks condition set in the :if option to check whether a revision should be created or not. Override this for # custom version condition checking. def version_condition_met? @@ -387,7 +353,7 @@ module ActiveRecord #:nodoc: version_condition.call(self) else version_condition - end + end end # Executes the block with the versioning callbacks disabled. @@ -412,50 +378,24 @@ module ActiveRecord #:nodoc: def empty_callback() end #:nodoc: - protected + protected # sets the new version before saving, unless you're using optimistic locking. In that case, let it take care of the version. def set_new_version - self.send("#{self.class.version_column}=", self.next_version) if new_record? || (!locking_enabled? && save_version?) + @saving_version = new_record? || save_version? + self.send("#{self.class.version_column}=", next_version) if new_record? || (!locking_enabled? && save_version?) end - + # Gets the next available version for the current record, or 1 for a new record def next_version - return 1 if new_record? - (versions.calculate(:max, :version) || 0) + 1 - end - - # clears current changed attributes. Called after save. - def clear_changed_attributes - self.changed_attributes = [] - end - - def write_changed_attribute(attr_name, attr_value) - # Convert to db type for comparison. Avoids failing Float<=>String comparisons. - attr_value_for_db = self.class.columns_hash[attr_name.to_s].type_cast(attr_value) - (self.changed_attributes ||= []) << attr_name.to_s unless self.changed?(attr_name) || self.send(attr_name) == attr_value_for_db - write_attribute(attr_name, attr_value_for_db) + (new_record? ? 0 : versions.calculate(:max, version_column).to_i) + 1 end module ClassMethods - # Finds a specific version of a specific row of this model - def find_version(id, version) - find_versions(id, - :conditions => ["#{versioned_foreign_key} = ? AND version = ?", id, version], - :limit => 1).first - end - - # Finds versions of a specific model. Takes an options hash like find - def find_versions(id, options = {}) - versioned_class.find :all, { - :conditions => ["#{versioned_foreign_key} = ?", id], - :order => 'version' }.merge(options) - end - # Returns an array of columns that are versioned. See non_versioned_columns def versioned_columns - self.columns.select { |c| !non_versioned_columns.include?(c.name) } + @versioned_columns ||= columns.select { |c| !non_versioned_columns.include?(c.name) } end - + # Returns an instance of the dynamic versioned model def versioned_class const_get versioned_class_name @@ -464,43 +404,42 @@ module ActiveRecord #:nodoc: # Rake migration task to create the versioned table using options passed to acts_as_versioned def create_versioned_table(create_table_options = {}) # create version column in main table if it does not exist - if !self.content_columns.find { |c| %w(version lock_version).include? c.name } - self.connection.add_column table_name, :version, :integer + if !self.content_columns.find { |c| [version_column.to_s, 'lock_version'].include? c.name } + self.connection.add_column table_name, version_column, :integer + self.reset_column_information end + + return if connection.table_exists?(versioned_table_name) self.connection.create_table(versioned_table_name, create_table_options) do |t| t.column versioned_foreign_key, :integer - t.column :version, :integer + t.column version_column, :integer end - - updated_col = nil + self.versioned_columns.each do |col| - updated_col = col if !updated_col && %(updated_at updated_on).include?(col.name) self.connection.add_column versioned_table_name, col.name, col.type, - :limit => col.limit, - :default => col.default, - :scale => col.scale, + :limit => col.limit, + :default => col.default, + :scale => col.scale, :precision => col.precision end - + if type_col = self.columns_hash[inheritance_column] self.connection.add_column versioned_table_name, versioned_inheritance_column, type_col.type, - :limit => type_col.limit, - :default => type_col.default, - :scale => type_col.scale, + :limit => type_col.limit, + :default => type_col.default, + :scale => type_col.scale, :precision => type_col.precision end - - if updated_col.nil? - self.connection.add_column versioned_table_name, :updated_at, :timestamp - end + + self.connection.add_index versioned_table_name, versioned_foreign_key end - + # Rake migration task to drop the versioned table def drop_versioned_table self.connection.drop_table versioned_table_name end - + # Executes the block with the versioning callbacks disabled. # # Foo.without_revision do @@ -532,14 +471,16 @@ module ActiveRecord #:nodoc: def without_locking(&block) current = ActiveRecord::Base.lock_optimistically ActiveRecord::Base.lock_optimistically = false if current - result = block.call - ActiveRecord::Base.lock_optimistically = true if current - result - end + begin + block.call + ensure + ActiveRecord::Base.lock_optimistically = true if current + end + end end end end end end -ActiveRecord::Base.send :include, ActiveRecord::Acts::Versioned \ No newline at end of file +ActiveRecord::Base.send :include, ActiveRecord::Acts::Versioned diff --git a/vendor/plugins/acts_as_versioned/test/abstract_unit.rb b/vendor/plugins/acts_as_versioned/test/abstract_unit.rb index 86df5e1..269667a 100644 --- a/vendor/plugins/acts_as_versioned/test/abstract_unit.rb +++ b/vendor/plugins/acts_as_versioned/test/abstract_unit.rb @@ -10,11 +10,18 @@ rescue LoadError require 'rubygems' retry end + +begin + require 'ruby-debug' + Debugger.start +rescue LoadError +end + require 'acts_as_versioned' config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") -ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite']} +ActiveRecord::Base.configurations = {'test' => config[ENV['DB'] || 'sqlite3']} ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test']) load(File.dirname(__FILE__) + "/schema.rb") diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/landmarks.yml b/vendor/plugins/acts_as_versioned/test/fixtures/landmarks.yml index 46d9617..cf06390 100644 --- a/vendor/plugins/acts_as_versioned/test/fixtures/landmarks.yml +++ b/vendor/plugins/acts_as_versioned/test/fixtures/landmarks.yml @@ -3,4 +3,5 @@ washington: name: Washington, D.C. latitude: 38.895 longitude: -77.036667 + doesnt_trigger_version: This is not important version: 1 diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/locked_pages_revisions.yml b/vendor/plugins/acts_as_versioned/test/fixtures/locked_pages_revisions.yml index 5c978e6..3a1be5a 100644 --- a/vendor/plugins/acts_as_versioned/test/fixtures/locked_pages_revisions.yml +++ b/vendor/plugins/acts_as_versioned/test/fixtures/locked_pages_revisions.yml @@ -2,26 +2,26 @@ welcome_1: id: 1 page_id: 1 title: Welcome to the weblg - version: 23 + lock_version: 23 version_type: LockedPage welcome_2: id: 2 page_id: 1 title: Welcome to the weblog - version: 24 + lock_version: 24 version_type: LockedPage thinking_1: id: 3 page_id: 2 title: So I was thinking!!! - version: 23 + lock_version: 23 version_type: SpecialLockedPage thinking_2: id: 4 page_id: 2 title: So I was thinking - version: 24 + lock_version: 24 version_type: SpecialLockedPage diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/pages.yml b/vendor/plugins/acts_as_versioned/test/fixtures/pages.yml index 07ac51f..9f4ab54 100644 --- a/vendor/plugins/acts_as_versioned/test/fixtures/pages.yml +++ b/vendor/plugins/acts_as_versioned/test/fixtures/pages.yml @@ -4,4 +4,5 @@ welcome: body: Such a lovely day version: 24 author_id: 1 - revisor_id: 1 \ No newline at end of file + revisor_id: 1 + created_on: "2008-01-01 00:00:00" \ No newline at end of file diff --git a/vendor/plugins/acts_as_versioned/test/migration_test.rb b/vendor/plugins/acts_as_versioned/test/migration_test.rb index 3cef741..47a7537 100644 --- a/vendor/plugins/acts_as_versioned/test/migration_test.rb +++ b/vendor/plugins/acts_as_versioned/test/migration_test.rb @@ -8,10 +8,15 @@ if ActiveRecord::Base.connection.supports_migrations? class MigrationTest < Test::Unit::TestCase self.use_transactional_fixtures = false - def teardown - ActiveRecord::Base.connection.initialize_schema_information - ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0" - + def teardown + if ActiveRecord::Base.connection.respond_to?(:initialize_schema_information) + ActiveRecord::Base.connection.initialize_schema_information + ActiveRecord::Base.connection.update "UPDATE schema_info SET version = 0" + else + ActiveRecord::Base.connection.initialize_schema_migrations_table + ActiveRecord::Base.connection.assume_migrated_upto_version(0) + end + Thing.connection.drop_table "things" rescue nil Thing.connection.drop_table "thing_versions" rescue nil Thing.reset_column_information diff --git a/vendor/plugins/acts_as_versioned/test/schema.rb b/vendor/plugins/acts_as_versioned/test/schema.rb index 7d5153d..49e6c0f 100644 --- a/vendor/plugins/acts_as_versioned/test/schema.rb +++ b/vendor/plugins/acts_as_versioned/test/schema.rb @@ -3,6 +3,7 @@ ActiveRecord::Schema.define(:version => 0) do t.column :version, :integer t.column :title, :string, :limit => 255 t.column :body, :text + t.column :created_on, :datetime t.column :updated_on, :datetime t.column :author_id, :integer t.column :revisor_id, :integer @@ -13,11 +14,14 @@ ActiveRecord::Schema.define(:version => 0) do t.column :version, :integer t.column :title, :string, :limit => 255 t.column :body, :text + t.column :created_on, :datetime t.column :updated_on, :datetime t.column :author_id, :integer t.column :revisor_id, :integer end + add_index :page_versions, [:page_id, :version], :unique => true + create_table :authors, :force => true do |t| t.column :page_id, :integer t.column :name, :string @@ -26,16 +30,20 @@ ActiveRecord::Schema.define(:version => 0) do create_table :locked_pages, :force => true do |t| t.column :lock_version, :integer t.column :title, :string, :limit => 255 + t.column :body, :text t.column :type, :string, :limit => 255 end create_table :locked_pages_revisions, :force => true do |t| t.column :page_id, :integer - t.column :version, :integer + t.column :lock_version, :integer t.column :title, :string, :limit => 255 + t.column :body, :text t.column :version_type, :string, :limit => 255 t.column :updated_at, :datetime end + + add_index :locked_pages_revisions, [:page_id, :lock_version], :unique => true create_table :widgets, :force => true do |t| t.column :name, :string, :limit => 50 @@ -51,10 +59,13 @@ ActiveRecord::Schema.define(:version => 0) do t.column :updated_at, :datetime end + add_index :widget_versions, [:widget_id, :version], :unique => true + create_table :landmarks, :force => true do |t| t.column :name, :string t.column :latitude, :float t.column :longitude, :float + t.column :doesnt_trigger_version,:string t.column :version, :integer end @@ -63,6 +74,9 @@ ActiveRecord::Schema.define(:version => 0) do t.column :name, :string t.column :latitude, :float t.column :longitude, :float + t.column :doesnt_trigger_version,:string t.column :version, :integer end + + add_index :landmark_versions, [:landmark_id, :version], :unique => true end diff --git a/vendor/plugins/acts_as_versioned/test/versioned_test.rb b/vendor/plugins/acts_as_versioned/test/versioned_test.rb index 1e22a69..9c4f2c9 100644 --- a/vendor/plugins/acts_as_versioned/test/versioned_test.rb +++ b/vendor/plugins/acts_as_versioned/test/versioned_test.rb @@ -17,13 +17,13 @@ class VersionedTest < Test::Unit::TestCase def test_saves_without_revision p = pages(:welcome) old_versions = p.versions.count - + p.save_without_revision - + p.without_revision do p.update_attributes :title => 'changed' end - + assert_equal old_versions, p.versions.count end @@ -31,8 +31,8 @@ class VersionedTest < Test::Unit::TestCase p = pages(:welcome) assert_equal 24, p.version assert_equal 'Welcome to the weblog', p.title - - assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23" + + assert p.revert_to!(23), "Couldn't revert to 23" assert_equal 23, p.version assert_equal 'Welcome to the weblg', p.title end @@ -58,12 +58,12 @@ class VersionedTest < Test::Unit::TestCase p = pages(:welcome) assert_equal 24, p.version assert_equal 'Welcome to the weblog', p.title - - assert p.revert_to!(p.versions.first), "Couldn't revert to 23" + + assert p.revert_to!(p.versions.find_by_version(23)), "Couldn't revert to 23" assert_equal 23, p.version assert_equal 'Welcome to the weblg', p.title end - + def test_rollback_fails_with_invalid_revision p = locked_pages(:welcome) assert !p.revert_to!(locked_pages(:thinking)) @@ -75,27 +75,27 @@ class VersionedTest < Test::Unit::TestCase assert_equal 1, p.versions.size assert_instance_of LockedPage.versioned_class, p.versions.first end - + def test_rollback_with_version_number_with_options p = locked_pages(:welcome) assert_equal 'Welcome to the weblog', p.title assert_equal 'LockedPage', p.versions.first.version_type - - assert p.revert_to!(p.versions.first.version), "Couldn't revert to 23" + + assert p.revert_to!(p.versions.first.lock_version), "Couldn't revert to 23" assert_equal 'Welcome to the weblg', p.title assert_equal 'LockedPage', p.versions.first.version_type end - + def test_rollback_with_version_class_with_options p = locked_pages(:welcome) assert_equal 'Welcome to the weblog', p.title assert_equal 'LockedPage', p.versions.first.version_type - + assert p.revert_to!(p.versions.first), "Couldn't revert to 1" assert_equal 'Welcome to the weblg', p.title assert_equal 'LockedPage', p.versions.first.version_type end - + def test_saves_versioned_copy_with_sti p = SpecialLockedPage.create! :title => 'first title' assert !p.new_record? @@ -103,12 +103,12 @@ class VersionedTest < Test::Unit::TestCase assert_instance_of LockedPage.versioned_class, p.versions.first assert_equal 'SpecialLockedPage', p.versions.first.version_type end - + def test_rollback_with_version_number_with_sti p = locked_pages(:thinking) assert_equal 'So I was thinking', p.title - - assert p.revert_to!(p.versions.first.version), "Couldn't revert to 1" + + assert p.revert_to!(p.versions.first.lock_version), "Couldn't revert to 1" assert_equal 'So I was thinking!!!', p.title assert_equal 'SpecialLockedPage', p.versions.first.version_type end @@ -116,11 +116,11 @@ class VersionedTest < Test::Unit::TestCase def test_lock_version_works_with_versioning p = locked_pages(:thinking) p2 = LockedPage.find(p.id) - + p.title = 'fresh title' p.save assert_equal 2, p.versions.size # limit! - + assert_raises(ActiveRecord::StaleObjectError) do p2.title = 'stale title' p2.save @@ -130,13 +130,13 @@ class VersionedTest < Test::Unit::TestCase def test_version_if_condition p = Page.create! :title => "title" assert_equal 1, p.version - + Page.feeling_good = false p.save assert_equal 1, p.version Page.feeling_good = true end - + def test_version_if_condition2 # set new if condition Page.class_eval do @@ -144,40 +144,40 @@ class VersionedTest < Test::Unit::TestCase alias_method :old_feeling_good, :feeling_good? alias_method :feeling_good?, :new_feeling_good end - + p = Page.create! :title => "title" assert_equal 1, p.version # version does not increment - assert_equal 1, p.versions(true).size - + assert_equal 1, p.versions.count + p.update_attributes(:title => 'new title') assert_equal 1, p.version # version does not increment - assert_equal 1, p.versions(true).size - + assert_equal 1, p.versions.count + p.update_attributes(:title => 'a title') assert_equal 2, p.version - assert_equal 2, p.versions(true).size - + assert_equal 2, p.versions.count + # reset original if condition Page.class_eval { alias_method :feeling_good?, :old_feeling_good } end - + def test_version_if_condition_with_block # set new if condition old_condition = Page.version_condition Page.version_condition = Proc.new { |page| page.title[0..0] == 'b' } - + p = Page.create! :title => "title" assert_equal 1, p.version # version does not increment - assert_equal 1, p.versions(true).size - + assert_equal 1, p.versions.count + p.update_attributes(:title => 'a title') assert_equal 1, p.version # version does not increment - assert_equal 1, p.versions(true).size - + assert_equal 1, p.versions.count + p.update_attributes(:title => 'b title') assert_equal 2, p.version - assert_equal 2, p.versions(true).size - + assert_equal 2, p.versions.count + # reset original if condition Page.version_condition = old_condition end @@ -187,7 +187,10 @@ class VersionedTest < Test::Unit::TestCase p.save p.save 5.times do |i| - assert_page_title p, i + p.title = "title#{i}" + p.save + assert_equal "title#{i}", p.title + assert_equal (i+2), p.version end end @@ -196,33 +199,31 @@ class VersionedTest < Test::Unit::TestCase p.update_attributes(:title => "title1") p.update_attributes(:title => "title2") 5.times do |i| - assert_page_title p, i, :lock_version + p.title = "title#{i}" + p.save + assert_equal "title#{i}", p.title + assert_equal (i+4), p.lock_version assert p.versions(true).size <= 2, "locked version can only store 2 versions" end end - - def test_track_changed_attributes_default_value - assert !Page.track_changed_attributes - assert LockedPage.track_changed_attributes - assert SpecialLockedPage.track_changed_attributes - end - - def test_version_order - assert_equal 23, pages(:welcome).versions.first.version - assert_equal 24, pages(:welcome).versions.last.version + + def test_track_altered_attributes_default_value + assert !Page.track_altered_attributes + assert LockedPage.track_altered_attributes + assert SpecialLockedPage.track_altered_attributes end - - def test_track_changed_attributes + + def test_track_altered_attributes p = LockedPage.create! :title => "title" assert_equal 1, p.lock_version assert_equal 1, p.versions(true).size - - p.title = 'title' + + p.body = 'whoa' assert !p.save_version? p.save assert_equal 2, p.lock_version # still increments version because of optimistic locking assert_equal 1, p.versions(true).size - + p.title = 'updated title' assert p.save_version? p.save @@ -235,22 +236,15 @@ class VersionedTest < Test::Unit::TestCase assert_equal 4, p.lock_version assert_equal 2, p.versions(true).size # version 1 deleted end - - def assert_page_title(p, i, version_field = :version) - p.title = "title#{i}" - p.save - assert_equal "title#{i}", p.title - assert_equal (i+4), p.send(version_field) - end - + def test_find_versions - assert_equal 2, locked_pages(:welcome).versions.size - assert_equal 1, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%weblog%']).length - assert_equal 2, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%web%']).length - assert_equal 0, locked_pages(:thinking).versions.find(:all, :conditions => ['title LIKE ?', '%web%']).length - assert_equal 2, locked_pages(:welcome).versions.length + assert_equal 1, locked_pages(:welcome).versions.find(:all, :conditions => ['title LIKE ?', '%weblog%']).size end - + + def test_find_version + assert_equal page_versions(:welcome_1), pages(:welcome).versions.find_by_version(23) + end + def test_with_sequence assert_equal 'widgets_seq', Widget.versioned_class.sequence_name 3.times { Widget.create! :name => 'new widget' } @@ -265,25 +259,24 @@ class VersionedTest < Test::Unit::TestCase def test_has_many_through_with_custom_association assert_equal [authors(:caged), authors(:mly)], pages(:welcome).revisors end - + def test_referential_integrity pages(:welcome).destroy assert_equal 0, Page.count assert_equal 0, Page::Version.count end - + def test_association_options association = Page.reflect_on_association(:versions) options = association.options assert_equal :delete_all, options[:dependent] - assert_equal 'version', options[:order] - + association = Widget.reflect_on_association(:versions) options = association.options assert_equal :nullify, options[:dependent] assert_equal 'version desc', options[:order] assert_equal 'widget_id', options[:foreign_key] - + widget = Widget.create! :name => 'new widget' assert_equal 1, Widget.count assert_equal 1, Widget.versioned_class.count @@ -297,12 +290,12 @@ class VersionedTest < Test::Unit::TestCase page_version = page.versions.last assert_equal page, page_version.page end - - def test_unchanged_attributes + + def test_unaltered_attributes landmarks(:washington).attributes = landmarks(:washington).attributes.except("id") assert !landmarks(:washington).changed? end - + def test_unchanged_string_attributes landmarks(:washington).attributes = landmarks(:washington).attributes.except("id").inject({}) { |params, (key, value)| params.update(key => value.to_s) } assert !landmarks(:washington).changed? @@ -311,18 +304,67 @@ class VersionedTest < Test::Unit::TestCase def test_should_find_earliest_version assert_equal page_versions(:welcome_1), pages(:welcome).versions.earliest end - + def test_should_find_latest_version assert_equal page_versions(:welcome_2), pages(:welcome).versions.latest end - + def test_should_find_previous_version assert_equal page_versions(:welcome_1), page_versions(:welcome_2).previous assert_equal page_versions(:welcome_1), pages(:welcome).versions.before(page_versions(:welcome_2)) end - + def test_should_find_next_version assert_equal page_versions(:welcome_2), page_versions(:welcome_1).next assert_equal page_versions(:welcome_2), pages(:welcome).versions.after(page_versions(:welcome_1)) end + + def test_should_find_version_count + assert_equal 2, pages(:welcome).versions.size + end + + def test_if_changed_creates_version_if_a_listed_column_is_changed + landmarks(:washington).name = "Washington" + assert landmarks(:washington).changed? + assert landmarks(:washington).altered? + end + + def test_if_changed_creates_version_if_all_listed_columns_are_changed + landmarks(:washington).name = "Washington" + landmarks(:washington).latitude = 1.0 + landmarks(:washington).longitude = 1.0 + assert landmarks(:washington).changed? + assert landmarks(:washington).altered? + end + + def test_if_changed_does_not_create_new_version_if_unlisted_column_is_changed + landmarks(:washington).doesnt_trigger_version = "This should not trigger version" + assert landmarks(:washington).changed? + assert !landmarks(:washington).altered? + end + + def test_without_locking_temporarily_disables_optimistic_locking + enabled1 = false + block_called = false + + ActiveRecord::Base.lock_optimistically = true + LockedPage.without_locking do + enabled1 = ActiveRecord::Base.lock_optimistically + block_called = true + end + enabled2 = ActiveRecord::Base.lock_optimistically + + assert block_called + assert !enabled1 + assert enabled2 + end + + def test_without_locking_reverts_optimistic_locking_settings_if_block_raises_exception + assert_raises(RuntimeError) do + LockedPage.without_locking do + raise RuntimeError, "oh noes" + end + end + assert ActiveRecord::Base.lock_optimistically + end end \ No newline at end of file -- libgit2 0.21.2