Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/asacalow/acts_as_versioned
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed May 23, 2008
2 parents 1772766 + 7164b71 commit 4d0cb9f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
20 changes: 10 additions & 10 deletions lib/acts_as_versioned.rb
Expand Up @@ -66,7 +66,7 @@ module Acts #: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_on_create, :save_version?, :clear_altered_attributes]
def self.included(base) # :nodoc:
base.extend ClassMethods
end
Expand Down Expand Up @@ -174,7 +174,7 @@ def acts_as_versioned(options = {}, &extension)
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_column, :max_version_limit, :track_altered_attributes, :version_condition, :version_sequence_name, :non_versioned_columns,
:version_association_options

# legacy
Expand All @@ -186,7 +186,7 @@ class << self
alias_method :non_versioned_fields=, :non_versioned_columns=
end

send :attr_accessor, :changed_attributes
send :attr_accessor, :altered_attributes

self.versioned_class_name = options[:class_name] || "Version"
self.versioned_foreign_key = options[:foreign_key] || self.to_s.foreign_key
Expand Down Expand Up @@ -228,10 +228,10 @@ def latest
after_create :save_version_on_create
after_update :save_version
after_save :clear_old_versions
after_save :clear_changed_attributes
after_save :clear_altered_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|
Expand Down Expand Up @@ -364,8 +364,8 @@ def versioned_attributes
# 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))
(!self.class.track_altered_attributes || (altered_attributes && altered_attributes.length > 0)) :
(altered_attributes && altered_attributes.include?(attr_name.to_s))
end

# keep old dirty? method
Expand Down Expand Up @@ -437,14 +437,14 @@ def next_version
end

# clears current changed attributes. Called after save.
def clear_changed_attributes
self.changed_attributes = []
def clear_altered_attributes
self.altered_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
(self.altered_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)
end

Expand Down
2 changes: 1 addition & 1 deletion test/abstract_unit.rb
Expand Up @@ -14,7 +14,7 @@

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")
Expand Down
13 changes: 9 additions & 4 deletions test/migration_test.rb
Expand Up @@ -8,10 +8,15 @@ class Thing < ActiveRecord::Base

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
Expand Down
12 changes: 6 additions & 6 deletions test/versioned_test.rb
Expand Up @@ -201,18 +201,18 @@ def test_version_max_limit
end
end

def test_track_changed_attributes_default_value
assert !Page.track_changed_attributes
assert LockedPage.track_changed_attributes
assert SpecialLockedPage.track_changed_attributes
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_version_order
assert_equal 23, pages(:welcome).versions.first.version
assert_equal 24, pages(:welcome).versions.last.version
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
Expand Down Expand Up @@ -311,7 +311,7 @@ def test_versioned_records_should_belong_to_parent
assert_equal page, page_version.page
end

def test_unchanged_attributes
def test_unaltered_attributes
landmarks(:washington).attributes = landmarks(:washington).attributes.except("id")
assert !landmarks(:washington).changed?
end
Expand Down

0 comments on commit 4d0cb9f

Please sign in to comment.