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

Return href on create #15005

Merged
merged 3 commits into from
Jun 9, 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
12 changes: 6 additions & 6 deletions app/controllers/api/base_controller/normalizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def normalize_hash(type, obj, opts = {})
attrs = normalize_select_attributes(obj, opts)
result = {}

href = new_href(type, obj["id"], obj["href"], opts)
href = new_href(type, obj["id"], obj["href"])
if href.present?
result["href"] = href
attrs -= ["href"]
Expand All @@ -28,7 +28,7 @@ def normalize_hash(type, obj, opts = {})
def normalize_attr(attr, value)
return if value.nil?
if value.kind_of?(Array) || value.kind_of?(ActiveRecord::Relation)
normalize_array(attr, value)
normalize_array(value)
elsif value.respond_to?(:attributes) || value.respond_to?(:keys)
normalize_hash(attr, value)
elsif Api.time_attribute?(attr)
Expand Down Expand Up @@ -109,12 +109,12 @@ def normalize_select_attributes(obj, opts)
end
end

def normalize_array(name, obj)
obj.collect { |item| normalize_attr(name, item) }
def normalize_array(obj)
obj.collect { |item| normalize_attr(@req.subcollection || @req.collection, item) }
end

def new_href(type, current_id, current_href, opts)
normalize_href(type, current_id) if opts[:add_href] && current_id.present? && current_href.blank?
def new_href(type, current_id, current_href)
normalize_href(type, current_id) if current_id.present? && current_href.blank?
end
end
end
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/api/base_controller/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,11 @@ def collection_to_jbuilder(type, reftype, resources, opts = {})
end

def resource_to_jbuilder(type, reftype, resource, opts = {})
normalize_options = {}
reftype = get_reftype(type, reftype, resource, opts)
json = Jbuilder.new
json.ignore_nil!

normalize_options = {:add_href => true}

pas = physical_attribute_selection(resource)
normalize_options[:render_attributes] = pas if pas.present?

Expand Down Expand Up @@ -341,7 +340,7 @@ def expand_subcollection(json, sc, sctype, subresources)
json.set! sc.to_s do |js|
subresources.each do |scr|
if @req.expand?(sc) || scr["id"].nil?
add_child js, normalize_hash(sctype, scr, :add_href => true)
add_child js, normalize_hash(sctype, scr)
else
js.child! { |jsc| jsc.href normalize_href(sctype, scr["id"]) }
end
Expand Down
4 changes: 2 additions & 2 deletions spec/requests/api/blueprints_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@

expected = {
"results" => a_collection_containing_exactly(
a_hash_including("name" => "foo", "description" => "bar"),
a_hash_including("name" => "baz", "description" => "qux")
a_hash_including("name" => "foo", "description" => "bar", "href" => a_string_including(blueprints_url)),
a_hash_including("name" => "baz", "description" => "qux", "href" => a_string_including(blueprints_url))
)
}
expect(response.parsed_body).to include(expected)
Expand Down
5 changes: 3 additions & 2 deletions spec/requests/api/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@

expect { run_post tags_url, options }.to change(Tag, :count).by(1)

tag = Tag.find(response.parsed_body["results"].first["id"])
result = response.parsed_body["results"].first
tag = Tag.find(result["id"])
tag_category = Category.find(tag.category.id)
expect(tag_category).to eq(category)

expect(result["href"]).to include(tags_url(tag.id))
expect(response).to have_http_status(:ok)
end

Expand Down
7 changes: 6 additions & 1 deletion spec/requests/api/tenant_quotas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@
it "can create a quota from a tenant" do
api_basic_authorize action_identifier(:quotas, :create, :subcollection_actions, :post)

expected = {
'results' => [
a_hash_including('href' => a_string_including("#{tenants_url(tenant.id)}/quotas/"))
]
}
expect do
run_post "/api/tenants/#{tenant.id}/quotas/", :name => :cpu_allocated, :value => 1
end.to change(TenantQuota, :count).by(1)

expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
end

Expand Down