Skip to content

Commit 3079fa1

Browse files
authored
Merge pull request #2400 from ViewComponent/2348-revert
Revert change setting #format
2 parents 7a86995 + 02d05bc commit 3079fa1

File tree

13 files changed

+14
-6655
lines changed

13 files changed

+14
-6655
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/app/components/turbo_content_type_component.html.erb

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/sandbox/app/components/turbo_content_type_component.rb

Lines changed: 0 additions & 10 deletions
This file was deleted.

test/sandbox/app/controllers/integration_examples_controller.rb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,4 @@ def multiple_formats_component
7575
def turbo_stream
7676
respond_to { |format| format.turbo_stream { render TurboStreamComponent.new } }
7777
end
78-
79-
def submit
80-
render TurboContentTypeComponent.new(
81-
message: "Submitted",
82-
show_form: false
83-
)
84-
end
8578
end

test/sandbox/app/views/integration_examples/turbo_content_type.html.erb

Lines changed: 0 additions & 20 deletions
This file was deleted.

test/sandbox/config/routes.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@
3535
get :slotable_default_override, to: "integration_examples#slotable_default_override"
3636
get :virtual_path_reset, to: "integration_examples#virtual_path_reset"
3737
post :create, to: "integration_examples#create"
38-
get :turbo_content_type, to: "integration_examples#turbo_content_type"
3938
get :translations_in_block, to: "integration_examples#translations_in_block"
40-
post :submit, to: "integration_examples#submit"
4139

4240
resources :posts
4341

0 commit comments

Comments
 (0)