Module: ModalStack::ControllerExtensions

Extended by:
ActiveSupport::Concern
Defined in:
lib/modal_stack/controller_extensions.rb

Instance Method Summary collapse

Instance Method Details

True when the current request was issued by the modal_stack JS runtime (signaled by the X-Modal-Stack-Request header on the fetch).

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/modal_stack/controller_extensions.rb', line 44

def modal_stack_request?
  return false unless respond_to?(:request) && request

  request.headers[ModalStack::REQUEST_HEADER] == "1"
end

#render_modal(template_or_options = nil, **options) ⇒ Object

Convenience for re-rendering inside the modal layout, e.g. after a validation failure on update:

def update
  if @project.update(project_params)
    redirect_to @project
  else
    render_modal :edit, status: :unprocessable_entity
  end
end


60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/modal_stack/controller_extensions.rb', line 60

def render_modal(template_or_options = nil, **options)
  render_args =
    if template_or_options.is_a?(Hash)
      template_or_options.merge(options)
    elsif template_or_options
      { template_or_options => true, **options }
    else
      options
    end
  render_args[:layout] = "modal" unless render_args.key?(:layout)
  render(**render_args)
end