diff options
10 files changed, 235 insertions, 212 deletions
diff --git a/vendor/plugins/acts_as_versioned/CHANGELOG b/vendor/plugins/acts_as_versioned/CHANGELOG index a5d339cc7..01882d767 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/lib/acts_as_versioned.rb b/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb index a46807df5..5299e0dc7 100644 --- a/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb +++ b/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb @@ -22,7 +22,7 @@ module ActiveRecord #:nodoc: module Acts #:nodoc: # Specify this act if you want to save a copy of the row in a versioned table. This assumes there is a - # versioned table ready and that your model has a version field. This works with optimisic locking if the lock_version + # versioned table ready and that your model has a version field. This works with optimistic locking if the lock_version # column is present as well. # # The class for the versioned model is derived the first time it is seen. Therefore, if you change your database schema you have to restart @@ -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,7 +95,7 @@ module ActiveRecord #:nodoc: # end # # * <tt>if_changed</tt> - 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. + # 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) @@ -148,7 +148,7 @@ module ActiveRecord #:nodoc: # # that create_table does # Post.create_versioned_table # end - # + # # def self.down # Post.drop_versioned_table # end @@ -172,21 +172,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 +185,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, 'version', 'lock_version', versioned_inheritance_column, 'created_at', 'created_on'] 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,7 +197,7 @@ 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 @@ -217,30 +205,24 @@ module ActiveRecord #:nodoc: has_many :versions, version_association_options do # finds earliest version of this record def earliest - @earliest ||= find(:first) + @earliest ||= find(:first, :order => 'version') end - + # find latest version of this record def latest @latest ||= find(:first, :order => 'version 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] end - + include options[:extend] if options[:extend].is_a?(Module) end @@ -252,22 +234,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 +264,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.version = 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 <tt>acts_as_versioned</tt>. @@ -304,24 +288,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 ["version <= ? 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) end self.clone_versioned_model(version, self) - self.send("#{self.class.version_column}=", version.version) + send("#{self.class.version_column}=", version.version) 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 +325,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.map(&:to_s) - 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.attributes[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 <tt>version_condition_met?</tt> and <tt>changed?</tt>. 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 +358,7 @@ module ActiveRecord #:nodoc: version_condition.call(self) else version_condition - end + end end # Executes the block with the versioning callbacks disabled. @@ -412,50 +383,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).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 <tt>find</tt> - 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 @@ -467,36 +412,40 @@ module ActiveRecord #:nodoc: if !self.content_columns.find { |c| %w(version lock_version).include? c.name } self.connection.add_column table_name, :version, :integer end - + self.connection.create_table(versioned_table_name, create_table_options) do |t| t.column versioned_foreign_key, :integer t.column :version, :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 + :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 + :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 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 @@ -531,7 +480,7 @@ module ActiveRecord #:nodoc: result = block.call ActiveRecord::Base.lock_optimistically = true if current result - end + end end end end diff --git a/vendor/plugins/acts_as_versioned/test/abstract_unit.rb b/vendor/plugins/acts_as_versioned/test/abstract_unit.rb index 177302e58..269667ad0 100644 --- a/vendor/plugins/acts_as_versioned/test/abstract_unit.rb +++ b/vendor/plugins/acts_as_versioned/test/abstract_unit.rb @@ -2,14 +2,26 @@ $:.unshift(File.dirname(__FILE__) + '/../../../rails/activesupport/lib') $:.unshift(File.dirname(__FILE__) + '/../../../rails/activerecord/lib') $:.unshift(File.dirname(__FILE__) + '/../lib') require 'test/unit' -require 'active_support' -require 'active_record' -require 'active_record/fixtures' +begin + require 'active_support' + require 'active_record' + require 'active_record/fixtures' +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 46d96176a..cf0639006 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/migrations/1_add_versioned_tables.rb b/vendor/plugins/acts_as_versioned/test/fixtures/migrations/1_add_versioned_tables.rb index 9512b5e82..5007b16ad 100644 --- a/vendor/plugins/acts_as_versioned/test/fixtures/migrations/1_add_versioned_tables.rb +++ b/vendor/plugins/acts_as_versioned/test/fixtures/migrations/1_add_versioned_tables.rb @@ -2,6 +2,8 @@ class AddVersionedTables < ActiveRecord::Migration def self.up create_table("things") do |t| t.column :title, :text + t.column :price, :decimal, :precision => 7, :scale => 2 + t.column :type, :string end Thing.create_versioned_table end diff --git a/vendor/plugins/acts_as_versioned/test/fixtures/pages.yml b/vendor/plugins/acts_as_versioned/test/fixtures/pages.yml index 07ac51f97..9f4ab546a 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/fixtures/widget.rb b/vendor/plugins/acts_as_versioned/test/fixtures/widget.rb index 3c38f2fcf..086ac2b40 100644 --- a/vendor/plugins/acts_as_versioned/test/fixtures/widget.rb +++ b/vendor/plugins/acts_as_versioned/test/fixtures/widget.rb @@ -1,6 +1,6 @@ class Widget < ActiveRecord::Base acts_as_versioned :sequence_name => 'widgets_seq', :association_options => { - :dependent => nil, :order => 'version desc' + :dependent => :nullify, :order => 'version desc' } non_versioned_columns << 'foo' end
\ 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 d85e95883..47a7537ce 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 @@ -21,8 +26,17 @@ if ActiveRecord::Base.connection.supports_migrations? assert_raises(ActiveRecord::StatementInvalid) { Thing.create :title => 'blah blah' } # take 'er up ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/') - t = Thing.create :title => 'blah blah' + t = Thing.create :title => 'blah blah', :price => 123.45, :type => 'Thing' assert_equal 1, t.versions.size + + # check that the price column has remembered its value correctly + assert_equal t.price, t.versions.first.price + assert_equal t.title, t.versions.first.title + assert_equal t[:type], t.versions.first[:type] + + # make sure that the precision of the price column has been preserved + assert_equal 7, Thing::Version.columns.find{|c| c.name == "price"}.precision + assert_equal 2, Thing::Version.columns.find{|c| c.name == "price"}.scale # now lets take 'er back down ActiveRecord::Migrator.down(File.dirname(__FILE__) + '/fixtures/migrations/') diff --git a/vendor/plugins/acts_as_versioned/test/schema.rb b/vendor/plugins/acts_as_versioned/test/schema.rb index 7d5153d07..4e7e96319 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 @@ -36,6 +40,8 @@ ActiveRecord::Schema.define(:version => 0) do t.column :version_type, :string, :limit => 255 t.column :updated_at, :datetime end + + add_index :locked_pages_revisions, [:page_id, :version], :unique => true create_table :widgets, :force => true do |t| t.column :name, :string, :limit => 50 @@ -51,10 +57,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 +72,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 b912500f9..6ab9e739c 100644 --- a/vendor/plugins/acts_as_versioned/test/versioned_test.rb +++ b/vendor/plugins/acts_as_versioned/test/versioned_test.rb @@ -14,16 +14,23 @@ class VersionedTest < Test::Unit::TestCase assert_instance_of Page.versioned_class, p.versions.first end + def test_version_has_unique_created_at + p = pages(:welcome) + p.title = 'update me' + p.save! + assert_not_equal p.created_on, p.versions.latest.created_on + end + 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 +38,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 +65,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 +82,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_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,11 +110,11 @@ 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_equal 'So I was thinking!!!', p.title assert_equal 'SpecialLockedPage', p.versions.first.version_type @@ -116,11 +123,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 +137,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 +151,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 +194,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 +206,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' 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 +243,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 +266,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_nil options[:dependent] + 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,32 +297,56 @@ class VersionedTest < Test::Unit::TestCase page_version = page.versions.last assert_equal page, page_version.page end - - def test_unchanged_attributes - landmarks(:washington).attributes = landmarks(:washington).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.inject({}) { |params, (key, value)| params.update key => value.to_s } + landmarks(:washington).attributes = landmarks(:washington).attributes.except("id").inject({}) { |params, (key, value)| params.update(key => value.to_s) } assert !landmarks(:washington).changed? end 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 end
\ No newline at end of file |