phlex-rails 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7235a9f512ec98cba7ac0c7b433c6b8862a79393d7759283f9374c2de12cbc30
4
- data.tar.gz: 63090c17e5a2996f2dce0da406fb97a3e58a43bfdc7799120cef331f3eea5133
3
+ metadata.gz: 407ff3d219b8d6ec6bbe429422b18125dde3f9cfb73ec756bad2ab4f08953083
4
+ data.tar.gz: b0e4f337a9626bf67a5e4dfd4172648cf69c264008fde9a5e3c1b456b98d7e72
5
5
  SHA512:
6
- metadata.gz: 3f6488be48a2585d14065193a5c15b8c96218926f1617ece544062fc2d6957a4ab0a43f4d9a46ce610ff5c2cc883c40547af9764699444976755df67784dc55f
7
- data.tar.gz: 1154d9f20816c76a233b4d83e919189a7c020fa0b7e364c113ab496867b40d33e19d6516571c7237baa11ce96de78bbcbcc2522f4acc614e8d6122f1227dfc9b
6
+ metadata.gz: b4e29f9f569bc0259d037d7b8a6e3ee8fb267d8210b2faf1e0a952d25275419e27c9621b615aad8be7d3f5531872848a63e46fdeb1a3ec8506e9babc5b27b1af
7
+ data.tar.gz: 4440057f8e253c7f0f78dbd41e4f92c6a8e4faeeac4cb339fbc26d09bc7bafa72302d2f2bc066b8879579b4dc3c053ba07f301622a8801d9a635a216f0ef2ac3
@@ -29,7 +29,7 @@ module Phlex
29
29
  input(
30
30
  name: "authenticity_token",
31
31
  type: "hidden",
32
- value: @_view_context.form_authenticity_token
32
+ value: helpers.form_authenticity_token
33
33
  )
34
34
  end
35
35
 
@@ -42,11 +42,11 @@ module Phlex
42
42
  end
43
43
 
44
44
  def url
45
- @_view_context.url_for(@model)
45
+ helpers.url_for(@model)
46
46
  end
47
47
 
48
48
  def field_name(*field)
49
- @_view_context.field_name(ActiveModel::Naming.param_key(@model.class), *field)
49
+ helpers.field_name(ActiveModel::Naming.param_key(@model.class), *field)
50
50
  end
51
51
 
52
52
  input_field :url_field, type: "url"
@@ -5,7 +5,7 @@ module Phlex
5
5
  module Helpers
6
6
  module CSPMetaTag
7
7
  def csp_meta_tag(...)
8
- if (output = @_view_context.csp_meta_tag(...))
8
+ if (output = helpers.csp_meta_tag(...))
9
9
  @_target << output
10
10
  end
11
11
  end
@@ -13,7 +13,7 @@ module Phlex
13
13
 
14
14
  module CSRFMetaTags
15
15
  def csrf_meta_tags
16
- if (output = @_view_context.csrf_meta_tags)
16
+ if (output = helpers.csrf_meta_tags)
17
17
  @_target << output
18
18
  end
19
19
  end
@@ -21,7 +21,7 @@ module Phlex
21
21
 
22
22
  module ActionCableMetaTag
23
23
  def action_cable_meta_tag
24
- if (output = @_view_context.action_cable_meta_tag)
24
+ if (output = helpers.action_cable_meta_tag)
25
25
  @_target << output
26
26
  end
27
27
  end
@@ -67,7 +67,7 @@ module Phlex
67
67
  end
68
68
 
69
69
  def form_with(*args, **kwargs, &block)
70
- @_target << @_view_context.form_with(*args, **kwargs) { |form|
70
+ @_target << helpers.form_with(*args, **kwargs) { |form|
71
71
  capture do
72
72
  yield(
73
73
  BufferedFormWith.new(form, buffer: @_target)
@@ -79,7 +79,7 @@ module Phlex
79
79
 
80
80
  module StylesheetLinkTag
81
81
  def stylesheet_link_tag(...)
82
- if (output = @_view_context.stylesheet_link_tag(...))
82
+ if (output = helpers.stylesheet_link_tag(...))
83
83
  @_target << output
84
84
  end
85
85
  end
@@ -87,7 +87,7 @@ module Phlex
87
87
 
88
88
  module FaviconLinkTag
89
89
  def favicon_link_tag(...)
90
- if (output = @_view_context.favicon_link_tag(...))
90
+ if (output = helpers.favicon_link_tag(...))
91
91
  @_target << output
92
92
  end
93
93
  end
@@ -95,7 +95,7 @@ module Phlex
95
95
 
96
96
  module ImageTag
97
97
  def image_tag(...)
98
- if (output = @_view_context.image_tag(...))
98
+ if (output = helpers.image_tag(...))
99
99
  @_target << output
100
100
  end
101
101
  end
@@ -103,7 +103,7 @@ module Phlex
103
103
 
104
104
  module AudioTag
105
105
  def audio_tag(...)
106
- if (output = @_view_context.audio_tag(...))
106
+ if (output = helpers.audio_tag(...))
107
107
  @_target << output
108
108
  end
109
109
  end
@@ -111,7 +111,7 @@ module Phlex
111
111
 
112
112
  module VideoTag
113
113
  def video_tag(...)
114
- if (output = @_view_context.video_tag(...))
114
+ if (output = helpers.video_tag(...))
115
115
  @_target << output
116
116
  end
117
117
  end
@@ -119,7 +119,7 @@ module Phlex
119
119
 
120
120
  module PreloadLinkTag
121
121
  def preload_link_tag(...)
122
- if (output = @_view_context.preload_link_tag(...))
122
+ if (output = helpers.preload_link_tag(...))
123
123
  @_target << output
124
124
  end
125
125
  end
@@ -127,7 +127,7 @@ module Phlex
127
127
 
128
128
  module JavaScriptIncludeTag
129
129
  def javascript_include_tag(...)
130
- if (output = @_view_context.javascript_include_tag(...))
130
+ if (output = helpers.javascript_include_tag(...))
131
131
  @_target << output
132
132
  end
133
133
  end
@@ -135,7 +135,7 @@ module Phlex
135
135
 
136
136
  module JavaScriptImportmapTags
137
137
  def javascript_importmap_tags(...)
138
- if (output = @_view_context.javascript_importmap_tags(...))
138
+ if (output = helpers.javascript_importmap_tags(...))
139
139
  @_target << output
140
140
  end
141
141
  end
@@ -143,7 +143,7 @@ module Phlex
143
143
 
144
144
  module JavaScriptImportModuleTag
145
145
  def javascript_import_module_tag(...)
146
- if (output = @_view_context.javascript_import_module_tag(...))
146
+ if (output = helpers.javascript_import_module_tag(...))
147
147
  @_target << output
148
148
  end
149
149
  end
@@ -151,7 +151,7 @@ module Phlex
151
151
 
152
152
  module ContentFor
153
153
  def content_for(slot, &block)
154
- @_view_context.content_for(slot, capture(&block))
154
+ helpers.content_for(slot, capture(&block))
155
155
  end
156
156
  end
157
157
  end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Rails
5
+ module HTML
6
+ module AppendMethodAddedWarning
7
+ def method_added(name)
8
+ if name == :append || name == :safe_append
9
+ raise Phlex::NameError, "You shouldn't redefine the #{name} method as it's required for safe HTML output."
10
+ end
11
+
12
+ super
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Rails
5
+ module HTML
6
+ module ClassMethods
7
+ def render_in(...)
8
+ new.render_in(...)
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Phlex
4
+ module Rails
5
+ module HTML
6
+ module Overrides
7
+ def helpers
8
+ if defined?(ViewComponent::Base) && @_view_context.is_a?(ViewComponent::Base)
9
+ @_view_context.helpers
10
+ else
11
+ @_view_context
12
+ end
13
+ end
14
+
15
+ def render(renderable, *args, **kwargs, &block)
16
+ return super if renderable.is_a?(Phlex::HTML)
17
+ return super if renderable.is_a?(Class) && renderable < Phlex::HTML
18
+
19
+ @_target << @_view_context.render(renderable, *args, **kwargs, &block)
20
+
21
+ nil
22
+ end
23
+
24
+ def render_in(view_context, &block)
25
+ if block_given?
26
+ call(view_context: view_context) do |*args|
27
+ view_context.with_output_buffer(self) do
28
+ original_length = @_target.length
29
+
30
+ if args.length == 1 && Phlex::HTML === args[0]
31
+ output = yield(
32
+ args[0].unbuffered
33
+ )
34
+ else
35
+ output = yield(*args)
36
+ end
37
+
38
+ unchanged = (original_length == @_target.length)
39
+
40
+ if unchanged
41
+ if output.is_a?(ActiveSupport::SafeBuffer)
42
+ unsafe_raw(output)
43
+ else
44
+ text(output)
45
+ end
46
+ end
47
+ end
48
+
49
+ nil
50
+ end.html_safe
51
+ else
52
+ call(view_context: view_context).html_safe
53
+ end
54
+ end
55
+
56
+ def safe_append=(value)
57
+ return unless value
58
+
59
+ @_target << case value
60
+ when String then value
61
+ when Symbol then value.name
62
+ else value.to_s
63
+ end
64
+ end
65
+
66
+ def append=(value)
67
+ return unless value
68
+
69
+ if value.html_safe?
70
+ self.safe_append = value
71
+ else
72
+ @_target << case value
73
+ when String then ERB::Util.html_escape(value)
74
+ when Symbol then ERB::Util.html_escape(value.name)
75
+ else ERB::Util.html_escape(value.to_s)
76
+ end
77
+ end
78
+ end
79
+
80
+ def capture
81
+ super&.html_safe
82
+ end
83
+
84
+ # Trick ViewComponent into thinking we're a ViewComponent to fix rendering output
85
+ def set_original_view_context(view_context)
86
+ end
87
+
88
+ private def yield_content(&block)
89
+ return unless block
90
+
91
+ case block.binding.receiver
92
+ when Phlex::HTML
93
+ super
94
+ else
95
+ @_view_context.with_output_buffer(self) { super }
96
+ end
97
+ end
98
+
99
+ private def yield_content_with_args(*args, &block)
100
+ return unless block
101
+
102
+ case block.binding.receiver
103
+ when Phlex::HTML
104
+ super
105
+ else
106
+ @_view_context.with_output_buffer(self) { super }
107
+ end
108
+ end
109
+ end
110
+ end
111
+ end
112
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Phlex
4
4
  module Rails
5
- VERSION = "0.5.0"
5
+ VERSION = "0.5.1"
6
6
  end
7
7
  end
data/lib/phlex/rails.rb CHANGED
@@ -7,19 +7,12 @@ module Phlex::Rails
7
7
  Loader = Zeitwerk::Loader.new.tap do |loader|
8
8
  loader.push_dir("#{__dir__}/rails", namespace: Phlex::Rails)
9
9
  loader.inflector = Zeitwerk::GemInflector.new(__FILE__)
10
+ loader.inflector.inflect("html" => "HTML")
10
11
  loader.setup
11
12
  end
12
13
 
13
- module AppendMethodAddedWarning
14
- def method_added(name)
15
- if name == :append || name == :safe_append
16
- raise Phlex::NameError, "You shouldn't redefine the #{name} method as it's required for safe HTML output."
17
- end
14
+ Phlex::HTML.prepend(Phlex::Rails::HTML::Overrides)
18
15
 
19
- super
20
- end
21
- end
22
-
23
- Phlex::HTML.prepend(Phlex::Rails::Renderable)
24
- Phlex::HTML.extend(Phlex::Rails::AppendMethodAddedWarning)
16
+ Phlex::HTML.extend(Phlex::Rails::HTML::ClassMethods)
17
+ Phlex::HTML.extend(Phlex::Rails::HTML::AppendMethodAddedWarning)
25
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: phlex-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joel Drapper
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-01-30 00:00:00.000000000 Z
11
+ date: 2023-02-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: phlex
@@ -104,8 +104,10 @@ files:
104
104
  - lib/phlex/rails/engine.rb
105
105
  - lib/phlex/rails/form.rb
106
106
  - lib/phlex/rails/helpers.rb
107
+ - lib/phlex/rails/html/append_method_added_warning.rb
108
+ - lib/phlex/rails/html/class_methods.rb
109
+ - lib/phlex/rails/html/overrides.rb
107
110
  - lib/phlex/rails/layout.rb
108
- - lib/phlex/rails/renderable.rb
109
111
  - lib/phlex/rails/version.rb
110
112
  - lib/phlex/testing/rails/view_helper.rb
111
113
  - lib/tasks/phlex_tasks.rake
@@ -1,106 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Phlex
4
- module Rails
5
- module Renderable
6
- def helpers
7
- @_view_context
8
- end
9
-
10
- def render(renderable, *args, **kwargs, &block)
11
- return super if renderable.is_a?(Phlex::HTML)
12
- return super if renderable.is_a?(Class) && renderable < Phlex::HTML
13
-
14
- @_target << @_view_context.render(renderable, *args, **kwargs, &block)
15
-
16
- nil
17
- end
18
-
19
- def render_in(view_context, &block)
20
- if block_given?
21
- call(view_context: view_context) do |*args|
22
- view_context.with_output_buffer(self) do
23
- original_length = @_target.length
24
-
25
- if args.length == 1 && Phlex::HTML === args[0]
26
- output = yield(
27
- args[0].unbuffered
28
- )
29
- else
30
- output = yield(*args)
31
- end
32
-
33
- unchanged = (original_length == @_target.length)
34
-
35
- if unchanged
36
- if output.is_a?(ActiveSupport::SafeBuffer)
37
- unsafe_raw(output)
38
- else
39
- text(output)
40
- end
41
- end
42
- end
43
-
44
- nil
45
- end.html_safe
46
- else
47
- call(view_context: view_context).html_safe
48
- end
49
- end
50
-
51
- def safe_append=(value)
52
- return unless value
53
-
54
- @_target << case value
55
- when String then value
56
- when Symbol then value.name
57
- else value.to_s
58
- end
59
- end
60
-
61
- def append=(value)
62
- return unless value
63
-
64
- if value.html_safe?
65
- self.safe_append = value
66
- else
67
- @_target << case value
68
- when String then ERB::Util.html_escape(value)
69
- when Symbol then ERB::Util.html_escape(value.name)
70
- else ERB::Util.html_escape(value.to_s)
71
- end
72
- end
73
- end
74
-
75
- def capture
76
- super&.html_safe
77
- end
78
-
79
- # Trick ViewComponent into thinking we're a ViewComponent to fix rendering output
80
- def set_original_view_context(view_context)
81
- end
82
-
83
- private def yield_content(&block)
84
- return unless block
85
-
86
- case block.binding.receiver
87
- when Phlex::HTML
88
- super
89
- else
90
- @_view_context.with_output_buffer(self) { super }
91
- end
92
- end
93
-
94
- private def yield_content_with_args(*args, &block)
95
- return unless block
96
-
97
- case block.binding.receiver
98
- when Phlex::HTML
99
- super
100
- else
101
- @_view_context.with_output_buffer(self) { super }
102
- end
103
- end
104
- end
105
- end
106
- end