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.



292
293
294
295
296
297
298
299
300
# File 'app/controllers/concerns/glib/json/libs.rb', line 292

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



333
334
335
# File 'app/controllers/concerns/glib/json/libs.rb', line 333

def json_libs_force_json_ui
  before_action :glib_force_json_ui
end

#json_libs_init(options) ⇒ Object



302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'app/controllers/concerns/glib/json/libs.rb', line 302

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



410
411
412
413
414
# File 'app/controllers/concerns/glib/json/libs.rb', line 410

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.



404
405
406
407
408
# File 'app/controllers/concerns/glib/json/libs.rb', line 404

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



346
347
348
349
350
351
352
353
354
355
356
357
358
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
# File 'app/controllers/concerns/glib/json/libs.rb', line 346

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



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

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



327
328
329
330
331
# File 'app/controllers/concerns/glib/json/libs.rb', line 327

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