I have some objects in my DB that I like to render with a dedicated Hyperstack view Component.
Lets say my objects have a unique name
property from A to J. Now I would like to loop through them with each
and render a ComponentA
, ComponentB
, ComponentC
, ... depending on the name
of my object, and pass my object as a param to the component.
What I do now is:
DB_Objects.each do |object|
if object.name == 'A'
ComponentA(data: object)
elsif object.name == 'B'
ComponentB(data: object)
elsif object.name == 'C'
ComponentC(data: object)
elsif ...
What I want to do is something like:
DB_Objects.each do |object|
('Component' + object.name).constantize(data: object)
end
This is pseudo code as you can't give variables to constantize. But it shows what I would like to do.
How can I prevent a manual mapping of a object to its view.