Skip to content

Commit

Permalink
feature: partial! api for rendering partials without setting to key (#34
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kortirso authored Oct 14, 2024
1 parent e1cd215 commit cc9862e
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,13 @@ json.posts do
end
```

Usage for rendering partial without assigning it to some key:

```ruby
json.partial! partial: "posts/blog_post", locals: {post: @post} do
end
```

### Partial Fragments
**Note** This is a [SuperglueJS][1] specific functionality.

Expand Down
1 change: 1 addition & 0 deletions lib/props_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class << self
self.template_lookup_options = {handlers: [:props]}

delegate :result!, :array!,
:partial!,
:extract!,
:deferred!,
:fragments!,
Expand Down
4 changes: 4 additions & 0 deletions lib/props_template/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ def array!(collection, options = {})
nil
end

def partial!(**options)
@context.render options
end

# json.id item.id
# json.value item.value
#
Expand Down
60 changes: 60 additions & 0 deletions spec/props_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,66 @@
end
end

context "partial!" do
it "renders with a partial" do
json = render(<<~PROPS)
json.partial! partial: 'simple'
PROPS

expect(json).to eql_json({
foo: "bar"
})
end

it "renders with locale" do
json = render(<<~PROPS)
json.partial! partial: 'simple', locale: :de
PROPS

expect(json).to eql_json({
foo: "Kein"
})
end

it "renders with variants" do
json = render(<<~PROPS)
json.partial! partial: 'simple', variants: :grid
PROPS

expect(json).to eql_json({
foo: "Grid"
})
end

it "renders with a partial with locals" do
json = render(<<~PROPS)
json.partial! partial: 'profile', locals: {email: '[email protected]'}
PROPS

expect(json).to eql_json({
email: "[email protected]"
})
end

it "renders an array of partials" do
json = render(<<~PROPS)
emails = [
'[email protected]',
'[email protected]',
]
json.array! emails do |email|
json.partial! partial: 'profile', locals: {email: email}
end
PROPS

expect(json).to eql_json([
{email: "[email protected]"},
{email: "[email protected]"}
])
end
end

context "extract!" do
it "extracts values for hash" do
object = { :foo => "bar", "bar" => "foo", :wiz => "wiz" }
Expand Down

0 comments on commit cc9862e

Please sign in to comment.