Skip to content

Commit

Permalink
[api] Cleanup attribute controller a little
Browse files Browse the repository at this point in the history
Now that it only has one tasks, we can shorten the function
names
  • Loading branch information
coolo committed Jun 26, 2018
1 parent 718e786 commit b41c687
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions src/api/app/controllers/attribute_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ class AttributeController < ApplicationController
validate_action delete: { method: :delete, response: :status }
validate_action update: { method: :put, request: :attrib_type, response: :status }
validate_action update: { method: :post, request: :attrib_type, response: :status }
before_action :require_attribute_name, only: [:show, :update, :delete]
before_action :load_attribute, only: [:show, :update, :delete]

# GET /attribute/:namespace/:name/_meta
def show
if (@at = attribute_type)
if @at
render template: 'attribute/show'
else
render_error message: "Unknown attribute '#{@namespace}':'#{@name}'",
Expand All @@ -20,7 +20,7 @@ def show
# DELETE /attribute/:namespace/:name/_meta
# DELETE /attribute/:namespace/:name
def delete
if (@at = attribute_type)
if @at
authorize @at, :destroy?
@at.destroy
end
Expand All @@ -32,53 +32,36 @@ def delete
def update
return unless (xml_element = validate_xml)

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

@at.update_from_xml(xml_element)
else
create_attribute_definiton(xml_element)
create(xml_element)
end

render_ok
end

private

def require_namespace
def load_attribute
@namespace = params[:namespace]
end

def require_attribute_namespace
require_namespace
@ans = AttribNamespace.find_by_name!(@namespace)
return true if @ans

render_error status: 400, errorcode: 'unknown_attribute_namespace',
message: "Specified attribute namespace does not exist: '#{namespace}'"
false
end

def require_attribute_name
return unless require_attribute_namespace
if params[:name].nil?
raise MissingParameterError, "parameter 'name' is missing"
end
@name = params[:name]
# find_by_name is something else (of course)
@at = @ans.attrib_types.where(name: @name).first
end

def create_attribute_definiton(xml_element)
def create(xml_element)
entry = AttribType.new(name: @name, attrib_namespace: @ans)
authorize entry, :create?

entry.update_from_xml(xml_element)
end

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

def validate_xml
xml_element = Xmlhash.parse(request.raw_post)

Expand Down

0 comments on commit b41c687

Please sign in to comment.