Module: PlutoniumGenerators::Concerns::RodauthRedirects

Included in:
Pu::Invites::InstallGenerator, Pu::Saas::WelcomeGenerator
Defined in:
lib/generators/pu/lib/plutonium_generators/concerns/rodauth_redirects.rb

Overview

Shared logic for updating Rodauth redirect configuration. Used by generators that need to point login/create_account redirects to /welcome.

Instance Method Summary collapse

Instance Method Details

#update_rodauth_redirects(rodauth_file, redirect_path: "/welcome") ⇒ Object

Updates login_redirect and create_account_redirect in a Rodauth plugin file to point to the given path (typically “/welcome”).

Parameters:

  • rodauth_file (String)

    relative path to the rodauth plugin file

  • redirect_path (String) (defaults to: "/welcome")

    the path to redirect to (default: “/welcome”)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/pu/lib/plutonium_generators/concerns/rodauth_redirects.rb', line 13

def update_rodauth_redirects(rodauth_file, redirect_path: "/welcome")
  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))

  # Update login_redirect
  if file_content.match?(/login_redirect\s+["']\/["']/)
    gsub_file rodauth_file,
      /login_redirect\s+["']\/["']/,
      "login_redirect \"#{redirect_path}\""
  end

  # Update or add create_account_redirect
  if file_content.include?("create_account_redirect")
    gsub_file rodauth_file,
      /create_account_redirect\s+["']\/["']/,
      "create_account_redirect \"#{redirect_path}\""
  elsif file_content.include?("login_redirect")
    inject_into_file rodauth_file,
      "\n    create_account_redirect \"#{redirect_path}\"\n",
      after: /login_redirect.*\n/
  end
end