Skip to content

Commit 1a6992d

Browse files
authored
Merge branch 'main' into relative-translation-keys-inside-blocks-resolve-to-partials-scope-instead-of-callers-scope
2 parents 169c94a + ecc66e8 commit 1a6992d

File tree

13 files changed

+139
-12
lines changed

13 files changed

+139
-12
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ group :development, :test do
2121
gem "cuprite"
2222
gem "dry-initializer", require: true
2323
gem "erb_lint"
24-
gem "haml", "~> 6"
24+
gem "haml", "~> 7"
2525
gem "jbuilder", "~> 2"
2626
gem "m", "~> 1"
2727
gem "method_source", "~> 1"

Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ GEM
141141
websocket-driver (~> 0.7)
142142
globalid (1.3.0)
143143
activesupport (>= 6.1)
144-
haml (6.4.0)
144+
haml (7.0.2)
145145
temple (>= 0.8.2)
146146
thor
147147
tilt
@@ -428,7 +428,7 @@ DEPENDENCIES
428428
cuprite
429429
dry-initializer
430430
erb_lint
431-
haml (~> 6)
431+
haml (~> 7)
432432
jbuilder (~> 2)
433433
m (~> 1)
434434
method_source (~> 1)

docs/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@ nav_order: 6
1414

1515
*Oussama Hilal*
1616

17+
* Allow I18n calls in `render?`.
18+
19+
*23tux*
20+
21+
* ViewComponent now works without `rails` and `railties` gems loaded, enabling compatibility with Bridgetown 2.0.
22+
23+
*Tom Lord*
24+
25+
* Capture partial block in the component's context, allowing access to the component instance inside the block.
26+
27+
*23tux*
28+
29+
* Add `after_compile` class method hook to enable extensions to run logic after component compilation.
30+
31+
*Jose Solás*
32+
1733
* Fix outdated reference to preview layout configuration in docs.
1834

1935
*Lucas Geron*

lib/view_component.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module ViewComponent
1717
autoload :Preview
1818
autoload :Translatable
1919

20-
if Rails.env.test?
20+
if defined?(Rails.env) && Rails.env.test?
2121
autoload :TestHelpers
2222
autoload :SystemSpecHelpers
2323
autoload :SystemTestHelpers

lib/view_component/base.rb

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def config
4646
end
4747

4848
include ActionView::Helpers
49-
include Rails.application.routes.url_helpers if defined?(Rails) && Rails.application
49+
include Rails.application.routes.url_helpers if defined?(Rails.application.routes)
5050
include ERB::Escape
5151
include ActiveSupport::CoreExt::ERBUtil
5252

@@ -131,15 +131,14 @@ def render_in(view_context, &block)
131131

132132
@__vc_content_evaluated = false
133133
@__vc_render_in_block = block
134+
@view_context.instance_variable_set(:@virtual_path, virtual_path)
134135

135136
before_render
136137

137138
if render?
138139
value = nil
139140

140141
@output_buffer.with_buffer do
141-
@view_context.instance_variable_set(:@virtual_path, virtual_path)
142-
143142
rendered_template =
144143
around_render do
145144
render_template_for(@__vc_requested_details).to_s
@@ -260,14 +259,17 @@ def render(options = {}, args = {}, &block)
260259
@view_context.render(options, args, &block)
261260
elsif block
262261
__vc_original_view_context.render(options, args) do
262+
# capture the block output in the view context of the component
263+
output = capture(&block)
264+
263265
# Partials are rendered to their own buffer and do not append to the
264266
# original @output_buffer we retain a reference to in #render_in. This
265267
# is a problem since the block passed to us here in the #render method
266268
# is evaluated within the context of ViewComponent::Base, and thus
267269
# appends to the original @output_buffer. To avoid this, we evaluate the
268270
# block in the view context instead, which will append to the output buffer
269271
# created for the partial.
270-
__vc_original_view_context.instance_exec(&block)
272+
__vc_original_view_context.capture { output }
271273
end
272274
else
273275
__vc_original_view_context.render(options, args)
@@ -301,7 +303,7 @@ def helpers
301303
@__vc_helpers ||= __vc_original_view_context || controller.view_context
302304
end
303305

304-
if ::Rails.env.development? || ::Rails.env.test?
306+
if defined?(Rails.env) && (::Rails.env.development? || ::Rails.env.test?)
305307
# @private
306308
def method_missing(method_name, *args) # rubocop:disable Style/MissingRespondToMissing
307309
super
@@ -330,7 +332,7 @@ def view_cache_dependencies
330332
[]
331333
end
332334

333-
if Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
335+
if defined?(Rails::VERSION) && Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
334336
# Rails expects us to define `format` on all renderables,
335337
# but we do not know the `format` of a ViewComponent until runtime.
336338
def format
@@ -610,6 +612,16 @@ def __vc_compiled?
610612
__vc_compiler.compiled?
611613
end
612614

615+
# Hook called by the compiler after a component is compiled.
616+
#
617+
# Extensions can override this class method to run logic after
618+
# compilation (e.g., generate helpers, register metadata, etc.).
619+
#
620+
# By default, this is a no-op.
621+
def after_compile
622+
# no-op by default
623+
end
624+
613625
# @private
614626
def __vc_ensure_compiled
615627
__vc_compile unless __vc_compiled?

lib/view_component/collection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def each(&block)
2020
components.each(&block)
2121
end
2222

23-
if Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
23+
if defined?(Rails::VERSION) && Rails::VERSION::MAJOR == 7 && Rails::VERSION::MINOR == 1
2424
# Rails expects us to define `format` on all renderables,
2525
# but we do not know the `format` of a ViewComponent until runtime.
2626
def format

lib/view_component/compiler.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def compile(raise_errors: false, force: false)
5252
@component.__vc_build_i18n_backend
5353

5454
CompileCache.register(@component)
55+
56+
@component.after_compile
5557
end
5658
end
5759

lib/view_component/config.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def default_previews_options
163163
options = ActiveSupport::OrderedOptions.new
164164
options.controller = "ViewComponentsController"
165165
options.route = "/rails/view_components"
166-
options.enabled = Rails.env.development? || Rails.env.test?
166+
options.enabled = defined?(Rails.env) && (Rails.env.development? || Rails.env.test?)
167167
options.default_layout = nil
168168
options.paths = default_preview_paths
169169
options
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<%= render "shared/yielding_partial" do %>
2+
<%= world %>
3+
<% end %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class PartialWithYieldAndMethodCallComponent < ViewComponent::Base
2+
def world
3+
"world"
4+
end
5+
end

0 commit comments

Comments
 (0)