From d1a14865e055091fc2a50ddfd7e9e206152b1095 Mon Sep 17 00:00:00 2001 From: bogdanvlviv Date: Mon, 10 Sep 2018 16:58:56 +0300 Subject: 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. --- activerecord/lib/active_record/core.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record/core.rb') 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 -- cgit v1.2.3