Created
January 12, 2026 00:26
-
-
Save tcannonfodder/216a47a276ad21f1ee95f71dd9ce6b39 to your computer and use it in GitHub Desktop.
CableCar template renderer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| class CableCarTemplate | |
| attr_accessor :source | |
| def initialize(&block) | |
| self.source = block | |
| end | |
| def render_in(view_context) | |
| source.call | |
| view_context.controller.render( | |
| cable_ready: view_context.cable_car | |
| ) | |
| end | |
| def format | |
| :json | |
| end | |
| def self.call(template, source = nil) | |
| src = source || template.source | |
| <<~RUBY | |
| __cablecar_template__ = CableCarTemplate.new do | |
| #{src} | |
| end | |
| __cablecar_template__.render_in(self).to_s | |
| RUBY | |
| end | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # frozen_string_literal: true | |
| dialog_id = dom_id(@organization_deal, :new_update) | |
| html_string = controller.render_to_string(ExampleComponent.new( | |
| open: true, | |
| id: dialog_id | |
| )) | |
| cable_car.append( | |
| html: html_string, | |
| selector: "#example", | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## ... | |
| initializer("cable_ready.template_renderer") do | |
| ActiveSupport.on_load :action_view do | |
| ActionView::Template.register_template_handler :cablecar, CableCarTemplate | |
| end | |
| end |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
of rendering variants/complexity, and fit into the larger Rails
MVC flow; we should have a template handler for CableReady
CableCarTemplateuses Rails'render_insupport to render theobject using the given
view_context.the source of the template
render_inis called, we make sure to call the source sothat it is evaluated, then we reach back into the view context's
controller to use the predefined renderer from CableReady, to
keep in line with the main app itself
CableCarTemplate.callmethod takes in the template/source,coalesces it into a
srcthat we can pass into an instance ofCableCarTemplate, which we then callrender_inusing theActionView context
:cablecar, so the file format wouldbe
[template].cable_ready.cablecarjbuilder,which makes it clear what renderer is being used