Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

ActiveRecord 3.2 Compatibility #17

Merged
merged 2 commits into from

2 participants

@codez

The latest version fo acts_as_versioned is tied to ActiveRecord 3.0. In order to use acts_as_versioned with ActiveRecord 3.2, this patch changes the dependency version to >= 3.0. Like this, forthcoming versions of ActiveRecord can still use this gem.

Additionally, I updated the code slightly to get rid of AR 3.2 deprecation warnings.

It would be great if you could release a new version of acts_as_versioned with these changes.

Cheers
Pascal

@technoweenie technoweenie merged commit 63b1fc8 into technoweenie:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 28, 2012
  1. @codez

    fix dependency format

    codez authored
This page is out of date. Refresh to see the latest.
View
2  .gitignore
@@ -4,3 +4,5 @@ test/*.log
pkg
.rvmrc
*.sqlite3.db
+.project
+Gemfile.lock
View
6 Gemfile
@@ -1,7 +1,7 @@
source 'http://rubygems.org'
group :development do
- gem 'rails', '3.0.0.beta4'
- gem 'sqlite3-ruby', '1.3.1'
- gem 'mysql', '2.8.1'
+ gem 'rails', '>=3.0.9'
+ gem 'sqlite3-ruby'
+ gem 'mysql'
end
View
2  Rakefile
@@ -60,7 +60,7 @@ task :coverage do
sh "open coverage/index.html"
end
-require 'rake/rdoctask'
+require 'rdoc/task'
Rake::RDocTask.new do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = "#{name} #{version}"
View
8 acts_as_versioned.gemspec
@@ -14,7 +14,7 @@ Gem::Specification.new do |s|
## the sub! line in the Rakefile
s.name = 'acts_as_versioned'
s.version = '0.6.0'
- s.date = '2010-07-19'
+ s.date = '2012-03-28'
s.rubyforge_project = 'acts_as_versioned'
## Make sure your summary is short. The description may be as long
@@ -40,11 +40,11 @@ Gem::Specification.new do |s|
## List your runtime dependencies here. Runtime dependencies are those
## that are needed for an end user to actually USE your code.
- s.add_dependency('activerecord', ["~> 3.0.0.beta4"])
+ s.add_dependency('activerecord', [">= 3.0.9"])
## List your development dependencies here. Development dependencies are
## those that are only needed during development
- s.add_development_dependency('sqlite3-ruby', ["~> 1.3.1"])
+ s.add_development_dependency('sqlite3-ruby', [">= 1.3.1"])
## Leave this section as-is. It will be automatically generated from the
## contents of your Git repository via the gemspec task. DO NOT REMOVE
@@ -68,7 +68,7 @@ Gem::Specification.new do |s|
test/fixtures/landmarks.yml
test/fixtures/locked_pages.yml
test/fixtures/locked_pages_revisions.yml
- test/fixtures/migrations/1_add_versioned_tables.rb
+ test/fixtures/migrations/2_add_versioned_tables.rb
test/fixtures/page.rb
test/fixtures/page_versions.yml
test/fixtures/pages.yml
View
216 lib/acts_as_versioned.rb
@@ -249,12 +249,12 @@ def versions_count
versioned_class.cattr_accessor :original_class
versioned_class.original_class = self
- versioned_class.set_table_name versioned_table_name
+ versioned_class.table_name = versioned_table_name
versioned_class.belongs_to self.to_s.demodulize.underscore.to_sym,
:class_name => "::#{self.to_s}",
:foreign_key => versioned_foreign_key
versioned_class.send :include, options[:extend] if options[:extend].is_a?(Module)
- versioned_class.set_sequence_name version_sequence_name if version_sequence_name
+ versioned_class.sequence_name = version_sequence_name if version_sequence_name
end
module Behaviors
@@ -268,134 +268,136 @@ module Behaviors
after_save :clear_old_versions
end
- module InstanceMethods
- # Saves a version of the model in the versioned table. This is called in the after_save callback by default
- 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
+ # Saves a version of the model in the versioned table. This is called in the after_save callback by default
+ 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 <tt>acts_as_versioned</tt>.
- # Override this method to set your own criteria for clearing old versions.
- def clear_old_versions
- 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
- self.class.versioned_class.delete_all ["#{self.class.version_column} <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id]
- end
+ # Clears old revisions if a limit is set with the :limit option in <tt>acts_as_versioned</tt>.
+ # Override this method to set your own criteria for clearing old versions.
+ def clear_old_versions
+ 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
+ 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) == id and !version.new_record?
- else
- return false unless version = versions.where(self.class.version_column => version).first
- end
- self.clone_versioned_model(version, self)
- send("#{self.class.version_column}=", version.send(self.class.version_column))
- true
+ # 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) == id and !version.new_record?
+ else
+ return false unless version = versions.where(self.class.version_column => version).first
end
+ self.clone_versioned_model(version, self)
+ send("#{self.class.version_column}=", version.send(self.class.version_column))
+ true
+ end
- # 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
- end
+ # 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
+ end
- # Temporarily turns off Optimistic Locking while saving. Used when reverting so that a new version is not created.
- def save_without_revision
- save_without_revision!
- true
- rescue
- false
- end
+ # Temporarily turns off Optimistic Locking while saving. Used when reverting so that a new version is not created.
+ def save_without_revision
+ save_without_revision!
+ true
+ rescue
+ false
+ end
- def save_without_revision!
- without_locking do
- without_revision do
- save!
- end
+ def save_without_revision!
+ without_locking do
+ without_revision do
+ save!
end
end
+ end
- def altered?
- track_altered_attributes ? (version_if_changed - changed).length < version_if_changed.length : changed?
- end
-
- # Clones a model. Used when saving a new version or reverting a model's version.
- def clone_versioned_model(orig_model, new_model)
- self.class.versioned_columns.each do |col|
- new_model[col.name] = orig_model.send(col.name) if orig_model.has_attribute?(col.name)
- end
+ def altered?
+ track_altered_attributes ? (version_if_changed - changed).length < version_if_changed.length : changed?
+ 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
+ # Clones a model. Used when saving a new version or reverting a model's version.
+ def clone_versioned_model(orig_model, new_model)
+ self.class.versioned_columns.each do |col|
+ new_model[col.name] = orig_model.send(col.name) if orig_model.has_attribute?(col.name)
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? && altered?
+ clone_inheritance_column(orig_model, new_model)
+ end
+
+ def clone_inheritance_column(orig_model, new_model)
+ if orig_model.is_a?(self.class.versioned_class) && new_model.class.column_names.include?(new_model.class.inheritance_column.to_s)
+ 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.class.column_names.include?(self.class.versioned_inheritance_column.to_s)
+ new_model[self.class.versioned_inheritance_column] = orig_model[orig_model.class.inheritance_column]
end
+ 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?
- case
- when version_condition.is_a?(Symbol)
- send(version_condition)
- when version_condition.respond_to?(:call) && (version_condition.arity == 1 || version_condition.arity == -1)
- version_condition.call(self)
- else
- version_condition
- 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? && altered?
+ end
- # Executes the block with the versioning callbacks disabled.
- #
- # @foo.without_revision do
- # @foo.save
- # end
- #
- def without_revision(&block)
- self.class.without_revision(&block)
+ # 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?
+ case
+ when version_condition.is_a?(Symbol)
+ send(version_condition)
+ when version_condition.respond_to?(:call) && (version_condition.arity == 1 || version_condition.arity == -1)
+ version_condition.call(self)
+ else
+ version_condition
end
+ end
- # Turns off optimistic locking for the duration of the block
- #
- # @foo.without_locking do
- # @foo.save
- # end
- #
- def without_locking(&block)
- self.class.without_locking(&block)
- end
+ # Executes the block with the versioning callbacks disabled.
+ #
+ # @foo.without_revision do
+ # @foo.save
+ # end
+ #
+ def without_revision(&block)
+ self.class.without_revision(&block)
+ end
- def empty_callback()
- end
+ # Turns off optimistic locking for the duration of the block
+ #
+ # @foo.without_locking do
+ # @foo.save
+ # end
+ #
+ def without_locking(&block)
+ self.class.without_locking(&block)
+ end
- #:nodoc:
+ def empty_callback()
+ end
- 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
- @saving_version = new_record? || save_version?
- self.send("#{self.class.version_column}=", next_version) if new_record? || (!locking_enabled? && save_version?)
- end
+ #:nodoc:
- # Gets the next available version for the current record, or 1 for a new record
- def next_version
- (new_record? ? 0 : versions.calculate(:maximum, version_column).to_i) + 1
- end
+ 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
+ @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
+ (new_record? ? 0 : versions.calculate(:maximum, version_column).to_i) + 1
end
module ClassMethods
View
0  ...ures/migrations/1_add_versioned_tables.rb → ...ures/migrations/2_add_versioned_tables.rb
File renamed without changes
View
1  test/migration_test.rb
@@ -19,7 +19,6 @@ def teardown
Thing.connection.drop_table "things" rescue nil
Thing.connection.drop_table "thing_versions" rescue nil
- Thing.reset_column_information
end
def test_versioned_migration
Something went wrong with that request. Please try again.