-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This improves the developer experience by adding rendering defaults for superglue views. Superglue typically requires 3 templates. ``` app/views/ posts/ index.html.erb index.jsx index.json.props users/ index.html.erb index.jsx index.json.props ``` In most cases `index.html.erb` is the same, so the immediate thought is to remove it. This PR makes the following possible: ``` app/views application/ superglue.html.erb posts/ index.jsx index.json.props users/ index.jsx index.json.props ``` In the above scenario, we can now render a common template using `superglue_template` in the controller: ``` class PostsController < ApplicationController before_action :use_jsx_rendering_defaults superglue_template "application/superglue" end ``` This PR also adds a custom resolver that allows for the following scenario: ``` app/views application/ superglue.html.erb posts/ index.jsx users/ index.jsx ``` This case is great for projects that have their own API and just want to use superglue for the rails like routing and take advantage of the UJS helpers. In that case, they can skip using `props_template`. Note that we still need the `app/views/layout/application.json.props` that gets generated.
- Loading branch information
Showing
34 changed files
with
351 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,3 @@ breezy/build/**/*.js | |
props_template/performance/**/*.png | ||
.tool-versions | ||
testapp/ | ||
superglue/ |
This file contains 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
5 changes: 5 additions & 0 deletions
5
lib/generators/superglue/install/templates/erb/superglue.html.erb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<script type="text/javascript"> | ||
window.SUPERGLUE_INITIAL_PAGE_STATE=<%= render_props %>;<%# erblint:disable ErbSafety %> | ||
</script> | ||
|
||
<div id="app"></div> |
This file contains 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
10 changes: 0 additions & 10 deletions
10
lib/generators/superglue/view_collection/templates/erb/edit.html.erb
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
lib/generators/superglue/view_collection/templates/erb/index.html.erb
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
lib/generators/superglue/view_collection/templates/erb/new.html.erb
This file was deleted.
Oops, something went wrong.
9 changes: 0 additions & 9 deletions
9
lib/generators/superglue/view_collection/templates/erb/show.html.erb
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require "active_support/concern" | ||
|
||
module Superglue | ||
module Rendering | ||
REACT_FORMATS = [:tsx, :jsx] | ||
|
||
extend ActiveSupport::Concern | ||
|
||
included do |base| | ||
base.class_attribute :_superglue_template, instance_accessor: true, default: "application/superglue" | ||
end | ||
|
||
class_methods do | ||
def superglue_template(template) | ||
self._superglue_template = template | ||
end | ||
end | ||
|
||
def use_jsx_rendering_defaults | ||
@_use_jsx_rendering_defaults = true | ||
end | ||
|
||
def _jsx_defaults | ||
@_use_jsx_rendering_defaults && request.format.html? | ||
end | ||
|
||
def _ensure_react_page!(template, prefixes) | ||
lookup_context.find(template, prefixes, false, [], formats: [], handlers: [], variants: [], locale: []) | ||
end | ||
|
||
def default_render | ||
if _jsx_defaults | ||
_ensure_react_page!(action_name.to_s, _prefixes) | ||
render | ||
else | ||
super | ||
end | ||
end | ||
|
||
def render_props | ||
if @_render_options | ||
if template_exists?(@_render_options[:template].to_s, @_render_options[:prefixes], formats: [:json]) | ||
render_to_string(@_render_options.merge({formats: [:json], layout: true})).strip.html_safe | ||
else | ||
render_to_string(@_render_options.merge({inline: "", formats: [:json], layout: true})).strip.html_safe | ||
end | ||
end | ||
end | ||
|
||
def render(...) | ||
if _jsx_defaults | ||
@_render_options = _normalize_render(...) | ||
_ensure_react_page!(@_render_options[:template].to_s, @_render_options[:prefixes]) | ||
|
||
html_template_exist = template_exists?(@_render_options[:template].to_s, @_render_options[:prefixes], false) | ||
|
||
if html_template_exist | ||
super | ||
else | ||
super(@_render_options.merge({template: _superglue_template, prefixes: []})) | ||
end | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
require 'action_view' | ||
|
||
module Superglue | ||
class Resolver < ActionView::FileSystemResolver | ||
class JsxPathParser < ActionView::Resolver::PathParser | ||
REACT_FORMATS = [:tsx, :jsx] | ||
|
||
def build_path_regex | ||
formats = Regexp.union(REACT_FORMATS.map(&:to_s)) | ||
|
||
%r{ | ||
\A | ||
(?:(?<prefix>.*)/)? | ||
(?<action>.*?) | ||
(?:\.(?<format>#{formats}))?? | ||
\z | ||
}x | ||
end | ||
|
||
def parse(path) | ||
@regex ||= build_path_regex | ||
match = @regex.match(path) | ||
path = ActionView::TemplatePath.build(match[:action], match[:prefix] || "", false) | ||
details = ActionView::TemplateDetails.new( | ||
nil, | ||
nil, | ||
match[:format]&.to_sym, | ||
nil | ||
) | ||
ParsedPath.new(path, details) | ||
end | ||
end | ||
|
||
def initialize(path) | ||
raise ArgumentError, "path already is a Resolver class" if path.is_a?(ActionView::Resolver) | ||
@unbound_templates = Concurrent::Map.new | ||
@path_parser = JsxPathParser.new | ||
@path = File.expand_path(path) | ||
end | ||
|
||
def clear_cache | ||
@unbound_templates.clear | ||
@path_parser = JsxPathParser.new | ||
end | ||
|
||
def source_for_template(template) | ||
"''" | ||
end | ||
|
||
def filter_and_sort_by_details(templates, requested_details) | ||
if requested_details.formats.empty? | ||
templates | ||
else | ||
[] | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.