Module: Glib::Json::Ui

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

Instance Method Summary collapse

Instance Method Details

#__json_ui_commit(options) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/controllers/concerns/glib/json/ui.rb', line 94

def __json_ui_commit(options)
  return if response.status >= 300

  case @__json_ui_rendering
  when :vue
    if (hash = json_transformation_start).is_a?(Hash)
      __json_ui_vue(hash, options)
    else
      raise "Invalid JSON UI payload: #{hash}"
    end
  end
end

#__json_ui_rendering?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/controllers/concerns/glib/json/ui.rb', line 35

def __json_ui_rendering?
  @__json_ui_rendering != nil
end

#__json_ui_start(options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/concerns/glib/json/ui.rb', line 69

def __json_ui_start(options)
  @__json_ui_activated = false
  @__json_ui_rendering = nil

  when_option = options[:when]
  if when_option.is_a?(Proc)
    # should_render = when_option.call(self)
    should_render = instance_exec(&when_option)
  else
    should_render = when_option == :always
  end

  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

Returns:

  • (Boolean)


31
32
33
# File 'app/controllers/concerns/glib/json/ui.rb', line 31

def json_ui_activated?
  @__json_ui_activated
end

#json_ui_page_lifecycle_prop(name) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/concerns/glib/json/ui.rb', line 39

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_propObject



58
59
60
61
62
63
64
65
66
67
# File 'app/controllers/concerns/glib/json/ui.rb', line 58

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_url_optionsObject

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 json_ui_url_options
  options = {}
  options[:_render] = params[:_render]
  options[:_locale] = params[:_locale]
  options[:_preview] = params[:_preview]
  options[:format] = :json if request.format == :json
  options
end