Created
May 31, 2017 17:29
-
-
Save CarlosUvaSilva/c890b8189915f5f752f8203b960fe3cf to your computer and use it in GitHub Desktop.
User controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Api::V1::UsersController < ApplicationController | |
before_action :set_item, only: [:show, :update, :reservations] | |
after_action :verify_authorized, except: :index, unless: -> { @user.nil? } | |
after_action :verify_policy_scoped, only: [:reservations] | |
def show | |
if @user | |
render json: @user | |
else | |
render_not_found! | |
end | |
end | |
def update | |
return render_not_found! unless @user | |
service_params = permitted_attributes_for_update | |
service_params = service_params.merge(user: @user) | |
service = UpdateUserService.new(service_params) | |
if service.call | |
render json: service.user | |
else | |
render_unprocessable_entity!(service.errors) | |
end | |
end | |
def reservations | |
@reservations = @user.reservations | |
render json: @reservations | |
end | |
private | |
def set_item | |
@user = User.find_by(id: params[:id]) | |
authorize @user | |
end | |
def permitted_attributes_for_update | |
params.permit( | |
:first_name, :last_name, :locale, :gender, :birthdate, :timezone, | |
:country, :state, :city, :region, :address, :phone_number) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment