diff options
author | Sean Griffin <sean@seantheprogrammer.com> | 2015-09-24 09:08:43 -0600 |
---|---|---|
committer | Sean Griffin <sean@seantheprogrammer.com> | 2015-09-24 09:08:43 -0600 |
commit | 8587cf0d930832c362249ccec5fe896bd6563e2b (patch) | |
tree | 298ca6c3f0a593cb55f9fe90c4f69b7647d43e94 | |
parent | 4a375a83deab7a3089e718a1d18ddc4c2846cd40 (diff) | |
parent | 4ccdd4122dfdab200c02f78a994f4112d6164164 (diff) | |
download | rails-8587cf0d930832c362249ccec5fe896bd6563e2b.tar.gz rails-8587cf0d930832c362249ccec5fe896bd6563e2b.tar.bz2 rails-8587cf0d930832c362249ccec5fe896bd6563e2b.zip |
Merge pull request #21720 from byroot/ignore-columns
Implement ActiveRecord::Base.ignored_columns
-rw-r--r-- | activerecord/CHANGELOG.md | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/model_schema.rb | 9 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 18 | ||||
-rw-r--r-- | activerecord/test/models/developer.rb | 5 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 1 |
5 files changed, 37 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 36fe3e5234..45c330b8af 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -32,6 +32,11 @@ *Yves Senn*, *Matthew Draper* +* Add `ActiveRecord::Base.ignored_columns` to make some columns + invisible from ActiveRecord. + + *Jean Boussier* + * `ActiveRecord::Tasks::MySQLDatabaseTasks` fails if shellout to mysql commands (like `mysqldump`) is not successful. diff --git a/activerecord/lib/active_record/model_schema.rb b/activerecord/lib/active_record/model_schema.rb index c461c04a26..a9bd094a66 100644 --- a/activerecord/lib/active_record/model_schema.rb +++ b/activerecord/lib/active_record/model_schema.rb @@ -50,6 +50,13 @@ module ActiveRecord class_attribute :pluralize_table_names, instance_writer: false self.pluralize_table_names = true + ## + # :singleton-method: + # Accessor for the list of columns names the model should ignore. Ignored columns won't have attribute + # accessors defined, and won't be referenced in SQL queries. + class_attribute :ignored_columns, instance_accessor: false + self.ignored_columns = [].freeze + self.inheritance_column = 'type' delegate :type_for_attribute, to: :class @@ -308,7 +315,7 @@ module ActiveRecord end def load_schema! - @columns_hash = connection.schema_cache.columns_hash(table_name) + @columns_hash = connection.schema_cache.columns_hash(table_name).except(*ignored_columns) @columns_hash.each do |name, column| warn_if_deprecated_type(column) define_attribute( diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 991145bed4..d6c48b99f3 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1571,4 +1571,22 @@ class BasicsTest < ActiveRecord::TestCase assert_not topic.id_changed? end + + test "ignored columns are not present in columns_hash" do + cache_columns = Developer.connection.schema_cache.columns_hash(Developer.table_name) + assert_includes cache_columns.keys, 'first_name' + refute_includes Developer.columns_hash.keys, 'first_name' + end + + test "ignored columns have no attirbute methods" do + refute Developer.new.respond_to?(:first_name) + refute Developer.new.respond_to?(:first_name=) + refute Developer.new.respond_to?(:first_name?) + end + + test "ignored columns don't prevent explicit declaration of attribute methods" do + assert Developer.new.respond_to?(:last_name) + assert Developer.new.respond_to?(:last_name=) + assert Developer.new.respond_to?(:last_name?) + end end diff --git a/activerecord/test/models/developer.rb b/activerecord/test/models/developer.rb index 8ac7a9df6a..7565e8f967 100644 --- a/activerecord/test/models/developer.rb +++ b/activerecord/test/models/developer.rb @@ -7,6 +7,8 @@ module DeveloperProjectsAssociationExtension2 end class Developer < ActiveRecord::Base + self.ignored_columns = %w(first_name last_name) + has_and_belongs_to_many :projects do def find_most_recent order("id DESC").first @@ -61,6 +63,9 @@ class Developer < ActiveRecord::Base developer.audit_logs.build :message => "Computer created" end + attr_accessor :last_name + define_attribute_method 'last_name' + def log=(message) audit_logs.build :message => message end diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index 4c6d1ef3ce..4b7272f10a 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -250,6 +250,7 @@ ActiveRecord::Schema.define do create_table :developers, force: true do |t| t.string :name + t.string :first_name t.integer :salary, default: 70000 if subsecond_precision_supported? t.datetime :created_at, precision: 6 |