Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
tweaks to make the version_column name work on both the parent and ve…
…rsion table
  • Loading branch information
Francis Sullivan committed Sep 23, 2008
1 parent 4b387a1 commit b1b14b9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
20 changes: 10 additions & 10 deletions lib/acts_as_versioned.rb
Expand Up @@ -181,7 +181,7 @@ def acts_as_versioned(options = {}, &extension)
self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key
self.versioned_table_name = options[:table_name] || "#{table_name_prefix}#{base_class.name.demodulize.underscore}_versions#{table_name_suffix}"
self.versioned_inheritance_column = options[:inheritance_column] || "versioned_#{inheritance_column}"
self.version_column = options[:version_column] || 'version'
@@version_column = self.version_column = options[:version_column] || 'version'
self.version_sequence_name = options[:sequence_name]
self.max_version_limit = options[:limit].to_i
self.version_condition = options[:if] || true
Expand All @@ -205,12 +205,12 @@ def acts_as_versioned(options = {}, &extension)
has_many :versions, version_association_options do
# finds earliest version of this record
def earliest
@earliest ||= find(:first, :order => 'version')
@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
Expand Down Expand Up @@ -276,7 +276,7 @@ def save_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.version_column}=", send(self.class.version_column))
rev.send("#{self.class.versioned_foreign_key}=", id)
rev.save
end
Expand All @@ -288,7 +288,7 @@ 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 ["version <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id]
self.class.versioned_class.delete_all ["#{self.class.version_column} <= ? and #{self.class.versioned_foreign_key} = ?", excess_baggage, id]
end
end

Expand All @@ -297,10 +297,10 @@ 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.find_by_version(version)
return false unless version = versions.send("find_by_#{self.class.version_column}", version)
end
self.clone_versioned_model(version, self)
send("#{self.class.version_column}=", version.version)
send("#{self.class.version_column}=", version.send(self.class.version_column))
true
end

Expand Down Expand Up @@ -392,7 +392,7 @@ def set_new_version

# Gets the next available version for the current record, or 1 for a new record
def next_version
(new_record? ? 0 : versions.calculate(:max, :version).to_i) + 1
(new_record? ? 0 : versions.calculate(:max, version_column).to_i) + 1
end

module ClassMethods
Expand All @@ -410,12 +410,12 @@ def versioned_class
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
self.connection.add_column table_name, version_column, :integer
end

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
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/locked_pages_revisions.yml
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/schema.rb
Expand Up @@ -35,13 +35,13 @@

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 :version_type, :string, :limit => 255
t.column :updated_at, :datetime
end

add_index :locked_pages_revisions, [:page_id, :version], :unique => true
add_index :locked_pages_revisions, [:page_id, :lock_version], :unique => true

create_table :widgets, :force => true do |t|
t.column :name, :string, :limit => 50
Expand Down
4 changes: 2 additions & 2 deletions test/versioned_test.rb
Expand Up @@ -88,7 +88,7 @@ def test_rollback_with_version_number_with_options
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
Expand All @@ -115,7 +115,7 @@ 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
Expand Down

0 comments on commit b1b14b9

Please sign in to comment.