Skip to content

Commit 1c6fe98

Browse files
Merge pull request #2334 from NCCE/3006-strapi-component-generator
Strapi Component Generator
2 parents aaaf445 + aa13075 commit 1c6fe98

File tree

9 files changed

+230
-1
lines changed

9 files changed

+230
-1
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ group :test do
105105
gem "axe-matchers", ">= 2.5.0", require: false
106106
gem "capybara"
107107
gem "climate_control"
108+
gem "generator_spec"
108109
gem "guard-rspec", require: false
109110
gem "rails-controller-testing"
110111
gem "rspec-json_expectations"

Gemfile.lock

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,9 @@ GEM
276276
fog-core
277277
nokogiri (>= 1.5.11, < 2.0.0)
278278
formatador (1.1.0)
279+
generator_spec (0.10.0)
280+
activesupport (>= 3.0.0)
281+
railties (>= 3.0.0)
279282
geocoder (1.8.2)
280283
globalid (1.2.1)
281284
activesupport (>= 6.1)
@@ -777,6 +780,7 @@ DEPENDENCIES
777780
faker
778781
faraday
779782
fog-aws
783+
generator_spec
780784
geocoder (>= 1.6.6)
781785
graphlient
782786
graphql

config/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class Application < Rails::Application
1919
# Please, add to the `ignore` list any other `lib` subdirectories that do
2020
# not contain `.rb` files, or that should not be reloaded or eager loaded.
2121
# Common ones are `templates`, `generators`, or `middleware`, for example.
22-
config.autoload_lib(ignore: %w[assets tasks])
22+
config.autoload_lib(ignore: %w[assets tasks generators])
2323

2424
# Settings in config/environments/* take precedence over those specified here.
2525
# Application configuration can go into files in config/initializers
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
module Strapi
2+
class ComponentGenerator < Rails::Generators::Base
3+
source_root File.expand_path("component_templates", __dir__)
4+
5+
argument :component_name, type: :string, required: true
6+
argument :component_type, type: :string, required: true
7+
class_option :strapi_params, type: :array, default: []
8+
9+
COMPONENT_TYPES = %w[blocks content_blocks buttons email_content]
10+
PROVIDER = "providers/strapi/"
11+
BASE_PATH = "app/services/cms/"
12+
STRAPI_PATH = "#{BASE_PATH}#{PROVIDER}"
13+
TEST_PATH = "spec/services/cms/#{PROVIDER}"
14+
15+
def setup_params
16+
raise StandardError unless COMPONENT_TYPES.include?(component_type)
17+
@component_name_class = component_name.classify
18+
@component_type_class = component_type.camelize
19+
@component_filename = component_name.underscore
20+
@component_type_filename = component_type.underscore
21+
@component_strapi_name = component_name.underscore.tr("_", "-")
22+
@strapi_params = options["strapi_params"]
23+
@rails_param_names = options["strapi_params"].map { _1.underscore }
24+
end
25+
26+
def create_query_file
27+
template("query_template.rb.tt", "#{STRAPI_PATH}queries/components/#{@component_type_filename}/#{@component_filename}.rb")
28+
end
29+
30+
def create_query_test
31+
template("query_test_template.rb.tt", "#{TEST_PATH}queries/components/#{@component_type_filename}/#{@component_filename}_spec.rb")
32+
end
33+
34+
def create_mock_file
35+
template("mock_template.rb.tt", "#{STRAPI_PATH}mocks/dynamic_components/#{@component_type_filename}/#{@component_filename}.rb")
36+
end
37+
38+
def create_data_file
39+
template("mapping_template.rb.tt", "#{BASE_PATH}dynamic_components/#{@component_type_filename}/#{@component_filename}.rb")
40+
end
41+
42+
def run_view_component_generator
43+
Rails::Generators.invoke(
44+
"component",
45+
["Cms::#{@component_name_class}", *@rails_param_names, "--test-framework=rspec", "--sidecar"].compact,
46+
behaviour: :invoke,
47+
destination_root:
48+
)
49+
rescue
50+
puts <<~HEREDOC
51+
#{"*" * 80}
52+
Unable to create component, please run this command seperatly
53+
54+
rails generate component Cms::#{@component_name_class} #{@rails_param_names.join(" ")} --test-framework=rspec
55+
#{"*" * 80}
56+
57+
HEREDOC
58+
end
59+
60+
def print_method_defintions
61+
puts <<~HEREDOC
62+
63+
#{"*" * 80}
64+
65+
Remember to add the mapping method to #{STRAPI_PATH}factories/#{@component_type_filename}_factory.rb
66+
67+
!! Code provided below, but may require modification depending on data types !!
68+
69+
#{factory_key}
70+
#{"-" * 80}
71+
72+
#{method_defintion}
73+
#{"*" * 80}
74+
75+
HEREDOC
76+
end
77+
78+
def factory_key
79+
<<~RUBY
80+
when "#{@component_strapi_name}":
81+
to_#{@component_filename}(strapi_data)
82+
RUBY
83+
end
84+
85+
def method_defintion
86+
<<~RUBY
87+
def to_#{@component_filename}(strapi_data)
88+
DynamicsComponents::Blocks::#{@component_name_class}.new(
89+
#{@strapi_params.map { "#{_1.underscore}: strapi_data[:#{_1}]" }.join(",\n\s\s\s\s")}
90+
)
91+
end
92+
RUBY
93+
end
94+
end
95+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Cms
2+
module DynamicComponents
3+
module <%= @component_type_class %>
4+
class <%= @component_name_class %>
5+
attr_accessor <%= @rails_param_names.map{":#{_1}"}.join(", ") %>
6+
7+
def initialize(<%= @rails_param_names.map{"#{_1}:"}.join(", ") %>)
8+
<%- @rails_param_names.each do |param| -%>
9+
<%= "@#{param} = #{param}" %>
10+
<%- end -%>
11+
end
12+
13+
def render
14+
Cms::<%= @component_name_class %>Component.new(<%= @rails_param_names.map{"#{_1}:"}.join(", ") %>)
15+
end
16+
end
17+
end
18+
end
19+
end
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Cms
2+
module Providers
3+
module Strapi
4+
module Mocks
5+
module DynamicComponents
6+
module <%= @component_type_class %>
7+
class <%= @component_name_class %> < StrapiMock
8+
strapi_component "blocks.<%= @component_strapi_name %>"
9+
10+
<%- @strapi_params.each do |param| -%>
11+
attribute(:<%= param %>) { }
12+
<%- end -%>
13+
end
14+
end
15+
end
16+
end
17+
end
18+
end
19+
end
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
module Cms
2+
module Providers
3+
module Strapi
4+
module Queries
5+
module Components
6+
module <%= @component_type_class %>
7+
class <%= @component_name_class %> < BaseComponentQuery
8+
def self.name = "Component<%= @component_name_class %>"
9+
10+
def self.base_fields
11+
<<~GRAPHQL.freeze
12+
<%- @strapi_params.each do |param| -%>
13+
<%= param %>
14+
<%- end -%>
15+
GRAPHQL
16+
end
17+
end
18+
end
19+
end
20+
end
21+
end
22+
end
23+
end
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Cms::Providers::Strapi::Queries::Components::<%= @component_type_class %>::<%= @component_name_class %> do
4+
it_should_behave_like "a strapi graphql component",
5+
%w[
6+
<%- @strapi_params.each do |param| -%>
7+
<%= param %>
8+
<%- end -%>
9+
]
10+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require "rails_helper"
2+
require "generator_spec"
3+
require "generators/strapi/component_generator"
4+
5+
RSpec.describe Strapi::ComponentGenerator, type: :generator do
6+
let(:tmp_dir) { Rails.root.join("tmp/gen_test") }
7+
tests Strapi::ComponentGenerator
8+
destination Rails.root.join("tmp/gen_test")
9+
10+
before do
11+
FileUtils.rm_rf(tmp_dir)
12+
prepare_destination
13+
end
14+
15+
after do
16+
FileUtils.rm_rf(tmp_dir)
17+
end
18+
19+
context "with all arguments" do
20+
before do
21+
run_generator %w[gen_test blocks --strapi-params=title textContent]
22+
end
23+
24+
it "creates the query file" do
25+
expect(File).to exist(file_path("app/services/cms/providers/strapi/queries/components/blocks/gen_test.rb"))
26+
end
27+
28+
it "creates the query test file" do
29+
expect(File).to exist(file_path("spec/services/cms/providers/strapi/queries/components/blocks/gen_test_spec.rb"))
30+
end
31+
32+
it "creates data file" do
33+
expect(File).to exist(file_path("app/services/cms/dynamic_components/blocks/gen_test.rb"))
34+
end
35+
36+
it "creates mock file" do
37+
expect(File).to exist(file_path("app/services/cms/providers/strapi/mocks/dynamic_components/blocks/gen_test.rb"))
38+
end
39+
40+
it "creates component file" do
41+
expect(File).to exist(file_path("app/components/cms/gen_test_component.rb"))
42+
expect(File).to exist(file_path("app/components/cms/gen_test_component/gen_test_component.html.erb"))
43+
expect(File).to exist(file_path("spec/components/cms/gen_test_component_spec.rb"))
44+
end
45+
46+
def file_path(file)
47+
"tmp/gen_test/#{file}"
48+
end
49+
end
50+
51+
context "with invalid type" do
52+
it "creates the query file" do
53+
expect {
54+
run_generator %w[gen_test not-a-type --strapi-params=title textContent]
55+
}.to raise_error(StandardError)
56+
end
57+
end
58+
end

0 commit comments

Comments
 (0)