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 property method actions on Generic Object subresources #247

Merged
merged 3 commits into from
Dec 20, 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
2 changes: 1 addition & 1 deletion app/controllers/api/base_controller/renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def add_actions(json, aspecs, type)
def expand_resource_custom_actions(resource, json, type, physical_attrs)
return unless render_actions(physical_attrs) && collection_config.custom_actions?(type)

href = json.attributes!["href"]
href = @req.subcollection.present? ? normalize_url("#{@req.subcollection}/#{resource.id}") : json.attributes!["href"]
json.actions do |js|
resource_custom_action_names(resource).each do |action|
add_child js, "name" => action, "method" => :post, "href" => href
Expand Down
9 changes: 0 additions & 9 deletions app/controllers/api/generic_objects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ def edit_resource(type, id, data)

private

def resource_custom_action_names(resource)
(property_method_names(resource) << super).flatten
end

def invoke_custom_action(type, resource, action, data)
return super(type, resource.reload, action, data) if resource_custom_action_button(resource, action)
result = begin
Expand All @@ -44,11 +40,6 @@ def invoke_custom_action(type, resource, action, data)
log_result(result)
result
end

def property_method_names(resource)
resource.respond_to?(:property_methods) ? Array(resource.property_methods) : []
end

def method_description(resource, action)
"Invoked method #{resource.generic_object_definition.name}##{action} for Generic Object id: #{resource.id} name: #{resource.name}"
end
Expand Down
8 changes: 8 additions & 0 deletions app/controllers/api/mixins/generic_objects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ module GenericObjects

private

def resource_custom_action_names(resource)
(property_method_names(resource) << super).flatten
end

def property_method_names(resource)
resource.respond_to?(:property_methods) ? Array(resource.property_methods) : []
end

def create_generic_object(object_definition, data)
data['generic_object_definition'] = object_definition
GenericObject.new(data.except(*ADDITIONAL_ATTRS)).tap do |generic_object|
Expand Down
6 changes: 4 additions & 2 deletions spec/requests/generic_object_definitions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@
object.add_to_property_association('vms', [vm])
end

it 'returns the generic object with property attributes by default' do
it 'returns the generic object with property attributes and custom method actions by default' do
object_def.add_property_method('foo')
api_basic_authorize subcollection_action_identifier(:generic_object_definitions, :generic_objects, :read, :get)

get(api_generic_object_definition_generic_object_url(nil, object_def.id, object.id))
Expand All @@ -143,7 +144,8 @@
'href' => api_generic_object_definition_generic_object_url(nil, object_def, object),
'id' => object.id.to_s,
'name' => 'bar',
'property_attributes' => { 'is_something' => true }
'property_attributes' => { 'is_something' => true },
'actions' => [{'name' => 'foo', 'method' => 'post', 'href' => api_generic_object_url(nil, object.id)}]
}
expect(response.parsed_body).to include(expected)
expect(response).to have_http_status(:ok)
Expand Down