Skip to content

Commit

Permalink
Move kpop_redirect_to and kpop_dismiss to turbo_stream.kpop.*
Browse files Browse the repository at this point in the history
  • Loading branch information
sfnelson committed Oct 25, 2023
1 parent 3a46e6c commit f3881a3
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 32 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ PATH
specs:
katalyst-kpop (3.0.0.pre.alpha.1)
html-attributes-utils
turbo-rails
view_component

GEM
Expand Down
24 changes: 0 additions & 24 deletions app/helpers/kpop_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,6 @@
module KpopHelper
using HTMLAttributesUtils

# Render a turbo stream action that will dismiss any open kpop modals.
def kpop_dismiss(id: "kpop")
turbo_stream.append(id) do
tag.div("", data: {
controller: "kpop--close",
kpop__close_kpop__frame_outlet: "##{id}",
turbo_temporary: "",
})
end
end

# Renders a kpop redirect controller response that will escape the frame and navigate to the given URL.
def kpop_redirect_to(url, id: "kpop", target: nil)
turbo_stream.append(id) do
tag.div("", data: {
controller: "kpop--redirect",
kpop__redirect_kpop__frame_outlet: "##{id}",
kpop__redirect_path_value: url,
kpop__redirect_target_value: target,
turbo_temporary: "",
})
end
end

# Renders a link that will navigate the kpop turbo frame to the given URL.
# The URL should render a modal response inside a kpop frame tag.
def kpop_link_to(name = nil, options = nil, html_attributes = nil, &block)
Expand Down
1 change: 1 addition & 0 deletions katalyst-kpop.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ Gem::Specification.new do |spec|
spec.metadata["rubygems_mfa_required"] = "true"

spec.add_dependency "html-attributes-utils"
spec.add_dependency "turbo-rails"
spec.add_dependency "view_component"
end
1 change: 1 addition & 0 deletions lib/katalyst/kpop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "view_component"

require "katalyst/kpop/engine"
require "katalyst/kpop/turbo"
require "katalyst/kpop/version"

module Katalyst
Expand Down
15 changes: 12 additions & 3 deletions lib/katalyst/kpop/engine.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
# frozen_string_literal: true

require "rails"
require "turbo-rails"

module Katalyst
module Kpop
class Engine < ::Rails::Engine
config.autoload_once_paths = %W(#{root}/app/helpers)

PRECOMPILE_ASSETS = %w(kpop.js kpop.min.js kpop.min.js.map).freeze

initializer "kpop.assets" do
if Rails.application.config.respond_to?(:assets)
Rails.application.config.assets.precompile += PRECOMPILE_ASSETS
end
end

initializer "kpop.helpers", before: :load_config_initializers do
ActiveSupport.on_load(:action_controller_base) do
helper Katalyst::Kpop::Engine.helpers
::Turbo::Streams::TagBuilder.define_method(:kpop) do
Katalyst::Kpop::Turbo::TagBuilder.new(self)
end

ActiveSupport.on_load(:action_view_base) do
ActiveSupport.on_load(:action_controller_base) do
helper Katalyst::Kpop::Engine.helpers
end
end
Expand Down
56 changes: 56 additions & 0 deletions lib/katalyst/kpop/turbo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

require "katalyst/kpop/engine"

module Katalyst
module Kpop
module Turbo
class TagBuilder
delegate :action, :append, :tag, to: :@builder

def initialize(builder)
@builder = builder
end

# Open a modal in the kpop frame identified by <tt>id</tt> either the <tt>content</tt> passed in or a
# rendering result determined by the <tt>rendering</tt> keyword arguments, the content in the block,
# or the rendering of the content as a record. Examples:
#
# <%= turbo_stream.kpop.open modal %>
# <%= turbo_stream.kpop.open partial: "modals/modal", locals: { record: } %>
# <%= turbo_stream.kpop.open do %>
# <%= render Kpop::ModalComponent.new(title: "Example") do %>
# ...
# <% end %>
# <% end %>
def open(content = nil, id: "kpop", **, &)
@builder.action(:kpop_open, id, content, **, &)
end

# Render a turbo stream action that will dismiss any open kpop modal.
def dismiss(id: "kpop")
append(id) do
tag.div("", data: {
controller: "kpop--close",
kpop__close_kpop__frame_outlet: "##{id}",
turbo_temporary: "",
})
end
end

# Renders a kpop redirect controller response that will escape the frame and navigate to the given URL.
def redirect_to(url, id: "kpop", target: nil)
append(id) do
tag.div("", data: {
controller: "kpop--redirect",
kpop__redirect_kpop__frame_outlet: "##{id}",
kpop__redirect_path_value: url,
kpop__redirect_target_value: target,
turbo_temporary: "",
})
end
end
end
end
end
end
10 changes: 5 additions & 5 deletions spec/dummy/app/controllers/modals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ def update
@error = true
render status: :unprocessable_entity
when "frame"
render turbo_stream: helpers.kpop_redirect_to(modal_path, target: "kpop")
render turbo_stream: turbo_stream.kpop.redirect_to(modal_path, target: "kpop")
when "content"
render turbo_stream: helpers.kpop_redirect_to(modal_path)
render turbo_stream: turbo_stream.kpop.redirect_to(modal_path)
else
render turbo_stream: turbo_stream.action(:kpop_open, "kpop", template: "modals/stream")
render turbo_stream: turbo_stream.kpop.open(template: "modals/stream")
end
end

Expand All @@ -42,7 +42,7 @@ def redirect_forwards
redirect_to test_path, status: :see_other
end
format.turbo_stream do
render turbo_stream: helpers.kpop_redirect_to(test_path)
render turbo_stream: turbo_stream.kpop.redirect_to(test_path)
end
end
end
Expand All @@ -53,7 +53,7 @@ def close_modal
redirect_to root_path
end
format.turbo_stream do
render turbo_stream: helpers.kpop_dismiss
render turbo_stream: turbo_stream.kpop.dismiss
end
end
end
Expand Down

0 comments on commit f3881a3

Please sign in to comment.