Skip to content

Commit

Permalink
Hide deprecated and duplicate attributes
Browse files Browse the repository at this point in the history
Some virtual_attributes are not needed in reporting nor the api.
Some deprecated attributes should not be advertised for customers to use.

After this change, the attributes are still accessible. So nothing will break, but
these attributes are no longer advertised in the public apis (i.e.: report builder and rest api).

This allows us to control which parts of our models are exposed.

This fixes ManageIQ#18130
This enables ManageIQ#20532
  • Loading branch information
kbrock committed Oct 12, 2020
1 parent c6cc54d commit e9aaf79
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 37 deletions.
1 change: 1 addition & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ApplicationRecord < ActiveRecord::Base
include ArNestedCountBy
include ArHrefSlug
include ToModelHash
include ArVisibleAttribute

extend ArTableLock
extend ArReferences
Expand Down
18 changes: 0 additions & 18 deletions app/models/mixins/deprecation_mixin.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
23 changes: 23 additions & 0 deletions lib/extensions/ar_visible_attribute.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
19 changes: 19 additions & 0 deletions spec/lib/extensions/ar_visible_attribute_spec.rb
Original file line number Diff line number Diff line change
@@ -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
18 changes: 0 additions & 18 deletions spec/models/mixins/deprecation_mixin_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit e9aaf79

Please sign in to comment.