Module: Jwtauth::Apiv01Controller

Defined in:
lib/jwtauth.rb

Overview

require include Pundit (gem <<pundit>>)

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#createObject

POST /api/v02/entity_names(s)



306
307
308
309
310
311
312
313
314
315
# File 'lib/jwtauth.rb', line 306

def create
  @record = @entity_model.new2nd(entity_params, current_user)
  authorize @record

  if @record.save
    render json: record_as_json(@record), status: :created
  else
    render json: @record.errors.full_messages, status: :unprocessable_entity
  end
end

#define_entityObject

Require define entity class name



269
270
271
# File 'lib/jwtauth.rb', line 269

def define_entity
  send_json_error(['entity model is not defined'], :internal_server_error)
end

#destroyObject

DELETE /api/v02/entity_names(s)/1



331
332
333
334
335
# File 'lib/jwtauth.rb', line 331

def destroy
  authorize @record

  @record.destroy
end

#indexObject

GET /api/v02/entity_name(s)



291
292
293
294
295
296
# File 'lib/jwtauth.rb', line 291

def index
  authorize @entity_model
  @records = core_index_filter(policy_scope(@entity_model))

  render json: { filters: filter_jsons, records: records_as_json(@records) }
end

#send_json_error(errors = [], status = :unprocessable_entity) ⇒ Object

Send error messages with status



274
275
276
# File 'lib/jwtauth.rb', line 274

def send_json_error(errors = [], status = :unprocessable_entity)
  render json: errors, status: status
end

#showObject

GET /api/v02/entity_names(s)/1



299
300
301
302
303
# File 'lib/jwtauth.rb', line 299

def show
  authorize @record

  render json: record_as_json(@record)
end

#updateObject

PATCH/PUT /api/v02/entity_names(s)/1



318
319
320
321
322
323
324
325
326
327
328
# File 'lib/jwtauth.rb', line 318

def update
  authorize @record
  @record.assign_attributes(entity_params)
  authorize @record

  if @record.save
    render json: record_as_json(@record), status: :ok
  else
    render json: @record.errors.full_messages, status: :unprocessable_entity
  end
end

#user_not_authorized(exception) ⇒ Object

Handle for user not authorized



279
280
281
282
283
284
285
286
287
288
# File 'lib/jwtauth.rb', line 279

def user_not_authorized(exception)
  policy_name = exception.policy.class.to_s.underscore
  error_message = I18n.t("#{policy_name}.#{exception.query}", scope: "pundit", default: :default)

  if request.format.symbol == :json
    send_json_error [error_message], :forbidden
  else
    render template: 'errors/401', locals: { message: error_message }
  end
end