Skip to content

Commit

Permalink
Use SupportsFeatureMixin to validate_order
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiendupont committed Aug 26, 2019
1 parent b57b0c8 commit 07d0313
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 10 deletions.
12 changes: 3 additions & 9 deletions app/controllers/api/mixins/service_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ module Mixins
module ServiceTemplates
def order_service_template(id, data, scheduled_time = nil)
service_template = resource_search(id, :service_templates, ServiceTemplate)
errors = validate_order(service_template, true)
if errors.present?
raise BadRequestError, "#{service_template_ident(service_template)} cannot be ordered - #{errors.join(", ")}"
end
raise BadRequestError, "#{service_template_ident(service_template)} cannot be ordered - #{service_template.unsupported_reason(:order)}" unless service_template.supports_order?

request_result = service_template.order(User.current_user, (data || {}), order_request_options, scheduled_time)
errors = request_result[:errors]
Expand All @@ -18,11 +15,8 @@ def order_service_template(id, data, scheduled_time = nil)

private

def validate_order(service_template, with_errors = false)
errors = []
errors << 'Service ordering via API is not allowed' unless api_request_allowed?
errors += service_template.validate_order(true)
with_errors ? errors : errors.blank?
def validate_order(service_template)
service_template.supports_order?
end
alias orderable? validate_order

Expand Down
2 changes: 1 addition & 1 deletion spec/requests/service_templates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -607,7 +607,7 @@
expected = {
"error" => a_hash_including(
"kind" => "bad_request",
"message" => /cannot be ordered - Service template does not belong to a service catalog, Service template is not configured to be displayed/
"message" => /cannot be ordered - Service template is not configured to be displayed/
)
}
expect(response).to have_http_status(:bad_request)
Expand Down

0 comments on commit 07d0313

Please sign in to comment.