Skip to content

Commit

Permalink
[api] Streamline rails usage in attribute controller
Browse files Browse the repository at this point in the history
Don't double check the route matching, use find_by_name
where not overloaded by class methods

Use before_filter instead of calling method
  • Loading branch information
coolo committed Jun 26, 2018
1 parent 686cd04 commit 77b5ebe
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 23 deletions.
36 changes: 14 additions & 22 deletions src/api/app/controllers/attribute_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ class AttributeController < ApplicationController
validate_action delete_attribute_definition: { method: :delete, response: :status }
validate_action update_attribute_definition: { method: :put, request: :attrib_type, response: :status }
validate_action update_attribute_definition: { method: :post, request: :attrib_type, response: :status }
before_action :require_namespace, only: [:show_namespace_definition, :delete_namespace_definition, :update_namespace_definition]
before_action :require_admin, only: [:update_namespace_definition, :delete_namespace_definition]
before_action :require_attribute_name, only: [:show_attribute_definition, :update_attribute_definition, :delete_attribute_definition]

def index
if params[:namespace]
an = AttribNamespace.where(name: params[:namespace]).first
unless an
render_error status: 400, errorcode: 'unknown_namespace',
message: "Attribute namespace does not exist: #{params[:namespace]}"
return
end
an = AttribNamespace.find_by_name!(params[:namespace])
list = an.attrib_types.pluck(:name)
else
list = AttribNamespace.pluck(:name)
Expand All @@ -37,7 +33,7 @@ def index
end

def show_namespace_definition
if (@an = AttribNamespace.where(name: ensure_namespace).select(:id, :name).first)
if (@an = AttribNamespace.find_by_name!(@namespace))
render template: 'attribute/namespace_definition'
else
render_error message: "Unknown attribute namespace '#{@namespace}'",
Expand All @@ -46,22 +42,21 @@ def show_namespace_definition
end

def delete_namespace_definition
AttribNamespace.where(name: ensure_namespace).destroy_all
AttribNamespace.where(name: @namespace).destroy_all
render_ok
end

# /attribute/:namespace/_meta
def update_namespace_definition
xml_element = Xmlhash.parse(request.raw_post)
ensure_namespace

unless xml_element['name'] == @namespace
render_error status: 400, errorcode: 'illegal_request',
message: "Illegal request: PUT/POST #{request.path}: path does not match content"
return
end

db = AttribNamespace.where(name: @namespace).first
db = AttribNamespace.find_by_name(@namespace)
if db
db.update_from_xml(xml_element)
else
Expand All @@ -84,9 +79,9 @@ def show_attribute_definition
# DELETE /attribute/:namespace/:name/_meta
# DELETE /attribute/:namespace/:name
def delete_attribute_definition
if (at = attribute_type)
authorize at, :destroy?
at.destroy
if (@at = attribute_type)
authorize @at, :destroy?
@at.destroy
end

render_ok
Expand All @@ -96,11 +91,10 @@ def delete_attribute_definition
def update_attribute_definition
return unless (xml_element = validate_attribute_definition_xml)

if (entry = attribute_type)
if (@at = attribute_type)
authorize entry, :update?

db = AttribType.find(entry.id) # get a writable object
db.update_from_xml(xml_element)
@at.update_from_xml(xml_element)
else
create_attribute_definiton(xml_element)
end
Expand Down Expand Up @@ -243,16 +237,13 @@ def find_attribute_container

private

def ensure_namespace
if params[:namespace].nil?
raise MissingParameterError, "parameter 'namespace' is missing"
end
def require_namespace
@namespace = params[:namespace]
end

def require_attribute_namespace
ensure_namespace
@ans = AttribNamespace.where(name: @namespace).first
require_namespace
@ans = AttribNamespace.find_by_name!(@namespace)
return true if @ans

render_error status: 400, errorcode: 'unknown_attribute_namespace',
Expand All @@ -276,6 +267,7 @@ def create_attribute_definiton(xml_element)
end

def attribute_type
# find_by_name is something else (of course)
@ans.attrib_types.where(name: @name).first
end

Expand Down
2 changes: 1 addition & 1 deletion src/api/test/functional/attributes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_namespace_index
login_Iggy

get '/attribute/NotExisting'
assert_response 400
assert_response 404

get '/attribute/OBS'
assert_response :success
Expand Down

0 comments on commit 77b5ebe

Please sign in to comment.