Module: Glib::Json::Ui
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/controllers/concerns/glib/json/ui.rb
Instance Method Summary collapse
- #__json_ui_commit(options) ⇒ Object
- #__json_ui_rendering? ⇒ Boolean
- #__json_ui_start(options) ⇒ Object
-
#form_authenticity_token(**args) ⇒ Object
Override.
- #json_ui_activated? ⇒ Boolean
-
#json_ui_commit_now ⇒ Object
Call this after
render-ing a json_ui response from a HALTING callback, e.g. - #json_ui_page_lifecycle_prop(name) ⇒ Object
- #json_ui_response_lifecycle_prop ⇒ Object
- #json_ui_should_render?(options) ⇒ Boolean
-
#json_ui_url_options ⇒ Object
NOTE: Override default_url_options and call this method.
Instance Method Details
#__json_ui_commit(options) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'app/controllers/concerns/glib/json/ui.rb', line 107 def __json_ui_commit() return if response.status >= 300 case @__json_ui_rendering when :vue if (hash = json_transformation_start).is_a?(Hash) __json_ui_vue(hash, ) else raise "Invalid JSON UI payload: #{hash}" end end end |
#__json_ui_rendering? ⇒ Boolean
45 46 47 |
# File 'app/controllers/concerns/glib/json/ui.rb', line 45 def __json_ui_rendering? @__json_ui_rendering != nil end |
#__json_ui_start(options) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/controllers/concerns/glib/json/ui.rb', line 89 def __json_ui_start() @__json_ui_activated = false @__json_ui_rendering = nil should_render = json_ui_should_render?() if should_render || params[:_render] == 'v1' @__json_ui_activated = true request.variant = :ui # Some bots (e.g. Line and DuckDuckGo) passes `*/*` in the `Accept` header is_html = request.format.html? || request.format == '*/*' if is_html && params[:_skip_render] != 'true' @__json_ui_rendering = :vue request.format = 'json' end end end |
#form_authenticity_token(**args) ⇒ Object
Override
17 18 19 |
# File 'app/controllers/concerns/glib/json/ui.rb', line 17 def form_authenticity_token(**args) Rails.env.test? ? 'test_token' : super end |
#json_ui_activated? ⇒ Boolean
31 32 33 |
# File 'app/controllers/concerns/glib/json/ui.rb', line 31 def json_ui_activated? @__json_ui_activated end |
#json_ui_commit_now ⇒ Object
Call this after render-ing a json_ui response from a HALTING callback,
e.g. a rate_limit handler or any before_action that renders. Halting
skips the rest of the callback chain, including the after_action that
swaps the JSON body for the HTML shell on browser page loads — without
this, an HTML request would receive raw JSON. Safe to call
unconditionally: the swap is a no-op for JSON/XHR requests.
41 42 43 |
# File 'app/controllers/concerns/glib/json/ui.rb', line 41 def json_ui_commit_now __json_ui_commit(self.class.) end |
#json_ui_page_lifecycle_prop(name) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'app/controllers/concerns/glib/json/ui.rb', line 59 def json_ui_page_lifecycle_prop(name) if (hash = json_transformation_start).is_a?(Hash) && hash['body'] # A valid page original_action = hash[name] if original_action && original_action['action'] == 'runMultiple' child_actions = original_action['childActions'] else hash[name] = { 'action' => 'runMultiple', 'childActions' => [] } child_actions = hash[name]['childActions'] child_actions << original_action end # hash[name] ||= { 'action' => 'runMultiple', 'childActions' => [] } # child_actions = hash[name]['childActions'] child_actions end end |
#json_ui_response_lifecycle_prop ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'app/controllers/concerns/glib/json/ui.rb', line 78 def json_ui_response_lifecycle_prop prop_name = 'onResponse' if (hash = json_transformation_start).is_a?(Hash) && hash[prop_name] # A valid response if hash[prop_name]['action'] != 'runMultiple' childActions = [hash[prop_name]] hash[prop_name] = { 'action' => 'runMultiple', 'childActions' => childActions } childActions end end end |
#json_ui_should_render?(options) ⇒ Boolean
49 50 51 52 53 54 55 56 57 |
# File 'app/controllers/concerns/glib/json/ui.rb', line 49 def json_ui_should_render?() when_option = [:when] if when_option.is_a?(Proc) # should_render = when_option.call(self) instance_exec(&when_option) else when_option == :always end end |
#json_ui_url_options ⇒ Object
NOTE: Override default_url_options and call this method
22 23 24 25 26 27 28 29 |
# File 'app/controllers/concerns/glib/json/ui.rb', line 22 def = {} [:_render] = params[:_render] [:_locale] = params[:_locale] [:_preview] = params[:_preview] [:format] = :json if request.format == :json end |