Module: Jwtauth::Apiv01Controller
- Defined in:
- lib/jwtauth.rb
Overview
require include Pundit (gem <<pundit>>)
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#create ⇒ Object
POST /api/v02/entity_names(s).
-
#define_entity ⇒ Object
Require define entity class name.
-
#destroy ⇒ Object
DELETE /api/v02/entity_names(s)/1.
-
#index ⇒ Object
GET /api/v02/entity_name(s).
-
#send_json_error(errors = [], status = :unprocessable_entity) ⇒ Object
Send error messages with status.
-
#show ⇒ Object
GET /api/v02/entity_names(s)/1.
-
#update ⇒ Object
PATCH/PUT /api/v02/entity_names(s)/1.
-
#user_not_authorized(exception) ⇒ Object
Handle for user not authorized.
Instance Method Details
#create ⇒ Object
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) @record if @record.save render json: record_as_json(@record), status: :created else render json: @record.errors., status: :unprocessable_entity end end |
#define_entity ⇒ Object
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 |
#destroy ⇒ Object
DELETE /api/v02/entity_names(s)/1
331 332 333 334 335 |
# File 'lib/jwtauth.rb', line 331 def destroy @record @record.destroy end |
#index ⇒ Object
GET /api/v02/entity_name(s)
291 292 293 294 295 296 |
# File 'lib/jwtauth.rb', line 291 def index @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 |
#show ⇒ Object
GET /api/v02/entity_names(s)/1
299 300 301 302 303 |
# File 'lib/jwtauth.rb', line 299 def show @record render json: record_as_json(@record) end |
#update ⇒ Object
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 @record @record.assign_attributes(entity_params) @record if @record.save render json: record_as_json(@record), status: :ok else render json: @record.errors., 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 (exception) policy_name = exception.policy.class.to_s.underscore = I18n.t("#{policy_name}.#{exception.query}", scope: "pundit", default: :default) if request.format.symbol == :json send_json_error [], :forbidden else render template: 'errors/401', locals: { message: } end end |