Class: UltimateTurboModal::Generators::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::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
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 117 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
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 147 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
14 15 16 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 14 def determine_framework_flavor @framework = prompt_for_flavor end |
#setup_javascript_dependencies ⇒ Object
Step 2: Setup Javascript Dependencies (Yarn/npm/Bun or Importmap)
19 20 21 22 23 24 25 26 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 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 19 def setup_javascript_dependencies package_name = "ultimate_turbo_modal" # Or the actual npm package name if different say "\nAttempting to set up JavaScript dependencies...", :yellow if uses_importmaps? say "Detected Importmaps. Pinning #{package_name}...", :green run "bin/importmap pin #{package_name}" say "\n✅ Pinned '#{package_name}' via importmap.", :green elsif uses_javascript_bundler? say "Detected jsbundling-rails (Yarn/npm/Bun). Adding #{package_name} package...", :green if uses_yarn? run "yarn add #{package_name}" say "\n✅ Added '#{package_name}' using Yarn.", :green elsif uses_npm? run "npm install --save #{package_name}" say "\n✅ Added '#{package_name}' using npm.", :green elsif uses_bun? run "bun add #{package_name}" say "\n✅ Added '#{package_name}' using Bun.", :green else # Default or fallback: Try yarn, but provide instructions for others say "Attempting to add with Yarn. If you use npm or Bun, please add manually.", :yellow run "yarn add #{package_name}" say "\n✅ Attempted to add '#{package_name}' using Yarn.", :green say " If this failed or you use npm/bun, please run:", :yellow say " npm install --save #{package_name}", :cyan say " # or", :cyan say " bun add #{package_name}\n", :cyan end else # Fallback instructions if neither is clearly detected say "\nCould not automatically detect Importmaps or jsbundling-rails.", :yellow say "Please manually add the '#{package_name}' JavaScript package.", :yellow say "If using Importmaps: bin/importmap pin #{package_name}", :cyan say "If using Yarn: yarn add #{package_name}", :cyan say "If using npm: npm install --save #{package_name}", :cyan say "If using Bun: bun add #{package_name}", :cyan say "Then, import it in your app/javascript/application.js:", :yellow say "import '#{package_name}'\n", :cyan end end |
#setup_stimulus_controller ⇒ Object
Step 3: Register Stimulus Controller
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 110 111 112 113 114 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 65 def setup_stimulus_controller stimulus_controller_path = rails_root_join("app", "javascript", "controllers", "index.js") controller_package = "ultimate_turbo_modal" # Package name where the controller is defined controller_name = "UltimateTurboModalController" # The exported controller class name stimulus_identifier = "modal" # The identifier for application.register import_line = "import { #{controller_name} } from \"#{controller_package}\"\n" register_line = "application.register(\"#{stimulus_identifier}\", #{controller_name})\n" say "\nAttempting to register Stimulus controller in #{stimulus_controller_path}...", :yellow unless File.exist?(stimulus_controller_path) say "❌ Stimulus controllers index file not found at #{stimulus_controller_path}.", :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 # Exit this method if the file doesn't exist end # Read the file content to check if lines already exist file_content = File.read(stimulus_controller_path) # Insert the import statement after the last existing import or a common marker # Using a regex to find the Stimulus import is often reliable import_anchor = /import .* from "@hotwired\/stimulus"\n/ if file_content.match?(import_anchor) && !file_content.include?(import_line) insert_into_file stimulus_controller_path, import_line, after: import_anchor say "✅ Added import statement.", :green elsif !file_content.include?(import_line) # Fallback: insert at the beginning if Stimulus import wasn't found (less ideal) insert_into_file stimulus_controller_path, import_line, before: /import/ say "✅ Added import statement (fallback position).", :green else say "⏩ Import statement already exists.", :blue end # Insert the register statement after Application.start() register_anchor = /Application\.start$$$$\n/ if file_content.match?(register_anchor) && !file_content.include?(register_line) insert_into_file stimulus_controller_path, register_line, after: register_anchor say "✅ Added controller registration.", :green elsif !file_content.include?(register_line) say "❌ Could not find `Application.start()` line to insert registration after.", :red say " Please manually add this line after your Stimulus application starts:", :yellow say " #{register_line.strip}\n", :cyan else say "⏩ Controller registration already exists.", :blue end end |
#show_readme ⇒ Object
158 159 160 161 162 |
# File 'lib/generators/ultimate_turbo_modal/install_generator.rb', line 158 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 |