superglue 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,88 @@
1
+ require "active_support/concern"
2
+
3
+ module Superglue
4
+ module Rendering
5
+ class UnsupportedOption < StandardError; end
6
+
7
+ extend ActiveSupport::Concern
8
+
9
+ included do |base|
10
+ base.class_attribute :_superglue_template, instance_accessor: true, default: "application/superglue"
11
+ end
12
+
13
+ class_methods do
14
+ def superglue_template(template)
15
+ self._superglue_template = template
16
+ end
17
+ end
18
+
19
+ def _render_template(options = {})
20
+ if @_capture_options_before_render
21
+ @_capture_options_before_render = false
22
+
23
+ if options.keys.intersect? [:file, :partial, :body, :plain, :html, :inline]
24
+ raise UnsupportedOption.new("`template:` and `action:` are the only options supported with `use_jsx_rendering_defaults`")
25
+ end
26
+
27
+ @_render_options = options
28
+ _ensure_react_page!(options[:template], options[:prefixes])
29
+
30
+ html_template_exist = template_exists?(options[:template], options[:prefixes], false)
31
+ if !html_template_exist
32
+ super(options.merge(
33
+ template: _superglue_template,
34
+ prefixes: []
35
+ ))
36
+ else
37
+ super
38
+ end
39
+ else
40
+ super
41
+ end
42
+ end
43
+
44
+ def render(...)
45
+ if _jsx_defaults
46
+ @_capture_options_before_render = true
47
+ end
48
+
49
+ super
50
+ end
51
+
52
+ def use_jsx_rendering_defaults
53
+ @_use_jsx_rendering_defaults = true
54
+ end
55
+
56
+ def _jsx_defaults
57
+ @_use_jsx_rendering_defaults && request.format.html?
58
+ end
59
+
60
+ def _ensure_react_page!(template, prefixes)
61
+ lookup_context.find(template, prefixes, false, [], formats: [], handlers: [], variants: [], locale: [])
62
+ end
63
+
64
+ def default_render
65
+ if _jsx_defaults
66
+ render
67
+ else
68
+ super
69
+ end
70
+ end
71
+
72
+ def render_props
73
+ if @_render_options
74
+ options = @_render_options
75
+
76
+ if template_exists?(options[:template], options[:prefixes], formats: [:json])
77
+ _render_template(options.merge({formats: [:json], layout: _layout_for_option(true)})).strip.html_safe
78
+ else
79
+ options.delete(:template)
80
+ options.delete(:action)
81
+ _render_template(options.merge({inline: "", formats: [:json], layout: _layout_for_option(true)})).strip.html_safe
82
+ end
83
+ else
84
+ ""
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,58 @@
1
+ require 'action_view'
2
+
3
+ module Superglue
4
+ class Resolver < ActionView::FileSystemResolver
5
+ class JsxPathParser < ActionView::Resolver::PathParser
6
+ REACT_FORMATS = [:tsx, :jsx]
7
+
8
+ def build_path_regex
9
+ formats = Regexp.union(REACT_FORMATS.map(&:to_s))
10
+
11
+ %r{
12
+ \A
13
+ (?:(?<prefix>.*)/)?
14
+ (?<action>.*?)
15
+ (?:\.(?<format>#{formats}))??
16
+ \z
17
+ }x
18
+ end
19
+
20
+ def parse(path)
21
+ @regex ||= build_path_regex
22
+ match = @regex.match(path)
23
+ path = ActionView::TemplatePath.build(match[:action], match[:prefix] || "", false)
24
+ details = ActionView::TemplateDetails.new(
25
+ nil,
26
+ nil,
27
+ match[:format]&.to_sym,
28
+ nil
29
+ )
30
+ ParsedPath.new(path, details)
31
+ end
32
+ end
33
+
34
+ def initialize(path)
35
+ raise ArgumentError, "path already is a Resolver class" if path.is_a?(ActionView::Resolver)
36
+ @unbound_templates = Concurrent::Map.new
37
+ @path_parser = JsxPathParser.new
38
+ @path = File.expand_path(path)
39
+ end
40
+
41
+ def clear_cache
42
+ @unbound_templates.clear
43
+ @path_parser = JsxPathParser.new
44
+ end
45
+
46
+ def source_for_template(template)
47
+ "''"
48
+ end
49
+
50
+ def filter_and_sort_by_details(templates, requested_details)
51
+ if requested_details.formats.empty?
52
+ templates
53
+ else
54
+ []
55
+ end
56
+ end
57
+ end
58
+ end
data/lib/superglue.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  require "superglue/helpers"
2
2
  require "superglue/redirection"
3
+ require "superglue/rendering"
4
+ require "superglue/resolver"
3
5
  require "props_template"
4
6
  require "form_props"
5
7
 
@@ -9,8 +11,10 @@ module Superglue
9
11
  include Helpers
10
12
 
11
13
  def self.included(base)
14
+ base.include ::Superglue::Rendering
12
15
  if base.respond_to?(:helper_method)
13
16
  base.helper_method :param_to_dig_path
17
+ base.helper_method :render_props
14
18
  end
15
19
  end
16
20
  end
@@ -31,6 +35,10 @@ module Superglue
31
35
 
32
36
  if app.config.superglue.auto_include
33
37
  include Controller
38
+
39
+ prepend_view_path(
40
+ Superglue::Resolver.new(Rails.root.join("app/views"))
41
+ )
34
42
  end
35
43
  end
36
44
  end
metadata CHANGED
@@ -1,13 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: superglue
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johny Ho
8
+ autorequire:
8
9
  bindir: bin
9
10
  cert_chain: []
10
- date: 2025-01-06 00:00:00.000000000 Z
11
+ date: 2025-02-18 00:00:00.000000000 Z
11
12
  dependencies:
12
13
  - !ruby/object:Gem::Dependency
13
14
  name: actionpack
@@ -63,8 +64,10 @@ executables: []
63
64
  extensions: []
64
65
  extra_rdoc_files: []
65
66
  files:
67
+ - README.md
66
68
  - lib/generators/superglue/install/install_generator.rb
67
69
  - lib/generators/superglue/install/templates/application.json.props
70
+ - lib/generators/superglue/install/templates/erb/superglue.html.erb
68
71
  - lib/generators/superglue/install/templates/initializer.rb
69
72
  - lib/generators/superglue/install/templates/js/application.jsx
70
73
  - lib/generators/superglue/install/templates/js/application_visit.js
@@ -86,10 +89,6 @@ files:
86
89
  - lib/generators/superglue/install/templates/ts/tsconfig.json
87
90
  - lib/generators/superglue/scaffold/scaffold_generator.rb
88
91
  - lib/generators/superglue/scaffold_controller/scaffold_controller_generator.rb
89
- - lib/generators/superglue/view_collection/templates/erb/edit.html.erb
90
- - lib/generators/superglue/view_collection/templates/erb/index.html.erb
91
- - lib/generators/superglue/view_collection/templates/erb/new.html.erb
92
- - lib/generators/superglue/view_collection/templates/erb/show.html.erb
93
92
  - lib/generators/superglue/view_collection/templates/js/edit.jsx
94
93
  - lib/generators/superglue/view_collection/templates/js/index.jsx
95
94
  - lib/generators/superglue/view_collection/templates/js/new.jsx
@@ -107,10 +106,13 @@ files:
107
106
  - lib/superglue.rb
108
107
  - lib/superglue/helpers.rb
109
108
  - lib/superglue/redirection.rb
110
- homepage: https://github.com/thoughtbot/superglue/
109
+ - lib/superglue/rendering.rb
110
+ - lib/superglue/resolver.rb
111
+ homepage: https://github.com/thoughtbot/superglue_rails/
111
112
  licenses:
112
113
  - MIT
113
114
  metadata: {}
115
+ post_install_message:
114
116
  rdoc_options: []
115
117
  require_paths:
116
118
  - lib
@@ -125,7 +127,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
127
  - !ruby/object:Gem::Version
126
128
  version: '0'
127
129
  requirements: []
128
- rubygems_version: 3.6.2
130
+ rubygems_version: 3.5.3
131
+ signing_key:
129
132
  specification_version: 4
130
133
  summary: Rails integration for SuperglueJS
131
134
  test_files: []
@@ -1,10 +0,0 @@
1
- <%% initial_state = controller.render_to_string(active_template_virtual_path, formats: [:json], locals: local_assigns, layout: true) %>
2
-
3
- <script type="text/javascript">
4
- window.SUPERGLUE_INITIAL_PAGE_STATE=<%%= initial_state.html_safe %>;<%%# erblint:disable ErbSafety %>
5
- </script>
6
-
7
- <%%# If you need SSR follow instructions at %>
8
- <%%# https://github.com/thoughtbot/superglue/blob/main/docs/recipes/server-side-rendering.md %>
9
- <div id="app"></div>
10
-
@@ -1,9 +0,0 @@
1
- <%% initial_state = controller.render_to_string(active_template_virtual_path, formats: [:json], locals: local_assigns, layout: true) %>
2
-
3
- <script type="text/javascript">
4
- window.SUPERGLUE_INITIAL_PAGE_STATE=<%%= initial_state.html_safe %>;<%%# erblint:disable ErbSafety %>
5
- </script>
6
-
7
- <%%# If you need SSR follow instructions at %>
8
- <%%# https://github.com/thoughtbot/superglue/blob/main/docs/recipes/server-side-rendering.md %>
9
- <div id="app"></div>
@@ -1,9 +0,0 @@
1
- <%% initial_state = controller.render_to_string(active_template_virtual_path, formats: [:json], locals: local_assigns, layout: true) %>
2
-
3
- <script type="text/javascript">
4
- window.SUPERGLUE_INITIAL_PAGE_STATE=<%%= initial_state.html_safe %>;<%%# erblint:disable ErbSafety %>
5
- </script>
6
-
7
- <%%# If you need SSR follow instructions at %>
8
- <%%# https://github.com/thoughtbot/superglue/blob/main/docs/recipes/server-side-rendering.md %>
9
- <div id="app"></div>
@@ -1,9 +0,0 @@
1
- <%% initial_state = controller.render_to_string(active_template_virtual_path, formats: [:json], locals: local_assigns, layout: true) %>
2
-
3
- <script type="text/javascript">
4
- window.SUPERGLUE_INITIAL_PAGE_STATE=<%%= initial_state.html_safe %>;<%%# erblint:disable ErbSafety %>
5
- </script>
6
-
7
- <%%# If you need SSR follow instructions at %>
8
- <%%# https://github.com/thoughtbot/superglue/blob/main/docs/recipes/server-side-rendering.md %>
9
- <div id="app"></div>