-
I've been wondering how to create ViewComponent-specific POROs and autoload it (without namespaces if possible). I found the following component for RailsDev.com: # https://github.com/joemasilotti/railsdevs.com/blob/main/app/components/nav_bar/base_component.rb
module NavBar
class BaseComponent < ApplicationComponent
private attr_reader :user
def initialize(user)
@user = user
end
def component
if user.present?
NavBar::UserComponent.new(user, links:)
else
NavBar::GuestComponent.new(links:)
end
end
private
def links
links = []
links << Link.new(t(".home"), root_path) if helpers.turbo_native_app?
links << Link.new(t(".developers"), developers_path)
links << Link.new(t(".pricing"), pricing_path)
links << Link.new(t(".about"), about_path)
links
end
end
end I got a bit amazed with the fact that the # https://github.com/joemasilotti/railsdevs.com/blob/main/app/models/link.rb
Link = Struct.new(:title, :path) I tried moving the module NavBar
class BaseComponent < ApplicationComponent
private attr_reader :user
def initialize(user)
@user = user
end
def component
if user.present?
NavBar::UserComponent.new(user, links:)
else
NavBar::GuestComponent.new(links:)
end
end
private
def links
links = []
links << Link.new(t(".home"), root_path) if helpers.turbo_native_app?
links << Link.new(t(".developers"), developers_path)
links << Link.new(t(".pricing"), pricing_path)
links << Link.new(t(".about"), about_path)
links
end
end
class Link # not working
Link = Struct.new(:title, :path)
end
end Does it mean that we have no way to put and autoload POROs under app/components? |
Beta Was this translation helpful? Give feedback.
Answered by
hachi8833
Mar 7, 2023
Replies: 1 comment 2 replies
-
I got the solution: https://viewcomponent.org/guide/slots.html#rendering-collections |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
hachi8833
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I got the solution: https://viewcomponent.org/guide/slots.html#rendering-collections