-
Notifications
You must be signed in to change notification settings - Fork 443
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
Render collections #274
Merged
Merged
Render collections #274
Changes from 28 commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
e1f0bce
Sketch out API for rendering collections
tclem c8997b1
Final newlines are nice
tclem 698fa7d
Rename template
tclem 0fe965f
Handle :as, fill out test coverage
tclem 7aaef37
Test out not using :as
tclem d6f435e
Merge remote-tracking branch 'origin/master' into render-collections
tclem 6df4749
Name the ivar item for clarity
tclem 6750a3a
Change up the api to .all(collection: ...)
tclem 6d96256
Move default kw to as_variable
tclem 6a3e54c
Auto generate item name
tclem 492c711
Handle passing collection object directly
tclem 85806b4
Duplicative of ProductCoupon
tclem a41527b
Just change the listing order
tclem 4b7e367
Move ViewComponent:Collection to a dedicated file
tclem c872eae
Draft a changelog entry
tclem 2255967
Add myself to the readme and tidy contributors
tclem 606d568
Add space after comma
tclem 9baecd1
Use the component class name directly
tclem 556de9e
More specific asserts for extra arguments
tclem f897365
ViewComponent::Collection constructor can be private
tclem 3793dd3
Be defensive against component changing the args hash
tclem f294f99
s/all/with_collection
tclem 44f326d
Iterate on the api
tclem 9f1bc8a
Bring tests up-to-date
tclem 84a6495
Express these as list items
tclem 6c3e7ee
Wire up an integration test
tclem 7196bef
html_safe is required for rendered content to show up
tclem 1b186fe
Different versions of ruby show slightly different errors here
tclem 340122b
Update api in changelog entry
tclem 4364c69
Bump version to 2.1.0
tclem 6612f8b
No need to use ivars in these tests
tclem 5a13abe
Update test/view_component/view_component_test.rb
tclem a1b85fc
Revert "Bump version to 2.1.0"
tclem 93b3cdc
Add some documentation to the readme
tclem b09ca46
Didn't mean to replace these
tclem c32d066
Update CHANGELOG.md
joelhawksley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,30 @@ | ||
|
||
# frozen_string_literal: true | ||
|
||
module ViewComponent | ||
class Collection | ||
def render_in(view_context, &block) | ||
as = @component.collection_parameter_name | ||
|
||
@collection.map do |item| | ||
@component.new(@options.merge(as => item)).render_in(view_context, &block) | ||
end.join.html_safe | ||
end | ||
|
||
private | ||
|
||
def initialize(component, object, **options) | ||
@component = component | ||
@collection = collection_variable(object || []) | ||
@options = options | ||
end | ||
tclem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def collection_variable(object) | ||
if object.respond_to?(:to_ary) | ||
object.to_ary | ||
else | ||
raise ArgumentError.new("The value of the argument isn't a valid collection. Make sure it responds to to_ary: #{object.inspect}") | ||
end | ||
end | ||
end | ||
end |
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,7 @@ | ||
<li> | ||
<h1>Product</h1> | ||
<h2><%= @product.name %></h2> | ||
<p> | ||
<%= @notice %> | ||
</p> | ||
</li> |
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,8 @@ | ||
# frozen_string_literal: true | ||
|
||
class ProductComponent < ViewComponent::Base | ||
def initialize(product:, notice:) | ||
@product = product | ||
@notice = notice | ||
end | ||
end |
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 @@ | ||
<h3><%= @item.percent_off %>%</h3> |
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,9 @@ | ||
# frozen_string_literal: true | ||
|
||
class ProductCouponComponent < ViewComponent::Base | ||
with_collection_parameter :item | ||
|
||
def initialize(item:) | ||
@item = item | ||
end | ||
end |
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,4 @@ | ||
<h1>Products for sale</h1> | ||
<ul> | ||
<%= render(ProductComponent.with_collection(@products, notice: "Today only")) %> | ||
</ul> |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just dropping a note here to make sure we update this to reflect whatever API we land on ❤️