Module: Glib::Json::Libs::ClassMethods

Defined in:
app/controllers/concerns/glib/json/libs.rb

Instance Method Summary collapse

Instance Method Details

#__json_libs_init_optionsObject

This usually resides in a base controller class, e.g. ApplicationController.



305
306
307
308
309
310
311
312
313
# File 'app/controllers/concerns/glib/json/libs.rb', line 305

def __json_libs_init_options
  if instance_variable_defined?(:@__json_libs_init_options)
    @__json_libs_init_options
  elsif superclass.respond_to?(:__json_libs_init_options)
    superclass.__json_libs_init_options
  else
    nil
  end
end

#json_libs_force_json_uiObject



346
347
348
# File 'app/controllers/concerns/glib/json/libs.rb', line 346

def json_libs_force_json_ui
  before_action :glib_force_json_ui
end

#json_libs_init(options) ⇒ Object



315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'app/controllers/concerns/glib/json/libs.rb', line 315

def json_libs_init(options)
  include Glib::Json::Transformation
  include Glib::Json::Validation
  include Glib::Json::Ui

  include Glib::Json::Traversal
  include Glib::Json::NewDynamicText

  @__json_libs_init_options = options

  before_action do
    __json_ui_start(options)
  end

  # Note that after_action gets executed in reverse
  after_action do
    __json_ui_commit(options)
  end
  after_action :__json_transformation_commit
  after_action :__json_validate_perform

  after_action :__json_traversal_perform
  after_action :__json_traversal_register_dynamic_text
end

#json_libs_rescue_404(preview_mode: false) ⇒ Object



423
424
425
426
427
# File 'app/controllers/concerns/glib/json/libs.rb', line 423

def json_libs_rescue_404(preview_mode: false)
  rescue_from ActiveRecord::RecordNotFound do |exception|
    glib_json_handle_404(exception, preview_mode)
  end
end

#json_libs_rescue_500(preview_mode: false) ⇒ Object

Call this before other rescues. Later rescue_from statements will take precedence, so more specific rescues have to be declared later.



417
418
419
420
421
# File 'app/controllers/concerns/glib/json/libs.rb', line 417

def json_libs_rescue_500(preview_mode: false)
  rescue_from StandardError do |exception|
    glib_json_handle_500(exception, preview_mode)
  end
end

#json_libs_rescue_csrf(sign_in_url_proc) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# File 'app/controllers/concerns/glib/json/libs.rb', line 359

def json_libs_rescue_csrf()
  rescue_from ActionController::InvalidAuthenticityToken do |exception|
    # sign_out(:user)

    # respond_to do |format|
    #   format.json do
    #     render json: {
    #       onResponse: {
    #         action: 'windows/open-v1',
    #         url: url
    #       }
    #     }
    #   end
    # end

    respond_to do |format|
      format.html do
        redirect_to instance_exec(&)
      end
      format.json do
        raise exception if Rails.env.test? || !json_ui_should_render?(self.class.__json_libs_init_options)

        render(
          json: {
            glib_json_dialog_mode? ? :onLoad : :onResponse => __glib_error_dialog(
              'Your session has expired',
              'Please refresh the page and retry.',
              ['dialog-csrf-error'],
              [
                {
                  view: 'button',
                  text: 'Refresh',
                  onClick: {
                    action: 'auth/saveCsrfToken',
                    token: form_authenticity_token,
                    onSave: {
                      action: 'dialogs/close',
                      onClose: {
                        action: 'windows/reload',
                        onReload: {
                          action: 'snackbars/alert',
                          message: 'Refreshed. You can try again now.'
                        }
                      }
                    }
                  }
                },
              ]
            )
          }
        )
      end
    end
  end
end

#json_libs_set_localeObject



350
351
352
353
354
355
356
357
# File 'app/controllers/concerns/glib/json/libs.rb', line 350

def json_libs_set_locale
  before_action do
    # Need to explicitly fallback to EN
    I18n.locale = params[:_locale] || :en
  rescue
    I18n.locale = :en
  end
end

#json_libs_skip_json_ui(options = {}) ⇒ Object



340
341
342
343
344
# File 'app/controllers/concerns/glib/json/libs.rb', line 340

def json_libs_skip_json_ui(options = {})
  prepend_before_action options do
    params[:_skip_render] = 'true'
  end
end