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
- #__delete_redirect_back_url(url) ⇒ Object
- #__glib_error_dialog(title, message) ⇒ Object
- #glib_force_json_ui ⇒ Object
- #glib_handle_401(sign_in_url) ⇒ Object
-
#glib_handle_403(user_default_url) ⇒ Object
The behaviour provided by this convenience method assumes that user is logged in.
- #glib_handle_404 ⇒ Object
- #glib_html_url(url) ⇒ Object
- #glib_json_handle_404(exception, preview_mode) ⇒ Object
- #glib_json_handle_500(exception, preview_mode) ⇒ Object
- #glib_redirect_to(url, return_to_previous: false) ⇒ Object
- #json_ui_app_build_version ⇒ Object
- #json_ui_app_bundle_id ⇒ Object
- #json_ui_app_device_os ⇒ Object
- #json_ui_app_is_android? ⇒ Boolean
- #json_ui_app_is_ios? ⇒ Boolean
- #json_ui_app_is_web? ⇒ Boolean
- #json_ui_etag ⇒ Object
-
#json_ui_redirect_back_or_to(default_url, json_action) ⇒ Object
def html_ui_redirect_back_or_to(default_url) redirect_url = __delete_redirect_back_url(default_url) redirect_to redirect_url end.
- #json_ui_redirect_to(url) ⇒ Object
- #json_ui_render(template, args = {}) ⇒ Object
- #json_ui_save_return_url(url = nil) ⇒ Object
Instance Method Details
#__delete_redirect_back_url(url) ⇒ Object
159 160 161 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 159 def __delete_redirect_back_url(url) session.delete(REDIRECT_BACK_KEY) || url end |
#__glib_error_dialog(title, message) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 175 def __glib_error_dialog(title, ) { 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": "label", "text": }, ], "padding": { "x": 20, "y": 22, }, } } end |
#glib_force_json_ui ⇒ Object
94 95 96 97 98 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 94 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) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 116 def glib_handle_401(sign_in_url) if !glib_json_dialog_mode? && request.get? session[REDIRECT_BACK_KEY] = url_for(params.to_unsafe_h.merge(format: nil)) end respond_to do |format| format.html do redirect_to sign_in_url end format.json do render json: json_ui_401_response(sign_in_url), status: Rails.env.test? ? :unauthorized : :ok 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.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 135 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 render json: if glib_json_dialog_mode? { onLoad: __glib_error_dialog('Access denied', "Make sure you're logged in with the correct account.") } else { onResponse: { action: 'windows/open', url: user_default_url } } end, status: Rails.env.test? ? :forbidden : :ok end end end |
#glib_handle_404 ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 100 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
65 66 67 68 69 70 71 72 73 74 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 65 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
213 214 215 216 217 218 219 220 221 222 223 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 213 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
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 197 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) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 76 def glib_redirect_to(url, return_to_previous: false) 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 json_ui_redirect_to url end end end |
#json_ui_app_build_version ⇒ Object
19 20 21 22 23 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 19 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_id ⇒ Object
15 16 17 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 15 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_os ⇒ Object
25 26 27 28 29 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 25 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
31 32 33 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 31 def json_ui_app_is_android? json_ui_app_device_os == 'android' end |
#json_ui_app_is_ios? ⇒ Boolean
35 36 37 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 35 def json_ui_app_is_ios? json_ui_app_device_os == 'ios' end |
#json_ui_app_is_web? ⇒ Boolean
39 40 41 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 39 def json_ui_app_is_web? json_ui_app_device_os == 'web' end |
#json_ui_etag ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 43 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_back_or_to(default_url, json_action) ⇒ Object
def html_ui_redirect_back_or_to(default_url)
redirect_url = __delete_redirect_back_url(default_url)
redirect_to redirect_url
end
168 169 170 171 172 173 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 168 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 subaction.windows_open url: redirect_url end end |
#json_ui_redirect_to(url) ⇒ Object
225 226 227 228 229 230 231 232 233 234 235 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 225 def json_ui_redirect_to(url) # `onLoad` also executes in `onResponse` situation, e.g. form submit. on_load = { action: 'windows/open', 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_load = { action: 'dialogs/close', onClose: on_load } end render json: { onLoad: on_load } end |
#json_ui_render(template, args = {}) ⇒ Object
61 62 63 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 61 def json_ui_render(template, args = {}) JSON.parse(render_to_string(template, locals: args)) end |
#json_ui_save_return_url(url = nil) ⇒ Object
112 113 114 |
# File 'app/controllers/concerns/glib/json/libs.rb', line 112 def json_ui_save_return_url(url = nil) session[REDIRECT_BACK_KEY] = url || url_for(params.to_unsafe_h.merge(format: nil)) end |