Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CarlosUvaSilva/c890b8189915f5f752f8203b960fe3cf to your computer and use it in GitHub Desktop.
Save CarlosUvaSilva/c890b8189915f5f752f8203b960fe3cf to your computer and use it in GitHub Desktop.
User controller
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