When rendering a valid kpop response in controllers, the current setup requires the definition of a layout, and a guard clause for a redirect.
I find this pattern is frequently repeated in one of the following implementations:
def FoobarsController < ApplicationController
def show
return redirect_to some_path, status: :see_other unless turbo_frame_request?
render locals: { object: @object }, layout: "kpop"
end
end
Or
def FoobarsController < ApplicationController
def show
if turbo_frame_request?
render locals: { object: @object }, layout: "kpop"
else
return redirect_to some_path, status: :see_other
end
end
end
Or variations of the above including the layout as a default for the controller
def FoobarsController < ApplicationController
layout "kpop"
...
end
Available options
.. To fill in
Potential use-case
An idea I've been playing with aims to simplify this logic in relevant controllers. This is for Kpop to provide a Backend helper which provides this functionality out of the box, which can be applied to any either individual controllers, or under the ApplicationController, in similar implementation to Katalyst Tables, Pagy, etc..
The following is the implementation concept I was hoping for:
module Admin
class ApplicationController < ActionController::Base
include Katalyst::Kpop::Backend
...
private
# Provide an optional override to an admin root
def default_return_path
admin_root_path
end
end
end
def FoobarsController < ApplicationController
def show
render_kpop locals: { object: @object }
end
private
# Provide an optional alt return path
def return_path
some_other_path
end
end
The following is a rough concept of what I had in mind to provide this functionality with ease;
module Katalyst
module Kpop
module Backend
extend ActiveSupport::Concern
KPOP_LAYOUT = "kpop"
# Render a KPOP modal or redirect to the assigned return path
# Provides a fallback to referer or default_return_path
#
# @param action [String] The current request action name, defaults to action_name
# @param locals [Hash] The locals to be passed to the view
# @param options [Hash] Additional options to pass to the render
def render_kpop(action = action_name, locals: {}, options: {})
if turbo_frame_request?
render action, locals:, **options, **variant, layout: KPOP_LAYOUT
else
redirect_to kpop_return_path, status: :see_other
end
end
private
# Provide a different variant name
def kpop_variant
nil
end
# Provide a different return path
def return_path
request.referer
end
# Provide a different default return path,
# e.g. admin_root_path or root_path
def default_return_path
root_path
end
# @private for internal usage only
def variant
return {} unless kpop_variant
{ variant: kpop_variant }
end
# @private for internal usage only
def kpop_return_path
return_path || default_return_path
end
end
end
end
Enhancement suggestion
Motiviation
.. To fill in
Current use-case
When rendering a valid kpop response in controllers, the current setup requires the definition of a layout, and a guard clause for a redirect.
I find this pattern is frequently repeated in one of the following implementations:
Or
Or variations of the above including the layout as a default for the controller
Available options
.. To fill in
Potential use-case
An idea I've been playing with aims to simplify this logic in relevant controllers. This is for Kpop to provide a Backend helper which provides this functionality out of the box, which can be applied to any either individual controllers, or under the
ApplicationController
, in similar implementation to Katalyst Tables, Pagy, etc..The following is the implementation concept I was hoping for:
The following is a rough concept of what I had in mind to provide this functionality with ease;