Created
November 29, 2024 20:07
-
-
Save vagnerzampieri/3ead705ff824a8a60dd8f3b122ed7482 to your computer and use it in GitHub Desktop.
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
| =begin | |
| - caso não tenha produto, renderizamos um skeleton | |
| - caso tenha produto, renderizamos os produtos buscados | |
| - seria interessante que um componente tenha a responsabilidade de renderizar um skeleton ou os produtos buscados | |
| <% if products.present? %> | |
| <% products.each do |product| %> | |
| <div class="grid-col-xs-12 grid-col-sm-6"> | |
| <%= render ProductComponent.new(product:) %> | |
| </div> | |
| <% end %> | |
| <% else %> | |
| <%= render ProductSkeletonComponent.new %> | |
| <% end %> | |
| =end | |
| # essa ideia não funcionou | |
| # module ConditionalRendering | |
| # extend ActiveSupport::Concern | |
| # def render_alternative(&block) | |
| # if render? | |
| # super() | |
| # else | |
| # capture(&block) | |
| # end | |
| # end | |
| # end | |
| class ProductSkeletonComponent < ViewComponent::Base | |
| erb_template <<-ERB | |
| <div class="grid-col-xs-12 grid-col-sm-6"> | |
| <div class="product-skeleton"></div> | |
| </div> | |
| ERB | |
| end | |
| class ProductComponent < ViewComponent::Base | |
| erb_template <<-ERB | |
| <li> | |
| <%= @product.name %> | |
| </li> | |
| ERB | |
| def initialize(product:) | |
| @product = product | |
| end | |
| def render? | |
| @product.present? | |
| end | |
| end | |
| ApplicationController.new.view_context.render(ProductComponent.with_collection([Product.last])) | |
| # funciona mas não gostei da solução | |
| ApplicationController.new.view_context.render(ProductComponent.with_collection([])).presence || | |
| ApplicationController.new.view_context.render(ProductSkeletonComponent.new) | |
| ApplicationController.new.view_context.render(ProductComponent.with_collection([])) | |
| # ideal | |
| class ProductComponent < ViewComponent::Base | |
| render_alternative ProductSkeletonComponent.new | |
| erb_template <<-ERB | |
| <li> | |
| <%= @product.name %> | |
| </li> | |
| ERB | |
| def initialize(product:) | |
| @product = product | |
| end | |
| def render? | |
| @product.present? | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment