Skip to content

Commit

Permalink
feat(models_controller/prepare_filters): Adding a pre hook on resourc…
Browse files Browse the repository at this point in the history
…e access
  • Loading branch information
paulRbr committed Sep 16, 2014
1 parent 17def90 commit 9758491
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions lib/yodatra/models_controller.rb
Expand Up @@ -63,14 +63,14 @@ class ModelsController < Sinatra::Base
READ_ALL = :read_all
get ALL_ROUTE do
retrieve_resources READ_ALL do |resource|
resource.all.as_json(read_scope).to_json
resource.all.as_json(read_scope)
end
end

READ_ONE = :read
get ONE_ROUTE do
retrieve_resources READ_ONE do |resource|
resource.as_json(read_scope).to_json
resource.as_json(read_scope)
end
end

Expand All @@ -82,9 +82,9 @@ class ModelsController < Sinatra::Base

if @one.id.nil?
status 400
@one.errors.full_messages.to_json
@one.errors.full_messages
else
@one.as_json(read_scope).to_json
@one.as_json(read_scope)
end
end
end
Expand All @@ -94,10 +94,10 @@ class ModelsController < Sinatra::Base
retrieve_resources UPDATE_ONE do |resource|
hash = self.send("#{model_name.underscore}_params".to_sym)
if resource.update_attributes(hash)
resource.as_json(read_scope).to_json
resource.as_json(read_scope)
else
status 400
resource.errors.full_messages.to_json
resource.errors.full_messages
end
end
end
Expand All @@ -106,10 +106,10 @@ class ModelsController < Sinatra::Base
delete ONE_ROUTE do
retrieve_resources DELETE_ONE do |resource|
if resource.destroy
resource.as_json(read_scope).to_json
resource.as_json(read_scope)
else
status 400
resource.errors.full_messages.to_json
resource.errors.full_messages
end
end
end
Expand Down Expand Up @@ -157,7 +157,7 @@ def enable_search_on(*attributes)
end

resource.where(search_terms.reduce(:or)).limit(100).
flatten.as_json(read_scope).to_json
flatten.as_json(read_scope)
end
end
end
Expand All @@ -169,23 +169,30 @@ def enable_search_on(*attributes)
# Defines a nested route or not and retrieves the correct resource (or resources)
# @param disables is the name to check if it was disabled
# @param &block to be yield with the retrieved resource
def retrieve_resources(disables)
# @returns resource in json format
def retrieve_resources(action)
pass unless involved?
no_route if disabled? disables
no_route if disabled? action

model = model_name.constantize
nested = nested_resources if nested?

if model.nil? || nested.nil? && nested?
raise ActiveRecord::RecordNotFound
else
model = nested if nested?
resource = nested? ? nested : model

# Check access to the resource
method = "prepare_#{action}"
resource = send(method, resource) if respond_to? method

# ONE resource else COLLECTION
one_id = nested? ? params[:captures].fourth : params[:captures].second if params[:captures].length == 4
model = model.find one_id unless one_id.nil?
yield(model)
resource = resource.find one_id unless one_id.nil?
yield(resource).to_json
end
rescue ActiveRecord::RecordNotFound
record_not_found
record_not_found.to_json
end

def nested?
Expand Down Expand Up @@ -259,7 +266,7 @@ def no_route

def record_not_found
status 404
['record not found'].to_json
['record not found']
end

end
Expand Down

0 comments on commit 9758491

Please sign in to comment.