1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

removes the obsolete private method column_methods_hash [Closes #11406]

This commit is contained in:
Xavier Noria 2013-07-16 14:19:24 +02:00
parent 1555a1800e
commit 9eb0cd28c9
4 changed files with 28 additions and 43 deletions

View file

@ -253,19 +253,6 @@ module ActiveRecord
@content_columns ||= columns.reject { |c| c.primary || c.name =~ /(_id|_count)$/ || c.name == inheritance_column }
end
# Returns a hash of all the methods added to query each of the columns in the table with the name of the method as the key
# and true as the value. This makes it possible to do O(1) lookups in respond_to? to check if a given method for attribute
# is available.
def column_methods_hash #:nodoc:
@dynamic_methods_hash ||= column_names.each_with_object(Hash.new(false)) do |attr, methods|
attr_name = attr.to_s
methods[attr.to_sym] = attr_name
methods["#{attr}=".to_sym] = attr_name
methods["#{attr}?".to_sym] = attr_name
methods["#{attr}_before_type_cast".to_sym] = attr_name
end
end
# Resets all the cached information about columns, which will cause them
# to be reloaded on the next request.
#

View file

@ -16,32 +16,23 @@ module ActiveRecord
end
def test_add_remove_single_field_using_string_arguments
assert_not TestModel.column_methods_hash.key?(:last_name)
assert_no_column TestModel, :last_name
add_column 'test_models', 'last_name', :string
TestModel.reset_column_information
assert TestModel.column_methods_hash.key?(:last_name)
assert_column TestModel, :last_name
remove_column 'test_models', 'last_name'
TestModel.reset_column_information
assert_not TestModel.column_methods_hash.key?(:last_name)
assert_no_column TestModel, :last_name
end
def test_add_remove_single_field_using_symbol_arguments
assert_not TestModel.column_methods_hash.key?(:last_name)
assert_no_column TestModel, :last_name
add_column :test_models, :last_name, :string
TestModel.reset_column_information
assert TestModel.column_methods_hash.key?(:last_name)
assert_column TestModel, :last_name
remove_column :test_models, :last_name
TestModel.reset_column_information
assert_not TestModel.column_methods_hash.key?(:last_name)
assert_no_column TestModel, :last_name
end
def test_unabstracted_database_dependent_types

View file

@ -177,20 +177,18 @@ class MigrationTest < ActiveRecord::TestCase
end
def test_filtering_migrations
assert !Person.column_methods_hash.include?(:last_name)
assert_no_column Person, :last_name
assert !Reminder.table_exists?
name_filter = lambda { |migration| migration.name == "ValidPeopleHaveLastNames" }
ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid", &name_filter)
Person.reset_column_information
assert Person.column_methods_hash.include?(:last_name)
assert_column Person, :last_name
assert_raise(ActiveRecord::StatementInvalid) { Reminder.first }
ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/valid", &name_filter)
Person.reset_column_information
assert !Person.column_methods_hash.include?(:last_name)
assert_no_column Person, :last_name
assert_raise(ActiveRecord::StatementInvalid) { Reminder.first }
end
@ -237,7 +235,7 @@ class MigrationTest < ActiveRecord::TestCase
skip "not supported on #{ActiveRecord::Base.connection.class}"
end
assert_not Person.column_methods_hash.include?(:last_name)
assert_no_column Person, :last_name
migration = Class.new(ActiveRecord::Migration) {
def version; 100 end
@ -253,8 +251,7 @@ class MigrationTest < ActiveRecord::TestCase
assert_equal "An error has occurred, this and all later migrations canceled:\n\nSomething broke", e.message
Person.reset_column_information
assert_not Person.column_methods_hash.include?(:last_name),
assert_no_column Person, :last_name,
"On error, the Migrator should revert schema changes but it did not."
end
@ -263,7 +260,7 @@ class MigrationTest < ActiveRecord::TestCase
skip "not supported on #{ActiveRecord::Base.connection.class}"
end
assert_not Person.column_methods_hash.include?(:last_name)
assert_no_column Person, :last_name
migration = Class.new(ActiveRecord::Migration) {
def version; 100 end
@ -279,8 +276,7 @@ class MigrationTest < ActiveRecord::TestCase
assert_equal "An error has occurred, this migration was canceled:\n\nSomething broke", e.message
Person.reset_column_information
assert_not Person.column_methods_hash.include?(:last_name),
assert_no_column Person, :last_name,
"On error, the Migrator should revert schema changes but it did not."
end
@ -289,7 +285,7 @@ class MigrationTest < ActiveRecord::TestCase
skip "not supported on #{ActiveRecord::Base.connection.class}"
end
assert_not Person.column_methods_hash.include?(:last_name)
assert_no_column Person, :last_name
migration = Class.new(ActiveRecord::Migration) {
self.disable_ddl_transaction!
@ -305,12 +301,11 @@ class MigrationTest < ActiveRecord::TestCase
e = assert_raise(StandardError) { migrator.migrate }
assert_equal "An error has occurred, all later migrations canceled:\n\nSomething broke", e.message
Person.reset_column_information
assert Person.column_methods_hash.include?(:last_name),
assert_column Person, :last_name,
"without ddl transactions, the Migrator should not rollback on error but it did."
ensure
Person.reset_column_information
if Person.column_methods_hash.include?(:last_name)
if Person.column_names.include?('last_name')
Person.connection.remove_column('people', 'last_name')
end
end

View file

@ -49,6 +49,18 @@ module ActiveRecord
assert_queries(0, :ignore_none => true, &block)
end
def assert_column(model, column_name, msg=nil)
assert has_column?(model, column_name), msg
end
def assert_no_column(model, column_name, msg=nil)
assert_not has_column?(model, column_name), msg
end
def has_column?(model, column_name)
model.reset_column_information
model.column_names.include?(column_name.to_s)
end
end
class SQLCounter