forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 0
Add default #render_in
implementation to ActiveModel::Conversion
#2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
seanpdoyle
wants to merge
3
commits into
action-view-render-in-options
Choose a base branch
from
active-model-conversion-render-in
base: action-view-render-in-options
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add default #render_in
implementation to ActiveModel::Conversion
#2
seanpdoyle
wants to merge
3
commits into
action-view-render-in-options
from
active-model-conversion-render-in
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9e9f75f
to
bbff5f5
Compare
8a1754a
to
cb1b21b
Compare
Closes [rails#45432][] Support for objects that respond to `#render_in` was introduced in [rails#36388][] and [rails#37919][]. Those implementations assume that the instance will all the context it needs to render itself. That assumption doesn't account for call-site arguments like `locals: { ... }` or a block. This commit expands support for rendering with a `:renderable` option to incorporate locals and blocks. For example: ```ruby class Greeting def render_in(view_context, **options, &block) if block view_context.render plain: block.call else case Array(options[:formats]).first when :json view_context.render json: { greeting: "Hello, World!" } else view_context.render **options, inline: "<%= Hello, <%= name %>!" end end end end render(Greeting.new) # => "Hello, World!" render(Greeting.new, name: "Local") # => "Hello, Local!" render(Greeting.new) { "Hello, Block!" } # => "Hello, Block!" render(renderable: Greeting.new, formats: :json) # => "{\"greeting\":\"Hello, World!\"}" ``` Since existing tools depend on the `#render_in(view_context)` signature without options, this commit deprecates that signature in favor of one that accepts options and a block. [rails#45432]: rails#45432 [rails#36388]: rails#36388 [rails#37919]: rails#37919
cb1b21b
to
b554fd3
Compare
bbff5f5
to
af82a27
Compare
159b549
to
1ba6b9f
Compare
Follow-up to [rails#46202][] Without overriding the new `#render_in` method, previous behavior will be preserved: render the view partial determined by calling `#to_partial_path`, then pass `object: self`. With the following view partial: ```erb <%# app/views/people/_person.html.erb %> <% local_assigns.with_defaults(shout: false) => { shout: } %> <%= shout ? person.name.upcase : person.name %> ``` Callers can render an instance of `Person` as a positional argument or a `:renderable` option: ```ruby person = Person.new(name: "Ralph") render person # => "Ralph" render person, shout: true # => "RALPH" render renderable: person # => "Ralph" render renderable: person, locals: { shout: true } # => "RALPH" ``` This preserves backward compatibility. At the same time, the `#render_in` method provides applications with an more flexibility, and an opportunity to manage how to transform models into Strings. For example, users of ViewComponent can map a model directly to a `ViewComponent::Base` instance. [rails#46202]: rails#46202 (comment)
af82a27
to
6123414
Compare
8f14f10
to
39af464
Compare
39af464
to
1d390d1
Compare
4161fe4
to
94926db
Compare
94926db
to
1c1918c
Compare
465e84a
to
98f1705
Compare
98f1705
to
56a1e49
Compare
56a1e49
to
97a7a97
Compare
77867f2
to
cd57c45
Compare
3b2546e
to
7f2d39e
Compare
7f2d39e
to
dbed3b1
Compare
dbed3b1
to
4c26fcd
Compare
a92e0e8
to
33d6dad
Compare
33d6dad
to
1ea1ac5
Compare
1ea1ac5
to
50c601b
Compare
fdc3103
to
7614159
Compare
7614159
to
15f9f6a
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Follow-up to #46202
Without overriding the new
#render_in
method, previous behavior will be preserved: render the view partial determined by calling#to_partial_path
, then passobject: self
.With the following view partial:
Callers can render an instance of
Person
as a positional argument or a:renderable
option:This preserves backward compatibility. At the same time, the
#render_in
method provides applications with an more flexibility, and an opportunity to manage how to transform models into Strings. For example, users of ViewComponent can map a model directly to aViewComponent::Base
instance.Summary
Other Information