Skip to content

Commit

Permalink
Generate a view component in ingredient generator
Browse files Browse the repository at this point in the history
Alchemy is mostly using a ViewComponent to render an ingredient. Only some situations the view partial is used. If you previous used the generator you ran into the situation, that the required ingredient was missing. Now the generator is creating the ingredient view component and is referring to that component in the view.
  • Loading branch information
kulturbande committed Sep 29, 2024
1 parent 8d4c097 commit 6bb2595
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/generators/alchemy/ingredient/ingredient_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@ def init
@ingredients_view_path = "app/views/alchemy/ingredients"
end

def create_view_component
template "view_component.rb.tt", "app/components/alchemy/ingredients/#{file_name}_view.rb"
end

def create_model
template "model.rb.tt", "app/models/alchemy/ingredients/#{file_name}.rb"
end

def copy_templates
@ingredient_editor_local = "#{file_name}_editor"
@ingredient_view_local = "#{file_name}_view"
@ingredient_view_component_class = "#{@class_name}View"
template "view.html.erb", "#{@ingredients_view_path}/_#{file_name}_view.html.erb"
template "editor.html.erb", "#{@ingredients_view_path}/_#{file_name}_editor.html.erb"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/alchemy/ingredient/templates/view.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<%%= <%= @ingredient_view_local %>.value %>
<%%= render Alchemy::Ingredients::<%= @ingredient_view_component_class %>.new(<%= @ingredient_view_local %>) %>
10 changes: 10 additions & 0 deletions lib/generators/alchemy/ingredient/templates/view_component.rb.tt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Alchemy
module Ingredients
class <%= @class_name %>View < BaseView

def call
content_tag(:div, ingredient.value)
end
end
end
end

0 comments on commit 6bb2595

Please sign in to comment.