Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/francis/acts_as_versioned
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Oct 3, 2008
2 parents 49463ee + 41c26cd commit d010b49
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 29 deletions.
44 changes: 23 additions & 21 deletions lib/acts_as_versioned.rb
Expand Up @@ -95,12 +95,7 @@ module ClassMethods
# 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.
# 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.
#
# * <tt>extend</tt> - 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:
Expand Down Expand Up @@ -185,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, 'version', 'lock_version', versioned_inheritance_column, 'created_at', 'created_on']
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.version_association_options = {
:class_name => "#{self.to_s}::#{versioned_class_name}",
:foreign_key => versioned_foreign_key,
Expand All @@ -201,16 +196,16 @@ def acts_as_versioned(options = {}, &extension)
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, :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 All @@ -220,11 +215,11 @@ def latest
unless options[:if_changed].nil?
self.track_altered_attributes = true
options[:if_changed] = [options[:if_changed]] unless options[:if_changed].is_a?(Array)
self.version_if_changed = options[:if_changed]
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
Expand Down Expand Up @@ -262,6 +257,8 @@ 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 All @@ -276,7 +273,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 +285,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 +294,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 All @@ -327,7 +324,7 @@ def save_without_revision!
end

def altered?
track_altered_attributes ? (version_if_changed.map(&:to_s) - changed).length < version_if_changed.length : changed?
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.
Expand Down Expand Up @@ -392,7 +389,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 @@ -409,13 +406,16 @@ def versioned_class
# 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.tables.include?(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
Expand All @@ -439,6 +439,8 @@ def create_versioned_table(create_table_options = {})
if updated_col.nil?
self.connection.add_column versioned_table_name, :updated_at, :timestamp
end

self.connection.create_index versioned_table_name, versioned_foreign_key
end

# Rake migration task to drop the versioned table
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 d010b49

Please sign in to comment.