Skip to content

Commit

Permalink
Merge branch 'tech_master'
Browse files Browse the repository at this point in the history
Conflicts:

	lib/acts_as_versioned.rb
  • Loading branch information
David Genord II committed Mar 30, 2009
2 parents 663bdae + 1bd3d25 commit 2424b23
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 24 deletions.
4 changes: 4 additions & 0 deletions VERSION.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
:patch: 2
:major: 0
:minor: 5
29 changes: 29 additions & 0 deletions acts_as_versioned.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{acts_as_versioned}
s.version = "0.5.2"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["technoweenie"]
s.date = %q{2009-01-20}
s.description = %q{TODO}
s.email = %q{technoweenie@bidwell.textdrive.com}
s.files = ["VERSION.yml", "lib/acts_as_versioned.rb", "test/abstract_unit.rb", "test/database.yml", "test/fixtures", "test/fixtures/authors.yml", "test/fixtures/landmark.rb", "test/fixtures/landmark_versions.yml", "test/fixtures/landmarks.yml", "test/fixtures/locked_pages.yml", "test/fixtures/locked_pages_revisions.yml", "test/fixtures/migrations", "test/fixtures/migrations/1_add_versioned_tables.rb", "test/fixtures/page.rb", "test/fixtures/page_versions.yml", "test/fixtures/pages.yml", "test/fixtures/widget.rb", "test/migration_test.rb", "test/schema.rb", "test/versioned_test.rb"]
s.has_rdoc = true
s.homepage = %q{http://github.com/technoweenie/acts_as_versioned}
s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
s.require_paths = ["lib"]
s.rubygems_version = %q{1.3.1}
s.summary = %q{TODO}

if s.respond_to? :specification_version then
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
s.specification_version = 2

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
else
end
else
end
end
28 changes: 11 additions & 17 deletions lib/acts_as_versioned.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ def acts_as_versioned(options = {}, &extension)
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, self.version_column, 'lock_version', versioned_inheritance_column, 'created_at', 'created_on'] + options[:non_versioned_columns].to_a.map(&:to_s)
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,
Expand Down Expand Up @@ -257,8 +257,6 @@ def versions_count
: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

create_versioned_table
end
end

Expand Down Expand Up @@ -412,17 +410,15 @@ def create_versioned_table(migrator = self.connection, create_table_options = {}
self.reset_column_information
end

return if connection.tables.include?(versioned_table_name.to_s)
return if connection.table_exists?(versioned_table_name)

migrator.create_table(versioned_table_name, create_table_options) do |t|
t.column versioned_foreign_key, :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)
migrator.add_column versioned_table_name, col.name, col.type,
self.versioned_columns.each do |col|
self.connection.add_column versioned_table_name, col.name, col.type,
:limit => col.limit,
:default => col.default,
:scale => col.scale,
Expand All @@ -437,11 +433,7 @@ def create_versioned_table(migrator = self.connection, create_table_options = {}
:precision => type_col.precision
end

if updated_col.nil?
migrator.add_column versioned_table_name, :updated_at, :timestamp
end

migrator.create_index versioned_table_name, versioned_foreign_key
self.connection.add_index versioned_table_name, versioned_foreign_key
end

# Rake migration task to drop the versioned table
Expand Down Expand Up @@ -480,14 +472,16 @@ def without_revision(&block)
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
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
ActiveRecord::Base.send :include, ActiveRecord::Acts::Versioned
32 changes: 25 additions & 7 deletions test/versioned_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ def test_saves_versioned_copy
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
Expand Down Expand Up @@ -349,4 +342,29 @@ def test_if_changed_does_not_create_new_version_if_unlisted_column_is_changed
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

0 comments on commit 2424b23

Please sign in to comment.