Skip to content

Commit 6d20e39

Browse files
committed
Update ingredient generator to create editor components
The generator now creates ViewComponent-based editor components instead of deprecated editor partials.
1 parent 0f81d3c commit 6d20e39

4 files changed

Lines changed: 94 additions & 22 deletions

File tree

lib/generators/alchemy/ingredient/ingredient_generator.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@ class IngredientGenerator < ::Rails::Generators::Base
1111

1212
def init
1313
@class_name = class_name.classify
14-
@ingredients_view_path = "app/views/alchemy/ingredients"
15-
end
16-
17-
def create_view_component
18-
template "view_component.rb.tt", "app/components/alchemy/ingredients/#{file_name}_view.rb"
1914
end
2015

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

25-
def copy_templates
26-
@ingredient_editor_local = "#{file_name}_editor"
27-
template "editor.html.erb", "#{@ingredients_view_path}/_#{file_name}_editor.html.erb"
20+
def create_view_component
21+
template "view_component.rb.tt", "app/components/alchemy/ingredients/#{file_name}_view.rb"
22+
end
23+
24+
def create_editor_component
25+
template "editor_component.rb.tt", "app/components/alchemy/ingredients/#{file_name}_editor.rb"
2826
end
2927

3028
def show_todo

lib/generators/alchemy/ingredient/templates/editor.html.erb

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
module Alchemy
4+
module Ingredients
5+
class <%= @class_name %>Editor < BaseEditor
6+
# Override the input_field method to customize the editor input.
7+
# The base implementation renders a text field.
8+
#
9+
# def input_field(form)
10+
# tag.div(class: "input-field") do
11+
# form.text_field(:value,
12+
# id: form_field_id,
13+
# minlength: length_validation&.fetch(:minimum, nil),
14+
# maxlength: length_validation&.fetch(:maximum, nil),
15+
# required: presence_validation?,
16+
# pattern: format_validation)
17+
# end
18+
# end
19+
end
20+
end
21+
end
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# frozen_string_literal: true
2+
3+
require "rails_helper"
4+
require "rails/generators"
5+
require "generators/alchemy/ingredient/ingredient_generator"
6+
7+
RSpec.describe Alchemy::Generators::IngredientGenerator do
8+
let(:destination) { Rails.root.join("tmp/generators") }
9+
10+
before do
11+
FileUtils.rm_rf(destination)
12+
FileUtils.mkdir_p(destination)
13+
end
14+
15+
after do
16+
FileUtils.rm_rf(destination)
17+
end
18+
19+
def run_generator(args = ["Color"])
20+
described_class.start(args, destination_root: destination, quiet: true)
21+
end
22+
23+
describe "generating a custom ingredient" do
24+
before { run_generator(["Foo"]) }
25+
26+
it "creates the model" do
27+
model_path = destination.join("app/models/alchemy/ingredients/foo.rb")
28+
expect(File.exist?(model_path)).to be true
29+
content = File.read(model_path)
30+
expect(content).to include("class Foo < Alchemy::Ingredient")
31+
end
32+
33+
it "creates the view component" do
34+
view_path = destination.join("app/components/alchemy/ingredients/foo_view.rb")
35+
expect(File.exist?(view_path)).to be true
36+
content = File.read(view_path)
37+
expect(content).to include("class FooView < BaseView")
38+
end
39+
40+
it "creates the editor component" do
41+
editor_path = destination.join("app/components/alchemy/ingredients/foo_editor.rb")
42+
expect(File.exist?(editor_path)).to be true
43+
content = File.read(editor_path)
44+
expect(content).to include("class FooEditor < BaseEditor")
45+
end
46+
47+
it "does not create an editor partial" do
48+
partial_path = destination.join("app/views/alchemy/ingredients/_foo_editor.html.erb")
49+
expect(File.exist?(partial_path)).to be false
50+
end
51+
end
52+
53+
describe "with underscored class name" do
54+
before { run_generator(["foo_bar"]) }
55+
56+
it "creates properly named files" do
57+
expect(File.exist?(destination.join("app/models/alchemy/ingredients/foo_bar.rb"))).to be true
58+
expect(File.exist?(destination.join("app/components/alchemy/ingredients/foo_bar_view.rb"))).to be true
59+
expect(File.exist?(destination.join("app/components/alchemy/ingredients/foo_bar_editor.rb"))).to be true
60+
end
61+
62+
it "uses classified name in class definitions" do
63+
model_content = File.read(destination.join("app/models/alchemy/ingredients/foo_bar.rb"))
64+
expect(model_content).to include("class FooBar < Alchemy::Ingredient")
65+
end
66+
end
67+
end

0 commit comments

Comments
 (0)