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 /activerecord/lib/active_record/core.rb | |
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.
Diffstat (limited to 'activerecord/lib/active_record/core.rb')
-rw-r--r-- | activerecord/lib/active_record/core.rb | 23 |
1 files changed, 17 insertions, 6 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 |