Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for API slugs #14344

Merged
merged 4 commits into from
Mar 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/models/application_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class ApplicationRecord < ActiveRecord::Base
include ArRegion
include ArLock
include ArNestedCountBy
include ArHrefSlug
include ToModelHash

extend ArTableLock
Expand Down
7 changes: 7 additions & 0 deletions lib/api/collection_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ def name_for_klass(resource_klass)
@cfg.detect { |_, spec| spec[:klass] == resource_klass.name }.try(:first)
end

def name_for_subclass(resource_class)
@cfg.detect do |collection, _|
collection_class = klass(collection)
collection_class && (collection_class == resource_class || collection_class.descendants.include?(resource_class))
end.try(:first)
end

def what_refers_to_feature(product_feature_name)
referenced_identifiers[product_feature_name]
end
Expand Down
9 changes: 9 additions & 0 deletions lib/api/utils.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Api
module Utils
def self.build_href_slug(klass, id)
return unless id
collection = Api::CollectionConfig.new.name_for_subclass(klass)
"#{collection}/#{id}" if collection
end
end
end
11 changes: 11 additions & 0 deletions lib/extensions/ar_href_slug.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module ArHrefSlug
extend ActiveSupport::Concern

included do
virtual_column :href_slug, :type => :string

def href_slug
Api::Utils.build_href_slug(self.class, id)
end
end
end
14 changes: 14 additions & 0 deletions spec/lib/api/collection_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,18 @@
expect(subject.name_for_klass(String)).to be_nil
end
end

describe "#name_for_subclass" do
it "returns the collection name for classes declared by the API" do
expect(subject.name_for_subclass(Vm)).to eq(:vms)
end

it "returns the collection name for classes that are accessible via collection_class" do
expect(subject.name_for_subclass(ManageIQ::Providers::Vmware::InfraManager::Vm)).to eq(:vms)
end

it "returns nil for classes unknown to the API" do
expect(subject.name_for_subclass(String)).to be_nil
end
end
end
11 changes: 6 additions & 5 deletions spec/requests/api/querying_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -506,18 +506,19 @@ def create_vms_by_name(names)
api_basic_authorize collection_action_identifier(:vms, :read, :get)
vm = create_vms_by_name(%w(aa)).first

run_get vms_url, :expand => "resources", :attributes => "name,vendor"
run_get vms_url, :expand => "resources", :attributes => "href_slug,name,vendor"

expected = {
"name" => "vms",
"count" => 1,
"subcount" => 1,
"resources" => [
{
"id" => vm.id,
"href" => a_string_matching(vms_url(vm.id)),
"name" => "aa",
"vendor" => anything
"id" => vm.id,
"href" => a_string_matching(vms_url(vm.id)),
"href_slug" => "vms/#{vm.id}",
"name" => "aa",
"vendor" => anything
}
]
}
Expand Down