Skip to content

Commit

Permalink
Merge pull request #1278 from kbrock/alias_attribute_access
Browse files Browse the repository at this point in the history
Expose attribute aliases in list of attributes
  • Loading branch information
jrafanie authored Feb 11, 2025
2 parents 5389dfd + 331de3e commit e1c594c
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/base_controller/normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@ def normalize_select_attributes(obj, opts)
if opts[:render_attributes].present?
opts[:render_attributes]
elsif obj.respond_to?(:attributes) && obj.class.respond_to?(:virtual_attribute_names)
obj.attributes.keys - obj.class.virtual_attribute_names
obj.class.all_attribute_names - obj.class.virtual_attribute_names
elsif obj.respond_to?(:attributes)
obj.attributes.keys
obj.class.all_attribute_names
else
obj.keys
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/pictures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def attribute_selection
if !@req.attributes.empty? || @additional_attributes
@req.attributes | Array(@additional_attributes) | ID_ATTRS
else
Picture.attribute_names - %w(content)
Picture.attribute_names | Picture.try(:attribute_aliases)&.keys - %w(content)
end
end
end
Expand Down
5 changes: 4 additions & 1 deletion lib/services/api/options_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,24 @@ def serialize

def attributes
return [] unless klass
options_attribute_list(klass.attribute_names - klass.virtual_attribute_names)
options_attribute_list(klass.all_attribute_names - klass.virtual_attribute_names)
end

def virtual_attributes
return [] unless klass

options_attribute_list(klass.virtual_attribute_names)
end

def relationships
return [] unless klass

(klass.reflections.keys | klass.virtual_reflections.keys.collect(&:to_s)).sort
end

def subcollections
return [] unless klass

Array(config[config.name_for_klass(klass)].subcollections).sort
end

Expand Down
6 changes: 4 additions & 2 deletions spec/requests/flavors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,15 @@
it "can show a provider's flavor" do
api_basic_authorize(action_identifier(:flavors, :read, :subresource_actions, :get))
ems = FactoryBot.create(:ems_cloud)
flavor = FactoryBot.create(:flavor, :ext_management_system => ems)
# declaring cpus to ensure alias_attribute works
flavor = FactoryBot.create(:flavor, :ext_management_system => ems, :cpus => 5)

get(api_provider_flavor_url(nil, ems, flavor))

expected = {
"href" => api_provider_flavor_url(nil, ems, flavor),
"id" => flavor.id.to_s
"id" => flavor.id.to_s,
"cpu_total_cores" => 5
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
Expand Down
2 changes: 1 addition & 1 deletion spec/support/api/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def expect_tagging_result(tag_results, status = :ok)

def expect_options_results(type, data = nil)
klass = ::Api::ApiConfig.collections[type].klass.constantize
attributes = select_attributes(klass.attribute_names - klass.virtual_attribute_names)
attributes = select_attributes(klass.all_attribute_names - klass.virtual_attribute_names)
reflections = (klass.reflections.keys | klass.virtual_reflections.keys.collect(&:to_s)).sort
subcollections = Array(::Api::ApiConfig.collections[type].subcollections).collect(&:to_s).sort
expected = {
Expand Down

0 comments on commit e1c594c

Please sign in to comment.