Skip to content

Commit e377c73

Browse files
committed
Revert change setting #format.
1 parent 7a86995 commit e377c73

File tree

6 files changed

+14
-26
lines changed

6 files changed

+14
-26
lines changed

docs/CHANGELOG.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ nav_order: 6
1010

1111
## main
1212

13+
* Revert change setting `#format`. In GitHub's codebase, the change led to hard-to-detect failures. For example, components rendered from controllers included layouts when they didn't before. In other cases, the response `content_type` changed, breaking downstream consumers. For cases where a specific content type is needed, use:
14+
15+
```ruby
16+
respond_to do |f|
17+
f.html_fragment do
18+
render(MyComponent.new)
19+
end
20+
end
21+
```
22+
23+
*Joel Hawksley*
24+
1325
## 4.0.0.rc4
1426

1527
* Fix issue where generators were not included in published gem.

lib/view_component/base.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,6 @@ def config
7171
# Config option that strips trailing whitespace in templates before compiling them.
7272
class_attribute :__vc_strip_trailing_whitespace, instance_accessor: false, instance_predicate: false, default: false
7373

74-
class_attribute :__vc_response_format, instance_accessor: false, instance_predicate: false, default: nil
75-
7674
attr_accessor :__vc_original_view_context
7775
attr_reader :current_template
7876

@@ -356,10 +354,6 @@ def content?
356354
__vc_render_in_block_provided? || __vc_content_set_by_with_content_defined?
357355
end
358356

359-
def format
360-
self.class.__vc_response_format
361-
end
362-
363357
# @private
364358
def with_original_virtual_path
365359
@view_context.instance_variable_set(:@virtual_path, @old_virtual_path)

lib/view_component/compiler.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,6 @@ def compile(raise_errors: false, force: false)
4848

4949
define_render_template_for
5050

51-
# Set the format if the component only responds to a single format.
52-
# Unfortunately we cannot determine which format a multi-format
53-
# component will respond to until render time, so those components
54-
# will not set the response format.
55-
#
56-
# TODO: Investigate upstream changes necessary to support multi-format renderables
57-
unique_formats = templates.map(&:format).uniq
58-
@component.__vc_response_format = unique_formats.last if unique_formats.one?
59-
6051
@component.__vc_register_default_slots
6152
@component.__vc_build_i18n_backend
6253

@@ -118,7 +109,6 @@ def template_errors
118109
errors << "Couldn't find a template file or inline render method for #{@component}." if @templates.empty?
119110

120111
@templates
121-
.reject { |template| template.inline_call? && !template.defined_on_self? }
122112
.map { |template| [template.variant, template.format] }
123113
.tally
124114
.select { |_, count| count > 1 }

lib/view_component/template.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class Inline < Template
4343
attr_reader :source
4444

4545
def initialize(component:, inline_template:)
46-
details = ActionView::TemplateDetails.new(nil, inline_template.language.to_sym, DEFAULT_FORMAT, nil)
46+
details = ActionView::TemplateDetails.new(nil, inline_template.language.to_sym, nil, nil)
4747

4848
super(
4949
component: component,
@@ -63,7 +63,7 @@ def type
6363
class InlineCall < Template
6464
def initialize(component:, method_name:, defined_on_self:)
6565
variant = method_name.to_s.include?("call_") ? method_name.to_s.sub("call_", "").to_sym : nil
66-
details = ActionView::TemplateDetails.new(nil, nil, DEFAULT_FORMAT, variant)
66+
details = ActionView::TemplateDetails.new(nil, nil, nil, variant)
6767

6868
super(component: component, details: details)
6969

test/sandbox/test/inline_template_test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,4 @@ class InlineComponentDerivedFromComponentSupportingVariants < Level2Component
169169

170170
assert_selector(".greeting-container h1", text: "Hello, Fox Mulder!")
171171
end
172-
173-
test "defines format" do
174-
assert_equal(:html, InlineErbComponent.new("Tester").format)
175-
end
176172
end

test/sandbox/test/rendering_test.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,6 @@ def test_render_without_template
130130
assert_selector("input[type='text'][name='name']")
131131
end
132132

133-
def test_inline_call_component_format
134-
assert_equal(:html, InlineComponent.new.format)
135-
end
136-
137133
def test_render_child_without_template
138134
render_inline(InlineChildComponent.new)
139135

0 commit comments

Comments
 (0)