forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Hide deprecated and duplicate attributes
Some virtual_attributes are not needed in reporting nor the api. Some deprecated attributes should not be advertised for customers to start using. Now, the attributes are still accessible. So nothing will break. But these attributes are no longer advertised on 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 Hide deprecated and duplicate attributes 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
Showing
7 changed files
with
67 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
module ArVisibleAttribute | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
class_attribute :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) | ||
self.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 | ||
self.attribute_names - self.hidden_attribute_names | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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 |