diff --git a/app/models/application_record.rb b/app/models/application_record.rb index 0e27a438fb9f..ed2ca68135e7 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -11,6 +11,7 @@ class ApplicationRecord < ActiveRecord::Base include ArNestedCountBy include ArHrefSlug include ToModelHash + include ArVisibleAttribute extend ArTableLock extend ArReferences diff --git a/app/models/mixins/deprecation_mixin.rb b/app/models/mixins/deprecation_mixin.rb index 26faa273c216..bf905b1f1410 100644 --- a/app/models/mixins/deprecation_mixin.rb +++ b/app/models/mixins/deprecation_mixin.rb @@ -1,25 +1,7 @@ module DeprecationMixin extend ActiveSupport::Concern - included do - cattr_accessor :hidden_attribute_names - self.hidden_attribute_names = [] - end - module ClassMethods - # @param [String|Symbol] attribute name of attribute to be hidden from the api and reporting - # this attribute is accessible to all ruby methods. But it is not advertised. - # we do this when deprecating an attribute or when introducing an internal attribute - def hide_attribute(attribute) - hidden_attribute_names << attribute.to_s - end - - # @return Array[String] attribute names that can be advertised in the api and reporting - # Other attributes are accessible, they are just no longer in our public api (or never were) - def visible_attribute_names - attribute_names - hidden_attribute_names - end - private def deprecate_belongs_to(old_belongs_to, new_belongs_to) diff --git a/lib/extensions/ar_visible_attribute.rb b/lib/extensions/ar_visible_attribute.rb new file mode 100644 index 000000000000..f9b8baf8adcd --- /dev/null +++ b/lib/extensions/ar_visible_attribute.rb @@ -0,0 +1,23 @@ +module ArVisibleAttribute + extend ActiveSupport::Concern + + included do + cattr_accessor :hidden_attribute_names + self.hidden_attribute_names = [] + end + + class_methods do + # @param [String|Symbol] attribute name of attribute to be hidden from the api and reporting + # this attribute is accessible to all ruby methods. But it is not advertised. + # we do this when deprecating an attribute or when introducing an internal attribute + def hide_attribute(attribute) + hidden_attribute_names << attribute.to_s + end + + # @return Array[String] attribute names that can be advertised in the api and reporting + # Other attributes are accessible, they are just no longer in our public api (or never were) + def visible_attribute_names + attribute_names - hidden_attribute_names + end + end +end diff --git a/lib/miq_expression.rb b/lib/miq_expression.rb index 164dd57ed45f..dcc6e2c8296f 100644 --- a/lib/miq_expression.rb +++ b/lib/miq_expression.rb @@ -909,7 +909,7 @@ def self.build_relats(model, parent = {}, seen = []) parent[:class_path] ||= model.name parent[:assoc_path] ||= model.name parent[:root] ||= model.name - result = {:columns => (model.try(:visible_attribute_names) || model.attribute_names), :parent => parent} + result = {:columns =>model.visible_attribute_names, :parent => parent} result[:reflections] = {} model.reflections_with_virtual.each do |assoc, ref| diff --git a/spec/lib/extensions/ar_visible_attribute_spec.rb b/spec/lib/extensions/ar_visible_attribute_spec.rb new file mode 100644 index 000000000000..26745a0866fd --- /dev/null +++ b/spec/lib/extensions/ar_visible_attribute_spec.rb @@ -0,0 +1,19 @@ +RSpec.describe ArVisibleAttribute do + context "visible_attribute_names" do + let(:klass) do + Class.new(ApplicationRecord) do + # ApplicationRecord already included ArVisibleAttribute + self.table_name = "vms" + end + end + + it "shows regular attributes" do + expect(klass.visible_attribute_names).to include("format") + end + + it "hides hidden attributes" do + klass.hide_attribute :format + expect(klass.visible_attribute_names).not_to include("format") + end + end +end diff --git a/spec/models/mixins/deprecation_mixin_spec.rb b/spec/models/mixins/deprecation_mixin_spec.rb index 7c3984df9fa4..7e059ef1c762 100644 --- a/spec/models/mixins/deprecation_mixin_spec.rb +++ b/spec/models/mixins/deprecation_mixin_spec.rb @@ -7,22 +7,4 @@ expect(Host.arel_attribute(:address).name).to eq("hostname") # typically this is a symbol. not perfect but it works end end - - context "visible_attribute_names" do - let(:klass) do - Class.new(ApplicationRecord) do - include DeprecationMixin - self.table_name = "vms" - end - end - - it "shows regular attributes" do - expect(klass.visible_attribute_names).to include("name") - end - - it "hides hidden attributes" do - klass.hide_attribute :name - expect(klass.visible_attribute_names).not_to include("name") - end - end end