Skip to content
This repository has been archived by the owner on Jan 19, 2023. It is now read-only.

[WIP] Explore exposable concept #9

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/turbo_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require "turbo-rails"
require "turbo_component/version"
require "turbo_component/engine"
require "turbo_component/railtie"

module TurboComponent
extend ActiveSupport::Autoload
Expand All @@ -19,6 +20,7 @@ module Concerns
autoload :Options
autoload :Routes
autoload :Tags
autoload :Exposable
end
end
end
21 changes: 21 additions & 0 deletions lib/turbo_component/concerns/exposable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module TurboComponent::Concerns::Exposable
extend ActiveSupport::Concern

included do
def self.expose_in_context(method_name)
prefix = self.name.deconstantize.underscore
module_ref = self
exposed_name = "#{prefix}_#{method_name}".to_sym

ApplicationController.class_eval do
define_method exposed_name do
module_ref.send(method_name, self)
end

helper_method exposed_name
end

exposed_name
end
end
end
12 changes: 12 additions & 0 deletions lib/turbo_component/railtie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module TurboComponent
class Railtie < Rails::Railtie
# initializer "turbo_component.action_controller" do
# ActiveSupport.on_load(:action_controller) do
# helpers = Dir[Rails.root.join("app", "turbo_components", "**", "*helpers.rb")]
# helpers.each do |helper_file|
# module_name
# end
# end
# end
end
end
1 change: 1 addition & 0 deletions test/dummy/app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
Counter::Helpers
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ def increment
session[:counter] ||= 0
session[:counter] += 1

Turbo::StreamsChannel.broadcast_replace_to :counter, targets: "#counter", content: helpers.turbo_component(:counter, async: false)
# Since this demo is based on the session, we should scope the broadcast by "session.id"
turbo_stream_id = counter_turbo_stream_id
Turbo::StreamsChannel.broadcast_replace_to turbo_stream_id, targets: "#counter", content: helpers.turbo_component(:counter, async: false)

# We don't have to return nothing since we will update the component with Turbo::StreamChannel
head :ok
end
end
9 changes: 9 additions & 0 deletions test/dummy/app/turbo_components/counter/helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module Counter::Helpers
include TurboComponent::Concerns::Exposable

def self.turbo_stream_id(context)
"#{context.session.id}-counter"
end

expose_in_context :turbo_stream_id
end
2 changes: 1 addition & 1 deletion test/dummy/app/views/welcome/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h1> Welcome </h1>
<%= turbo_stream_from :counter %>
<%= turbo_stream_from counter_turbo_stream_id %>
<%= turbo_component :counter, async: false do %>
loading...
<% end %>
Expand Down