Skip to content

Commit

Permalink
rename hidden attributes to private attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Nov 6, 2020
1 parent 9dec939 commit 507d42a
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ApplicationRecord < ActiveRecord::Base
include ArNestedCountBy
include ArHrefSlug
include ToModelHash
include ArVisibleAttribute
include ArPrivateAttribute

extend ArTableLock
extend ArReferences
Expand Down
2 changes: 1 addition & 1 deletion app/models/ext_management_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion app/models/mixins/deprecation_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/acts_as_ar_model.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ActsAsArModel
include Vmdb::Logging
include ArVisibleAttribute
include ArPrivateAttribute

def self.connection
ActiveRecord::Base.connection
Expand Down
Original file line number Diff line number Diff line change
@@ -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
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.visible_attribute_names, :parent => parent}
result = {:columns => model.public_attribute_names, :parent => parent}
result[:reflections] = {}

model.reflections_with_virtual.each do |assoc, ref|
Expand Down
37 changes: 37 additions & 0 deletions spec/lib/extensions/ar_private_attribute_spec.rb
Original file line number Diff line number Diff line change
@@ -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
37 changes: 0 additions & 37 deletions spec/lib/extensions/ar_visible_attribute_spec.rb

This file was deleted.

0 comments on commit 507d42a

Please sign in to comment.