Class: Pu::Invites::InstallGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- Pu::Invites::InstallGenerator
- Includes:
- ActiveRecord::Generators::Migration, PlutoniumGenerators::Concerns::RodauthRedirects, PlutoniumGenerators::Generator
- Defined in:
- lib/generators/pu/invites/install_generator.rb
Instance Method Summary collapse
- #add_entity_action ⇒ Object
- #add_entity_association ⇒ Object
- #add_entity_policy ⇒ Object
- #add_resource_policy_helper ⇒ Object
- #add_routes ⇒ Object
- #add_user_action ⇒ Object
- #add_user_policy ⇒ Object
- #add_welcome_invite_class ⇒ Object
- #configure_rodauth ⇒ Object
- #create_controllers ⇒ Object
- #create_definition ⇒ Object
- #create_entity_interaction ⇒ Object
- #create_interactions ⇒ Object
- #create_mailer ⇒ Object
- #create_model ⇒ Object
- #create_package ⇒ Object
- #create_policy ⇒ Object
- #create_user_interaction ⇒ Object
- #create_user_invites_migration ⇒ Object
- #create_views ⇒ Object
- #integrate_with_welcome_controller ⇒ Object
- #show_instructions ⇒ Object
- #validate_requirements ⇒ Object
- #verify_active_record_encryption ⇒ Object
Methods included from PlutoniumGenerators::Concerns::RodauthRedirects
Methods included from PlutoniumGenerators::Generator
derive_association_name, find_shared_namespace, included
Methods included from PlutoniumGenerators::Concerns::Logger
#debug, #error, #exception, #info, #success, #warn
Methods included from PlutoniumGenerators::Concerns::Config
Instance Method Details
#add_entity_action ⇒ Object
189 190 191 192 193 |
# File 'lib/generators/pu/invites/install_generator.rb', line 189 def add_entity_action inject_into_file entity_definition_path, " action :invite_user, interaction: #{entity_model}::InviteUserInteraction, category: :secondary\n", after: /class #{entity_model}Definition < .+\n/ end |
#add_entity_association ⇒ Object
173 174 175 176 177 |
# File 'lib/generators/pu/invites/install_generator.rb', line 173 def add_entity_association inject_into_file entity_model_path, " has_many :#{invite_table}, class_name: \"Invites::#{invite_model}\", dependent: :destroy\n", before: /^\s*# add has_many associations above\.\n/ end |
#add_entity_policy ⇒ Object
195 196 197 198 199 |
# File 'lib/generators/pu/invites/install_generator.rb', line 195 def add_entity_policy inject_into_file entity_policy_path, "def invite_user?\n current_membership&.owner?\n end\n\n ", before: "# Core attributes" end |
#add_resource_policy_helper ⇒ Object
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/generators/pu/invites/install_generator.rb', line 218 def add_resource_policy_helper resource_policy_path = "app/policies/resource_policy.rb" return unless File.exist?(Rails.root.join(resource_policy_path)) file_content = File.read(Rails.root.join(resource_policy_path)) helper_code = " def current_membership\n return unless entity_scope && user\n\n @current_membership ||= \#{membership_model}.find_by(\#{entity_association_name}: entity_scope, \#{user_association_name}: user)\n end\n RUBY\n\n if file_content.include?(\"private\\n\")\n inject_into_file resource_policy_path,\n \"\\n\#{helper_code}\",\n after: \"private\\n\"\n else\n inject_into_file resource_policy_path,\n \"\\n private\\n\\n\#{helper_code}\",\n before: /^end\\s*\\z/\n end\nend\n" |
#add_routes ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/generators/pu/invites/install_generator.rb', line 244 def add_routes routes_content = File.read(Rails.root.join("config/routes.rb")) flow_marker = "# Invitation routes for #{invite_model}" if routes_content.include?(flow_marker) say_status :skip, "Invitation routes for #{invite_model} already present", :yellow else welcome_present = routes_content.include?("# Invitation welcome routes") welcome_block = if welcome_present "" else "\n # Invitation welcome routes (shared across all invite flows)\n scope module: :invites do\n get \"invitations/welcome\", to: \"welcome#index\", as: :invites_welcome_check\n delete \"invitations/welcome\", to: \"welcome#skip\", as: :invites_welcome_skip\n end\n RUBY\n end\n\n flow_block = <<-RUBY\n\n \#{flow_marker}\n scope module: :invites do\n get \"\#{invitations_path}/:token\", to: \"\#{invitations_path}#show\", as: :\#{invite_route_prefix}_invitation\n post \"\#{invitations_path}/:token/accept\", to: \"\#{invitations_path}#accept\", as: :accept_\#{invite_route_prefix}_invitation\n get \"\#{invitations_path}/:token/signup\", to: \"\#{invitations_path}#signup\", as: :\#{invite_route_prefix}_invitation_signup\n post \"\#{invitations_path}/:token/signup\", to: \"\#{invitations_path}#signup\"\n end\n RUBY\n\n inject_into_file \"config/routes.rb\",\n welcome_block + flow_block,\n before: /^end\\s*\\z/\n end\n\n # If no main WelcomeController exists, add /welcome route pointing to\n # Invites::WelcomeController so Rodauth's login_redirect \"/welcome\" works.\n routes_content = File.read(Rails.root.join(\"config/routes.rb\"))\n unless File.exist?(Rails.root.join(\"app/controllers/welcome_controller.rb\")) ||\n routes_content.include?(%(get \"welcome\", to: \"invites/welcome#index\"))\n welcome_route = <<-RUBY\n\n # Welcome route (handled by invites package \u2014 replace with pu:saas:welcome for full onboarding)\n get \"welcome\", to: \"invites/welcome#index\"\n RUBY\n\n inject_into_file \"config/routes.rb\",\n welcome_route,\n before: /^\\s*# Invitation welcome routes/\n end\nend\n" |
#add_user_action ⇒ Object
206 207 208 209 210 |
# File 'lib/generators/pu/invites/install_generator.rb', line 206 def add_user_action inject_into_file user_definition_path, " action :invite_user, interaction: #{user_model}::InviteUserInteraction, category: :primary\n", after: /class #{user_model}Definition < .+\n/ end |
#add_user_policy ⇒ Object
212 213 214 215 216 |
# File 'lib/generators/pu/invites/install_generator.rb', line 212 def add_user_policy inject_into_file user_policy_path, "def invite_user?\n current_membership&.owner?\n end\n\n ", before: "# Core attributes" end |
#add_welcome_invite_class ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/generators/pu/invites/install_generator.rb', line 110 def add_welcome_invite_class welcome_path = "packages/invites/app/controllers/invites/welcome_controller.rb" return unless File.exist?(Rails.root.join(welcome_path)) content = File.read(Rails.root.join(welcome_path)) new_class = "::Invites::#{invite_model}" # Already present? bail. Use a non-word lookahead so we don't match # `::Invites::FunderInvite` when looking for `::Invites::Funder`. return if content =~ /#{Regexp.escape(new_class)}(?!\w)/ # Find `def invite_classes` block; inject before the closing `]`. injection = content.sub(/(\bdef invite_classes\b.*?\[)([^\]]*)(\])/m) do before, list, after = Regexp.last_match[1], Regexp.last_match[2], Regexp.last_match[3] existing = list.strip new_list = existing.empty? ? new_class : "#{existing.chomp(",").strip}, #{new_class}" "#{before}#{new_list}#{after}" end if injection != content File.write(Rails.root.join(welcome_path), injection) say_status :inject, "Added #{new_class} to welcome controller's invite_classes", :green end end |
#configure_rodauth ⇒ Object
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/generators/pu/invites/install_generator.rb', line 299 def configure_rodauth return unless rodauth? rodauth_file = "app/rodauth/#{rodauth_config}_rodauth_plugin.rb" unless File.exist?(Rails.root.join(rodauth_file)) say_status :skip, "Rodauth plugin not found: #{rodauth_file}", :yellow return end file_content = File.read(Rails.root.join(rodauth_file)) # Check if already configured if file_content.include?("after_welcome_redirect") say_status :skip, "Rodauth already configured for invites", :yellow return end # Check for existing after_login block (non-commented) # Single-line: after_login { remember_login } single_line_match = file_content.match(/^(\s*)after_login\s*\{\s*(.+?)\s*\}/) # Multi-line: after_login do ... end multi_line_match = file_content.match(/^(\s*)after_login\s+do\s*\n(.*?)\n(\s*)end/m) if single_line_match # Convert single-line block to multi-line with our code added indent = single_line_match[1] existing_code = single_line_match[2] original_line = single_line_match[0] new_block = " \#{indent}after_login do\n \#{indent} \#{existing_code}\n \#{indent} session[:after_welcome_redirect] = session.delete(login_redirect_session_key)\n \#{indent}end\n RUBY\n\n gsub_file rodauth_file, original_line, new_block\n say_status :info, \"Added session redirect to existing after_login block\", :green\n elsif multi_line_match\n # Multi-line block - add our line before the end\n indent = multi_line_match[1]\n block_content = multi_line_match[2]\n end_indent = multi_line_match[3]\n original_block = multi_line_match[0]\n\n new_block = <<~RUBY.chomp\n \#{indent}after_login do\n \#{block_content}\n \#{indent} session[:after_welcome_redirect] = session.delete(login_redirect_session_key)\n \#{end_indent}end\n RUBY\n\n gsub_file rodauth_file, original_block, new_block\n say_status :info, \"Added session redirect to existing after_login block\", :green\n else\n # Add new after_login block\n after_login_code = <<-RUBY\n\n # ==> User Invites - Move captured path to session for WelcomeController\n after_login do\nsession[:after_welcome_redirect] = session.delete(login_redirect_session_key)\n end\n RUBY\n\n inject_into_file rodauth_file,\n after_login_code,\n before: /^ end\\s*\\n/\n\n say_status :info, \"Added after_login block for invites\", :green\n end\n\n # Add other config options if not present\n unless file_content.include?(\"login_return_to_requested_location?\")\n inject_into_file rodauth_file,\n \"\\n # Enable path capture so Rodauth stores the originally requested URL\\n login_return_to_requested_location? true\\n\",\n after: /login_redirect.*\\n/\n end\n\n # Update login_redirect and create_account_redirect to /welcome\n update_rodauth_redirects(rodauth_file)\nend\n".chomp |
#create_controllers ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/generators/pu/invites/install_generator.rb', line 98 def create_controllers template "packages/invites/app/controllers/invites/user_invitations_controller.rb", "packages/invites/app/controllers/invites/#{invitations_path}_controller.rb" # Welcome controller is a one-shot — only generate if it doesn't exist yet. welcome_path = "packages/invites/app/controllers/invites/welcome_controller.rb" unless File.exist?(Rails.root.join(welcome_path)) template "packages/invites/app/controllers/invites/welcome_controller.rb", welcome_path end end |
#create_definition ⇒ Object
163 164 165 166 |
# File 'lib/generators/pu/invites/install_generator.rb', line 163 def create_definition template "packages/invites/app/definitions/invites/user_invite_definition.rb", "packages/invites/app/definitions/invites/#{invite_underscore}_definition.rb" end |
#create_entity_interaction ⇒ Object
179 180 181 182 183 184 185 186 187 |
# File 'lib/generators/pu/invites/install_generator.rb', line 179 def create_entity_interaction dest_path = if entity_in_package? "packages/#{entity_package}/app/interactions/#{entity_table}/invite_user_interaction.rb" else "app/interactions/#{entity_table}/invite_user_interaction.rb" end template "app/interactions/invite_user_interaction.rb", dest_path end |
#create_interactions ⇒ Object
155 156 157 158 159 160 161 |
# File 'lib/generators/pu/invites/install_generator.rb', line 155 def create_interactions template "packages/invites/app/interactions/invites/resend_invite_interaction.rb", "packages/invites/app/interactions/invites/resend_invite_interaction.rb" template "packages/invites/app/interactions/invites/cancel_invite_interaction.rb", "packages/invites/app/interactions/invites/cancel_invite_interaction.rb" end |
#create_mailer ⇒ Object
87 88 89 90 91 92 93 94 95 96 |
# File 'lib/generators/pu/invites/install_generator.rb', line 87 def create_mailer template "packages/invites/app/mailers/invites/user_invite_mailer.rb", "packages/invites/app/mailers/invites/#{invite_underscore}_mailer.rb" template "packages/invites/app/views/invites/user_invite_mailer/invitation.html.erb", "packages/invites/app/views/invites/#{invite_underscore}_mailer/invitation.html.erb" template "packages/invites/app/views/invites/user_invite_mailer/invitation.text.erb", "packages/invites/app/views/invites/#{invite_underscore}_mailer/invitation.text.erb" end |
#create_model ⇒ Object
82 83 84 85 |
# File 'lib/generators/pu/invites/install_generator.rb', line 82 def create_model template "packages/invites/app/models/invites/user_invite.rb", "packages/invites/app/models/invites/#{invite_underscore}.rb" end |
#create_package ⇒ Object
72 73 74 75 |
# File 'lib/generators/pu/invites/install_generator.rb', line 72 def create_package return if File.exist?(File.join(destination_root, "packages/invites")) generate "pu:pkg:package", "invites" end |
#create_policy ⇒ Object
168 169 170 171 |
# File 'lib/generators/pu/invites/install_generator.rb', line 168 def create_policy template "packages/invites/app/policies/invites/user_invite_policy.rb", "packages/invites/app/policies/invites/#{invite_underscore}_policy.rb" end |
#create_user_interaction ⇒ Object
201 202 203 204 |
# File 'lib/generators/pu/invites/install_generator.rb', line 201 def create_user_interaction template "app/interactions/user_invite_user_interaction.rb", "app/interactions/#{user_table}/invite_user_interaction.rb" end |
#create_user_invites_migration ⇒ Object
77 78 79 80 |
# File 'lib/generators/pu/invites/install_generator.rb', line 77 def create_user_invites_migration migration_template "db/migrate/create_user_invites.rb", "db/migrate/create_#{invite_table}.rb" end |
#create_views ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/generators/pu/invites/install_generator.rb', line 135 def create_views %w[landing show signup error].each do |view| template "packages/invites/app/views/invites/user_invitations/#{view}.html.erb", "packages/invites/app/views/invites/#{invitations_path}/#{view}.html.erb" end # Welcome view is a one-shot too. pending_path = "packages/invites/app/views/invites/welcome/pending_invitation.html.erb" unless File.exist?(Rails.root.join(pending_path)) template "packages/invites/app/views/invites/welcome/pending_invitation.html.erb", pending_path end layout_path = "packages/invites/app/views/layouts/invites/invitation.html.erb" unless File.exist?(Rails.root.join(layout_path)) template "packages/invites/app/views/layouts/invites/invitation.html.erb", layout_path end end |
#integrate_with_welcome_controller ⇒ Object
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 |
# File 'lib/generators/pu/invites/install_generator.rb', line 382 def integrate_with_welcome_controller welcome_controller_path = Rails.root.join("app/controllers/welcome_controller.rb") return unless File.exist?(welcome_controller_path) file_content = File.read(welcome_controller_path) return if file_content.include?("PendingInviteCheck") relative_path = "app/controllers/welcome_controller.rb" # Add PendingInviteCheck concern and invites view path inject_into_file relative_path, " include Plutonium::Invites::PendingInviteCheck\n\n prepend_view_path Invites::Engine.root.join(\"app/views\")\n", after: /class WelcomeController.*\n/ # Add invite check as first step in index inject_into_file relative_path, " return redirect_to(invites_welcome_check_path) if pending_invite\n\n", after: /def index\n/ # Add invite_classes method if neither it nor invite_class is present if file_content !~ /def invite_classes\b/ && file_content !~ /def invite_class\b/ inject_into_file relative_path, "\n def invite_classes\n [::Invites::#{invite_model}]\n end\n", before: /^end\s*\z/ else # Inject this invite_model into the existing invite_classes array if missing. host_content = File.read(Rails.root.join(relative_path)) if host_content =~ /def invite_classes\b/ && host_content !~ /::Invites::#{invite_model}\b/ updated = host_content.sub(/(\bdef invite_classes\b.*?\[)([^\]]*)(\])/m) do before, list, after = Regexp.last_match[1], Regexp.last_match[2], Regexp.last_match[3] existing = list.strip new_list = existing.empty? ? "::Invites::#{invite_model}" : "#{existing.chomp(",").strip}, ::Invites::#{invite_model}" "#{before}#{new_list}#{after}" end File.write(Rails.root.join(relative_path), updated) end end # Update Invites::WelcomeController to redirect to /welcome (the main hub) # instead of / (the app root) invites_welcome_path = "packages/invites/app/controllers/invites/welcome_controller.rb" if File.exist?(Rails.root.join(invites_welcome_path)) gsub_file invites_welcome_path, /def default_redirect_path\n\s*"\/"\n\s*end/, "def default_redirect_path\n \"/welcome\"\n end" end say_status :info, "Integrated invite check into WelcomeController", :green end |
#show_instructions ⇒ Object
454 455 456 |
# File 'lib/generators/pu/invites/install_generator.rb', line 454 def show_instructions readme "INSTRUCTIONS" if behavior == :invoke end |
#validate_requirements ⇒ Object
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 |
# File 'lib/generators/pu/invites/install_generator.rb', line 39 def validate_requirements errors = [] unless File.exist?(Rails.root.join(entity_model_path)) errors << "Entity model not found: #{entity_model_path}" end unless File.exist?(Rails.root.join(entity_definition_path)) errors << "Entity definition not found: #{entity_definition_path}" end unless File.exist?(Rails.root.join(entity_policy_path)) errors << "Entity policy not found: #{entity_policy_path}" end unless File.exist?(Rails.root.join(user_definition_path)) errors << "User definition not found: #{user_definition_path}" end unless File.exist?(Rails.root.join(user_policy_path)) errors << "User policy not found: #{user_policy_path}" end unless File.exist?(membership_model_file) errors << "Membership model not found: #{membership_model_file.relative_path_from(Rails.root)}" end if errors.any? errors.each { |e| say_status :error, e, :red } raise Thor::Error, "Required files missing:\n - #{errors.join("\n - ")}" end end |
#verify_active_record_encryption ⇒ Object
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/generators/pu/invites/install_generator.rb', line 432 def verify_active_record_encryption return if behavior == :revoke return if active_record_encryption_configured? say_status :warning, "ActiveRecord encryption keys are not configured", :yellow say "\n Invite tokens use `encrypts :token, deterministic: true`. Without keys,\n creating or accepting an invite raises\n ActiveRecord::Encryption::Errors::Configuration.\n\n Generate keys and add them to credentials with:\n\n bin/rails db:encryption:init\n\n Then paste the printed `active_record_encryption:` block into\n config/credentials.yml.enc (or set the equivalent ENV vars in\n production).\n\n MSG\nend\n".indent(2) |