-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into remove-p-shorthand-post-deprecation
* main: Ignore /tmp if you're running BT in tmp/starter like CI does Fix some CHANGELOG issues and refit some examples + words Skip needless CI steps (#60) We don't need to depend on sqlite3 that was for an earlier version of the ViewComponent integration Fix #54 to use new section yield syntax Mixing `yield` call styles (#54) Expose Sections as an alternative to `content_for` (#57) Introduce Capybara to the test suite (#55) Add `content_from` to let partials relay contents (#53) v0.1.9 v0.1.8 Document release 🎉 (#52) Rename `Partial#output_buffer` to `Partial#yield` (#41) Allow outer partial access when capturing (#49) Fix accessing `partial` before rendering leaks state (#47) Use load hooks for monkey_patch loading (#48)
- Loading branch information
Showing
27 changed files
with
654 additions
and
107 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Gemfile.lock | ||
/tmp | ||
/pkg | ||
*~ |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,163 @@ | ||
## CHANGELOG | ||
|
||
* Let partials respond to named content sections | ||
|
||
```erb | ||
<% partial.content_for :title, "Title content" %> # Before | ||
<% partial.title "Title content" %> # After | ||
# Which can then be output | ||
<% partial.title %> # => "Title content" | ||
<% partial.title? %> # => true | ||
``` | ||
|
||
Note, `title` responds to `present?` so rendering could also be made conditional with: | ||
|
||
```erb | ||
<% partial.title if partial.title? %> # Instead of this… | ||
<% partial.title.presence %> # …you can do this | ||
``` | ||
|
||
#### Passing procs or components | ||
|
||
Procs and objects that implement `render_in`, like ViewComponents, can also be appended as content: | ||
|
||
```erb | ||
<% partial.title { "some content" } %> | ||
<% partial.title TitleComponent.new(Current.user) %> | ||
``` | ||
|
||
#### Capturing `options` | ||
|
||
Options can also be captured and output: | ||
|
||
```erb | ||
<% partial.title class: "text-m4" %> # partial.title.options # => { class: "text-m4" } | ||
# When output `to_s` is called and options automatically pipe through `tag.attributes`: | ||
<h1 <% partial.title.options %>> # => <h1 class="text-m4"> | ||
``` | ||
|
||
#### Proxying to the view context and appending content | ||
|
||
A content section appends to its content when calling any view context method on it, e.g.: | ||
|
||
```erb | ||
<% partial.title.t ".title" %> | ||
<% partial.title.link_to @document.name, @document %> | ||
<% partial.title.render "title", user: Current.user %> | ||
<% partial.title.render TitleComponent.new(Current.user) do |component| %> | ||
<% … %> | ||
<% end %> | ||
``` | ||
|
||
#### Building elements with `tag` proxy | ||
|
||
These `tag` calls let you generate elements based on the stored content and options: | ||
|
||
```erb | ||
<% partial.title "content", class: "post-title" %> # Adding some content and options… | ||
<% partial.title.h2 %> # => <h2 class="post-title">content</h2> | ||
<% partial.title.h2 "more" %> # => <h2 class="post-title">contentmore</h2> | ||
``` | ||
|
||
* Add `NicePartials#t` to aid I18n. | ||
|
||
When using NicePartials with I18n you end up with lots of calls that look like: | ||
|
||
```erb | ||
<% partial.title t(".title") %> | ||
<% partial.description t(".header") %> | ||
<% partial.byline t("custom.key") %> | ||
``` | ||
|
||
With NicePartials' `t` method, you can write the above as: | ||
|
||
```erb | ||
<% partial.t :title, description: :header, byline: "custom.key" %> | ||
``` | ||
|
||
Clarifying what keys get converted to what content sections on the partial rather than the syntax heavy `partial.… t(".…")`. | ||
|
||
Like the Rails built-in `t` method, it's just a shorthand alias for `translate` so that's available too. | ||
|
||
* Add `Partial#content_from` | ||
|
||
`content_from` lets a partial extract contents from another partial. | ||
Additionally, contents can be renamed by passing a hash: | ||
|
||
```erb | ||
<% partial.title "Hello there" %> | ||
<% partial.byline "Somebody" %> | ||
<%= render "shared/title" do |cp| %> | ||
# Here the inner partial `cp` accesses the outer partial through `partial` | ||
# extracting the `title` and `byline` contents. | ||
# `byline` is renamed to `name` in `cp`. | ||
<% cp.content_from partial, :title, byline: :name %> | ||
<% end %> | ||
``` | ||
|
||
### 0.1.9 | ||
|
||
* Remove need to insert `<% yield p = np %>` in partials. | ||
|
||
Nice Partials now automatically captures blocks passed to `render`. | ||
Instead of `p`, a `partial` method has been added to access the | ||
current `NicePartials::Partial` object. | ||
|
||
Here's a script to help update your view code: | ||
|
||
```ruby | ||
files_to_inspect = [] | ||
|
||
Dir["app/views/**/*.html.erb"].each do |path| | ||
if contents = File.read(path).match(/(<%=? yield\(?.*? = np\)? %>\n+)/m)&.post_match | ||
files_to_inspect << path if contents.match?(/render.*?do \|/) | ||
|
||
contents.gsub! /\bp\.(?=yield|helpers|content_for|content_for\?)/, "partial." | ||
File.write path, contents | ||
end | ||
end | ||
|
||
if files_to_inspect.any? | ||
puts "These files had render calls with a block parameter and likely require some manual edits:" | ||
puts files_to_inspect | ||
else | ||
puts "No files with render calls with a block parameter found, you're likely all set" | ||
end | ||
``` | ||
|
||
* Support manual `yield`s in partials. | ||
|
||
Due to the automatic yield support above, support has also been added for manual `yield some_object` calls. | ||
|
||
Nice Partials automatically appends the `partial` to the yielded arguments, so you can | ||
change `render … do |some_object|` to `render … do |some_object, partial|`. | ||
|
||
* Deprecate `p` as the partial object access. Use `partial` instead. | ||
|
||
* Expose `partial.yield` to access the captured output buffer. | ||
|
||
Lets you access what a `<%= yield %>` call returned, like this: | ||
|
||
```erb | ||
<%= render "card" do %> | ||
This is the content of the internal output buffer | ||
<% end %> | ||
``` | ||
|
||
```erb | ||
# app/views/cards/_card.html.erb | ||
# This can be replaced with `partial.yield`. | ||
<%= yield %> # Will output "This is the content of the internal output buffer" | ||
``` | ||
|
||
### 0.1.7 | ||
|
||
* Rely on `ActiveSupport.on_load :action_view` | ||
* Add support for Ruby 3.0 | ||
|
||
### 0.1.0 | ||
|
||
* Initial release | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,7 @@ gemspec | |
gem "minitest" | ||
gem "rake" | ||
gem "irb" | ||
|
||
gem "view_component" | ||
gem "rails" | ||
gem "capybara" |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#!/usr/bin/env ruby | ||
$: << File.expand_path("../test", __dir__) | ||
|
||
require "bundler/setup" | ||
require "rails/plugin/test" |
This file contains 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
This file contains 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
Oops, something went wrong.