|
| 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 |
0 commit comments