diff --git a/app/models/application_record.rb b/app/models/application_record.rb index ed2ca68135e7..90ee016639a8 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -11,7 +11,7 @@ class ApplicationRecord < ActiveRecord::Base include ArNestedCountBy include ArHrefSlug include ToModelHash - include ArVisibleAttribute + include ArPrivateAttribute extend ArTableLock extend ArReferences diff --git a/app/models/ext_management_system.rb b/app/models/ext_management_system.rb index 81d6a766d01d..e9a772980c2c 100644 --- a/app/models/ext_management_system.rb +++ b/app/models/ext_management_system.rb @@ -5,7 +5,7 @@ class ExtManagementSystem < ApplicationRecord include VerifyCredentialsMixin include DeprecationMixin - hide_attribute "aggregate_memory" # better to use total_memory (coin toss - they're similar) + private_attribute "aggregate_memory" # better to use total_memory (coin toss - they're similar) def self.with_tenant(tenant_id) tenant = Tenant.find(tenant_id) diff --git a/app/models/mixins/deprecation_mixin.rb b/app/models/mixins/deprecation_mixin.rb index bf905b1f1410..08bd24169eef 100644 --- a/app/models/mixins/deprecation_mixin.rb +++ b/app/models/mixins/deprecation_mixin.rb @@ -18,7 +18,7 @@ def deprecate_belongs_to(old_belongs_to, new_belongs_to) def deprecate_attribute(old_attribute, new_attribute) deprecate_attribute_methods(old_attribute, new_attribute) virtual_attribute(old_attribute, -> { type_for_attribute(new_attribute.to_s) }) - hide_attribute(old_attribute) + private_attribute(old_attribute) end def deprecate_attribute_methods(old_attribute, new_attribute) diff --git a/lib/acts_as_ar_model.rb b/lib/acts_as_ar_model.rb index 70aa219a9fcc..6712fd95b711 100644 --- a/lib/acts_as_ar_model.rb +++ b/lib/acts_as_ar_model.rb @@ -1,6 +1,6 @@ class ActsAsArModel include Vmdb::Logging - include ArVisibleAttribute + include ArPrivateAttribute def self.connection ActiveRecord::Base.connection diff --git a/lib/extensions/ar_visible_attribute.rb b/lib/extensions/ar_private_attribute.rb similarity index 55% rename from lib/extensions/ar_visible_attribute.rb rename to lib/extensions/ar_private_attribute.rb index e74cf521aca7..99197a7478db 100644 --- a/lib/extensions/ar_visible_attribute.rb +++ b/lib/extensions/ar_private_attribute.rb @@ -1,22 +1,22 @@ -module ArVisibleAttribute +module ArPrivateAttribute extend ActiveSupport::Concern included do - class_attribute :hidden_attribute_names, :default => [] + class_attribute :private_attribute_names, :default => [] end class_methods do - # @param [String|Symbol] attribute name of attribute to be hidden from the api and reporting + # @param [String|Symbol] attribute name of attribute to be private 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) - self.hidden_attribute_names += [attribute.to_s] + def private_attribute(attribute) + self.private_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 - self.attribute_names - self.hidden_attribute_names + def public_attribute_names + self.attribute_names - self.private_attribute_names end end end diff --git a/lib/miq_expression.rb b/lib/miq_expression.rb index 2be55e0380a7..b8b7338adf72 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.visible_attribute_names, :parent => parent} + result = {:columns => model.public_attribute_names, :parent => parent} result[:reflections] = {} model.reflections_with_virtual.each do |assoc, ref| diff --git a/spec/lib/extensions/ar_private_attribute_spec.rb b/spec/lib/extensions/ar_private_attribute_spec.rb new file mode 100644 index 000000000000..e6c65e321081 --- /dev/null +++ b/spec/lib/extensions/ar_private_attribute_spec.rb @@ -0,0 +1,37 @@ +RSpec.describe ArPrivateAttribute do + # NOTE: ApplicationRecord already included ArPrivateAttribute + let(:klass) { Class.new(ApplicationRecord) { self.table_name = "vms" } } + let(:other_klass) { Class.new(ApplicationRecord) { self.table_name = "vms" } } + + context ".public_attribute_names" do + it "shows regular attributes" do + expect(klass.public_attribute_names).to include("type") + end + + it "hides private attributes" do + klass.private_attribute :type + expect(klass.public_attribute_names).not_to include("type") + end + + it "only hides attributes for the specified class" do + klass.private_attribute :type + expect(other_klass.public_attribute_names).to include("type") + end + end + + context ".private_attribute_names" do + it "starts with no private attributes" do + expect(klass.private_attribute_names).to be_empty + end + + it "hides private attributes" do + klass.private_attribute :type + expect(klass.private_attribute_names).to include("type") + end + + it "only hides for specified class" do + klass.private_attribute :type + expect(other_klass.private_attribute_names).not_to include("type") + end + end +end diff --git a/spec/lib/extensions/ar_visible_attribute_spec.rb b/spec/lib/extensions/ar_visible_attribute_spec.rb deleted file mode 100644 index 43e8d9bf59d0..000000000000 --- a/spec/lib/extensions/ar_visible_attribute_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -RSpec.describe ArVisibleAttribute do - # NOTE: ApplicationRecord already included ArVisibleAttribute - let(:klass) { Class.new(ApplicationRecord) { self.table_name = "vms" } } - let(:other_klass) { Class.new(ApplicationRecord) { self.table_name = "vms" } } - - context ".visible_attribute_names" do - it "shows regular attributes" do - expect(klass.visible_attribute_names).to include("type") - end - - it "hides hidden attributes" do - klass.hide_attribute :type - expect(klass.visible_attribute_names).not_to include("type") - end - - it "only hides for specified class" do - klass.hide_attribute :type - expect(other_klass.visible_attribute_names).to include("type") - end - end - - context ".hidden_attribute_names" do - it "starts with no hidden attributes" do - expect(klass.hidden_attribute_names).to be_empty - end - - it "hides hidden attributes" do - klass.hide_attribute :type - expect(klass.hidden_attribute_names).to include("type") - end - - it "only hides for specified class" do - klass.hide_attribute :type - expect(other_klass.hidden_attribute_names).not_to include("type") - end - end -end