aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/model_schema.rb13
-rw-r--r--activerecord/test/cases/migration/column_attributes_test.rb21
-rw-r--r--activerecord/test/cases/migration_test.rb25
-rw-r--r--activerecord/test/cases/test_case.rb12
4 files changed, 28 insertions, 43 deletions
diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb
index ac2d2f2712..44ea8610f2 100644
--- a/activerecord/lib/active_record/model_schema.rb
+++ b/activerecord/lib/active_record/model_schema.rb
@@ -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.
#
diff --git a/activerecord/test/cases/migration/column_attributes_test.rb b/activerecord/test/cases/migration/column_attributes_test.rb
index 6f5f29f0da..aa606ac8bb 100644
--- a/activerecord/test/cases/migration/column_attributes_test.rb
+++ b/activerecord/test/cases/migration/column_attributes_test.rb
@@ -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
diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb
index e99312c245..ed080b2995 100644
--- a/activerecord/test/cases/migration_test.rb
+++ b/activerecord/test/cases/migration_test.rb
@@ -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
diff --git a/activerecord/test/cases/test_case.rb b/activerecord/test/cases/test_case.rb
index 70b970639a..c33e5cdc00 100644
--- a/activerecord/test/cases/test_case.rb
+++ b/activerecord/test/cases/test_case.rb
@@ -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