Module: Glib::Json::Libs

Extended by:
ActiveSupport::Concern
Defined in:
app/controllers/concerns/glib/json/libs.rb

Defined Under Namespace

Modules: ClassMethods

Constant Summary collapse

REDIRECT_BACK_KEY =
:glib_return_to_url

Instance Method Summary collapse

Instance Method Details

#__delete_redirect_back_url(url) ⇒ Object



195
196
197
# File 'app/controllers/concerns/glib/json/libs.rb', line 195

def __delete_redirect_back_url(url)
  session.delete(REDIRECT_BACK_KEY) || url
end

#__glib_error_dialog(title, message, style_classes, bottom_views = []) ⇒ Object

def json_ui_redirect_back_or_to(default_url, json_action)

redirect_url = __delete_redirect_back_url(default_url)
json_action.windows_closeAll onClose: ->(subaction) do
  puts "***** R2: #{redirect_url}"
  subaction.windows_open url: redirect_url
end

end



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'app/controllers/concerns/glib/json/libs.rb', line 216

def __glib_error_dialog(title, message, style_classes, bottom_views = [])
  {
    action: 'dialogs/show',
    # Don't show another dialog, just use the current one since it's a new dialog anyway, which means
    # that it wouldn't contain existing user data.
    updateExisting: glib_json_dialog_mode?,
    title: title,
    body: {
      childViews: [
        {
          view: 'panels/vertical',
          styleClasses: ['glib-error-dialog'].concat(style_classes),
          childViews: [
            {
              view: 'label',
              text: message
            },
          ].concat(bottom_views),
        }
      ]
      # Set this in the project's custom CSS.
      # padding: {
      #   x: 20,
      #   y: 22,
      # },
    }
  }
end

#delete_redirect_back_url(url) ⇒ Object



199
200
201
# File 'app/controllers/concerns/glib/json/libs.rb', line 199

def delete_redirect_back_url(url)
  __delete_redirect_back_url(url)
end

#glib_force_json_uiObject



102
103
104
105
106
# File 'app/controllers/concerns/glib/json/libs.rb', line 102

def glib_force_json_ui
  if params[:_render] != 'v1'
    redirect_to url_for(params.to_unsafe_h.merge(_render: 'v1'))
  end
end

#glib_handle_401(sign_in_url, **redirect_options) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/concerns/glib/json/libs.rb', line 124

def glib_handle_401(, **redirect_options)
  if !glib_json_dialog_mode? && request.get?
    json_ui_save_return_url
  end

  respond_to do |format|
    format.html do
      redirect_to , **redirect_options
    end
    format.json do
      render \
        json: json_ui_401_response(, **redirect_options),
        status: Rails.env.test? ? :unauthorized : :ok
    end
    format.csv do
      if Rails.env.test?
        render status: :unauthorized
      else
        redirect_to , **redirect_options
      end
    end
  end
end

#glib_handle_403(user_default_url) ⇒ Object

The behaviour provided by this convenience method assumes that user is logged in. When accessing a forbidden page, redirects to ‘user_default_url` because we don’t want the user to get confused in the scenario where after logging in, glib_redirect_back_or_to tries to open a page that the user doesn’t have access of.



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/concerns/glib/json/libs.rb', line 152

def glib_handle_403(user_default_url)
  respond_to do |format|
    format.html do
      redirect_to user_default_url
    end
    format.json do
      if __json_ui_rendering?
        redirect_to user_default_url
        return
      end

      json_response = \
        if glib_json_dialog_mode?
          {
            onLoad: __glib_error_dialog(
              'Access denied',
              "Make sure you're logged in with the correct account.",
              ['dialog-403-error']
            )
          }
        else
          {
            onResponse: {
              action: 'windows/open', url: user_default_url
            }
          }
        end
      render json: json_response, status: Rails.env.test? ? :forbidden : :ok
    end
    format.csv do
      if Rails.env.test?
        render status: :forbidden
      else
        redirect_to user_default_url
      end
    end
  end
end

#glib_handle_404Object

Raises:

  • (ActionController::RoutingError)


108
109
110
111
112
113
114
115
116
# File 'app/controllers/concerns/glib/json/libs.rb', line 108

def glib_handle_404
  raise ActionController::RoutingError.new('Not Found')

  # if json_ui_activated?
  #   render file: Rails.root.join('public', '404.html'), status: :not_found
  # else
  #   raise exception
  # end
end

#glib_html_url(url) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/concerns/glib/json/libs.rb', line 66

def glib_html_url(url)
  url = url.include?('?') ? url.sub(/.json\?/, '?') : url.sub(/.json$/, '')

  # As much as possible, try retaining the front fragment by matching the back, because
  # the front could be either a `?` or `&`
  url = url.sub(/format=json\&/, '')

  # If no match, then we replace the front fragment
  url.sub(/[\&\?]format=json/, '')
end

#glib_json_handle_404(exception, preview_mode) ⇒ Object



261
262
263
264
265
266
267
268
269
270
271
# File 'app/controllers/concerns/glib/json/libs.rb', line 261

def glib_json_handle_404(exception, preview_mode)
  if json_ui_activated?
    if Rails.env.production? || preview_mode
      render file: Rails.root.join('public', '404.html'), status: :not_found
    else
      raise exception
    end
  else
    raise exception
  end
end

#glib_json_handle_500(exception, preview_mode) ⇒ Object



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'app/controllers/concerns/glib/json/libs.rb', line 245

def glib_json_handle_500(exception, preview_mode)
  if json_ui_activated?
    if Rails.env.production? || preview_mode
      if defined?(Rollbar) && !preview_mode
        Rollbar.error(exception, use_exception_level_filters: true)
      end

      render file: Rails.root.join('public', '500.html'), status: :internal_server_error
    else
      raise exception
    end
  else
    raise exception
  end
end

#glib_redirect_to(url, return_to_previous: false, status: :ok) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/controllers/concerns/glib/json/libs.rb', line 77

def glib_redirect_to(url, return_to_previous: false, status: :ok)
  url = __delete_redirect_back_url(url) if return_to_previous

  respond_to do |format|
    format.html do
      redirect_to url
    end
    format.json do
      if __json_ui_rendering?
        redirect_to glib_html_url(url)
        return
      end

      # Inside a dialog, it's easier to simply use HTTP redirect, as opposed to trying to
      # get the dialog to perform manual redirection.
      if glib_json_dialog_mode?
        redirect_to url
        return
      end

      json_ui_redirect_to url, status: status
    end
  end
end

#json_ui_app_build_versionObject



20
21
22
23
24
# File 'app/controllers/concerns/glib/json/libs.rb', line 20

def json_ui_app_build_version
  @json_ui_app_build_version ||= request.headers['GApp-Build-Version'] || request.headers['JsonUiApp-Build-Version']
  @json_ui_app_build_version = params[:build_version] if @json_ui_app_build_version.nil? && Rails.env.development?  # For easy testing
  @json_ui_app_build_version
end

#json_ui_app_bundle_idObject



16
17
18
# File 'app/controllers/concerns/glib/json/libs.rb', line 16

def json_ui_app_bundle_id
  @json_ui_app_bundle_id ||= request.headers['GApp-Bundle-Id'] || request.headers['JsonUiApp-Bundle-Id']
end

#json_ui_app_device_osObject



26
27
28
29
30
# File 'app/controllers/concerns/glib/json/libs.rb', line 26

def json_ui_app_device_os
  @json_ui_app_device_os ||= request.headers['GApp-Device-Os'] || request.headers['JsonUiApp-Device-Os']
  @json_ui_app_device_os = params[:device_os] if @json_ui_app_device_os.nil? && Rails.env.development?  # For easy testing
  @json_ui_app_device_os || 'web'
end

#json_ui_app_is_android?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/controllers/concerns/glib/json/libs.rb', line 32

def json_ui_app_is_android?
  json_ui_app_device_os == 'android'
end

#json_ui_app_is_ios?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/controllers/concerns/glib/json/libs.rb', line 36

def json_ui_app_is_ios?
  json_ui_app_device_os == 'ios'
end

#json_ui_app_is_web?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'app/controllers/concerns/glib/json/libs.rb', line 40

def json_ui_app_is_web?
  json_ui_app_device_os == 'web'
end

#json_ui_etagObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/concerns/glib/json/libs.rb', line 44

def json_ui_etag
  template = "#{controller_name}/#{action_name}"

  # 1) It's necessary to pass in the template explicitly because of the switching between JSON
  # and HTML rendering.
  #
  # 2) When a model is not provided, two pages the use the same template (e.g. `projects#show`)
  # will produce the exact same ETAG, even though their content might be different. Presumably
  # this is because the ETAG is calculated based on the static template as opposed to the
  # generated output.
  #
  # However, this is not a problem because the browser will still treat these pages as
  # separate because they have different URLs (e.g. `projects/1` vs `projects/2`), meaning
  # that it will not accidentally use the cache from a different URL even though the ETAG
  # is exactly the same.
  fresh_when nil, template: template
end

#json_ui_redirect_to(url, status: :ok) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'app/controllers/concerns/glib/json/libs.rb', line 273

def json_ui_redirect_to(url, status: :ok)
  # Use windows_reload instead of windows_open so clicking back will skip the bridging page.
  # on_response = { action: 'windows/open', url: url }
  on_response = { action: 'windows/reload', url: url }

  if glib_json_dialog_mode?
    # Only do this in dialog mode because this seems to add to the rendering time
    # (i.e. longer flicker time), presumably due to the use of things like `nextTick()`.
    on_response = { action: 'dialogs/close', onClose: on_response }
  end

  # Use onResponse because it is harder to guarantee that `onLoad` executes in Vuejs because it relies
  # on app.vue's reactivity.
  render json: { onResponse: on_response }, status: status
end

#json_ui_render(template, args = {}) ⇒ Object



62
63
64
# File 'app/controllers/concerns/glib/json/libs.rb', line 62

def json_ui_render(template, args = {})
  JSON.parse(render_to_string(template, locals: args))
end

#json_ui_save_return_url(url = nil) ⇒ Object



120
121
122
# File 'app/controllers/concerns/glib/json/libs.rb', line 120

def json_ui_save_return_url(url = nil)
  session[REDIRECT_BACK_KEY] = url || url_for(params.to_unsafe_h.merge(format: nil))
end

#redirect_back_urlObject



191
192
193
# File 'app/controllers/concerns/glib/json/libs.rb', line 191

def redirect_back_url
  session[REDIRECT_BACK_KEY]
end