metamaps--metamaps/app/controllers/api/restful_controller.rb

51 lines
1 KiB
Ruby
Raw Normal View History

class API::RestfulController < ActionController::Base
2016-03-11 22:37:18 +00:00
include Pundit
include PunditExtra
snorlax_used_rest!
2016-03-12 01:26:23 +00:00
load_and_authorize_resource only: [:show, :update, :destroy]
2016-03-11 22:37:18 +00:00
2016-03-11 06:26:54 +00:00
def create
2016-03-25 02:26:32 +00:00
instantiate_resource
2016-03-11 06:26:54 +00:00
resource.user = current_user
2016-03-25 06:33:26 +00:00
authorize resource
2016-03-11 06:26:54 +00:00
create_action
respond_with_resource
end
private
2016-03-11 23:05:42 +00:00
def resource_serializer
"new_#{resource_name}_serializer".camelize.constantize
end
2016-03-11 22:37:18 +00:00
def accessible_records
if current_user
visible_records
else
public_records
end
end
def current_user
super || token_user || doorkeeper_user || nil
end
def token_user
token = params[:access_token]
access_token = Token.find_by_token(token)
@token_user ||= access_token.user if access_token
end
def doorkeeper_user
return unless doorkeeper_token.present?
doorkeeper_render_error unless valid_doorkeeper_token?
@doorkeeper_user ||= User.find(doorkeeper_token.resource_owner_id)
end
def permitted_params
@permitted_params ||= PermittedParams.new(params)
end
end