diff options
author | bogdanvlviv <bogdanvlviv@gmail.com> | 2018-09-10 16:58:56 +0300 |
---|---|---|
committer | bogdanvlviv <bogdanvlviv@gmail.com> | 2018-09-12 18:51:55 +0300 |
commit | d1a14865e055091fc2a50ddfd7e9e206152b1095 (patch) | |
tree | 63e86a87156f090c3474c6ad0a75842ad2109f99 | |
parent | d40e33fcc5899a91ceab10f8bd86a1455846917a (diff) | |
download | rails-d1a14865e055091fc2a50ddfd7e9e206152b1095.tar.gz rails-d1a14865e055091fc2a50ddfd7e9e206152b1095.tar.bz2 rails-d1a14865e055091fc2a50ddfd7e9e206152b1095.zip |
Build string set when `filter_attributes` is assigned
It would allow `filter_attributes` to be reused across multiple
calls to `#inspect` or `#pretty_print`.
- Add `require "set"`
- Remove `filter_attributes` instance reader. I think there is no need
to keep it.
-rw-r--r-- | activerecord/lib/active_record/core.rb | 23 | ||||
-rw-r--r-- | railties/test/application/configuration_test.rb | 3 |
2 files changed, 19 insertions, 7 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index d5d2c70a8a..0002941ff6 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -3,6 +3,7 @@ require "active_support/core_ext/hash/indifferent_access" require "active_support/core_ext/string/filters" require "concurrent/map" +require "set" module ActiveRecord module Core @@ -125,9 +126,7 @@ module ActiveRecord class_attribute :default_connection_handler, instance_writer: false - ## - # Specifies columns which shouldn't be exposed while calling #inspect. - class_attribute :filter_attributes, instance_writer: false, default: [] + self.filter_attributes = [] def self.connection_handler ActiveRecord::RuntimeRegistry.connection_handler || default_connection_handler @@ -227,6 +226,20 @@ module ActiveRecord end end + # Returns columns which shouldn't be exposed while calling #inspect. + def filter_attributes + if defined?(@filter_attributes) + @filter_attributes + else + superclass.filter_attributes + end + end + + # Specifies columns which shouldn't be exposed while calling #inspect. + def filter_attributes=(attributes_names) + @filter_attributes = attributes_names.map(&:to_s).to_set + end + # Returns a string like 'Post(id:integer, title:string, body:text)' def inspect if self == Base @@ -582,9 +595,7 @@ module ActiveRecord end def filter_attribute?(attribute_name) - filter_attributes = self.filter_attributes.map(&:to_s).to_set - - filter_attributes.include?(attribute_name) && !read_attribute(attribute_name).nil? + self.class.filter_attributes.include?(attribute_name) && !read_attribute(attribute_name).nil? end end end diff --git a/railties/test/application/configuration_test.rb b/railties/test/application/configuration_test.rb index 83192edb39..44f6af5d4a 100644 --- a/railties/test/application/configuration_test.rb +++ b/railties/test/application/configuration_test.rb @@ -3,6 +3,7 @@ require "isolation/abstract_unit" require "rack/test" require "env_helpers" +require "set" class ::MyMailInterceptor def self.delivering_email(email); email; end @@ -2049,7 +2050,7 @@ module ApplicationTests RUBY app "development" assert_equal [ :password, :credit_card_number ], Rails.application.config.filter_parameters - assert_equal [ :password, :credit_card_number ], ActiveRecord::Base.filter_attributes + assert_equal [ "password", "credit_card_number" ].to_set, ActiveRecord::Base.filter_attributes end private |