Class: UltimateTurboModal::Generators::InstallGenerator
- Inherits:
-
Base
- Object
- Rails::Generators::Base
- Base
- UltimateTurboModal::Generators::InstallGenerator
- Defined in:
- lib/generators/ultimate_turbo_modal/install_generator.rb
Instance Method Summary collapse
-
#add_modal_turbo_frame ⇒ Object
Step 4: Add Turbo Frame to Layout.
- #copy_initializer_and_flavor ⇒ Object
-
#determine_framework_flavor ⇒ Object
Step 1: Determine CSS framework flavor.
-
#setup_javascript_dependencies ⇒ Object
Step 2: Setup Javascript Dependencies (Yarn/npm/Bun or Importmap).
-
#setup_stimulus_controller ⇒ Object
Step 3: Register Stimulus Controller.
- #show_readme ⇒ Object
Instance Method Details
#add_modal_turbo_frame ⇒ Object
Step 4: Add Turbo Frame to Layout
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 82 def add_modal_turbo_frame layout_path = rails_root_join("app", "views", "layouts", "application.html.erb") frame_tag = "<%= turbo_frame_tag \"modal\" %>\n" body_tag_regex = /<body.*>\s*\n?/ say "\nAttempting to add modal Turbo Frame to #{layout_path}...", :yellow unless File.exist?(layout_path) say "❌ Layout file not found at #{layout_path}.", :red say " Please manually add the following line inside the <body> tag of your main layout:", :yellow say " #{frame_tag.strip}\n", :cyan return end file_content = File.read(layout_path) if file_content.include?(frame_tag.strip) say "⏩ Turbo Frame tag already exists.", :blue elsif file_content.match?(body_tag_regex) # Insert after the opening body tag insert_into_file layout_path, " #{frame_tag}", after: body_tag_regex # Add indentation say "✅ Added Turbo Frame tag inside the <body>.", :green else say "❌ Could not find the opening <body> tag in #{layout_path}.", :red say " Please manually add the following line inside the <body> tag:", :yellow say " #{frame_tag.strip}\n", :cyan end end |
#copy_initializer_and_flavor ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 111 def copy_initializer_and_flavor say "\nCreating initializer for `#{@framework}` flavor...", :green copy_file "ultimate_turbo_modal.rb", "config/initializers/ultimate_turbo_modal.rb" gsub_file "config/initializers/ultimate_turbo_modal.rb", "FLAVOR", ":#{@framework}" say "✅ Initializer created at config/initializers/ultimate_turbo_modal.rb" say "Copying flavor file...", :green copy_file "flavors/#{@framework}.rb", "config/initializers/ultimate_turbo_modal_#{@framework}.rb" say "✅ Flavor file copied to config/initializers/ultimate_turbo_modal_#{@framework}.rb\n" end |
#determine_framework_flavor ⇒ Object
Step 1: Determine CSS framework flavor
16 17 18 19 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 16 def determine_framework_flavor @framework = [:flavor] || prompt_for_flavor validate_flavor!(@framework) end |
#setup_javascript_dependencies ⇒ Object
Step 2: Setup Javascript Dependencies (Yarn/npm/Bun or Importmap)
22 23 24 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 22 def setup_javascript_dependencies add_js_dependency end |
#setup_stimulus_controller ⇒ Object
Step 3: Register Stimulus Controller
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 27 def setup_stimulus_controller index_path = rails_root_join("app", "javascript", "controllers", "index.js") application_path = rails_root_join("app", "javascript", "controllers", "application.js") controller_name = "UltimateTurboModalController" stimulus_identifier = "modal" import_line = "import { #{controller_name} } from \"#{package_name}\"\n" register_line = "application.register(\"#{stimulus_identifier}\", #{controller_name})\n" # Determine which file contains Application.start() — it may be index.js or application.js target_path, file_content = find_stimulus_target(index_path, application_path) say "\nAttempting to register Stimulus controller...", :yellow unless target_path say "❌ Stimulus controllers file not found.", :red say " Please manually add the following lines to your Stimulus setup:", :yellow say " #{import_line.strip}", :cyan say " #{register_line.strip}\n", :cyan return end say " Target file: #{target_path}", :yellow # Insert the import statement after an existing import from @hotwired/stimulus or ./application import_anchor = /import .* from ["'](?:@hotwired\/stimulus|\.\/application)["']\n/ if file_content.include?(import_line) say "⏩ Import statement already exists.", :blue elsif file_content.match?(import_anchor) insert_into_file target_path, import_line, after: import_anchor say "✅ Added import statement.", :green elsif file_content.match?(/import/) insert_into_file target_path, import_line, before: /import/ say "✅ Added import statement (fallback position).", :green else prepend_to_file target_path, import_line say "✅ Added import statement (prepended to file).", :green end # Insert the register statement after Application.start() register_anchor = /Application\.start\(\)\n/ if file_content.include?(register_line) say "⏩ Controller registration already exists.", :blue elsif file_content.match?(register_anchor) insert_into_file target_path, register_line, after: register_anchor say "✅ Added controller registration.", :green else say "❌ Could not find `Application.start()` line in #{target_path}.", :red say " Please manually add these lines to your Stimulus setup:", :yellow say " #{import_line.strip}", :cyan say " #{register_line.strip}\n", :cyan end end |
#show_readme ⇒ Object
122 123 124 125 126 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 122 def show_readme say "\nUltimateTurboModal installation complete!\n", :magenta say "Please review the initializer files, ensure JS is set up, and check your layout file.", :magenta say "Don't forget to restart your Rails server!", :yellow end |