Class: Pu::Invites::InstallGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
ActiveRecord::Generators::Migration, PlutoniumGenerators::Concerns::RodauthRedirects, PlutoniumGenerators::Generator
Defined in:
lib/generators/pu/invites/install_generator.rb

Instance Method Summary collapse

Methods included from PlutoniumGenerators::Concerns::RodauthRedirects

#update_rodauth_redirects

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

#read_config, #write_config

Instance Method Details

#add_entity_actionObject



188
189
190
191
192
# File 'lib/generators/pu/invites/install_generator.rb', line 188

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_associationObject



172
173
174
175
176
# File 'lib/generators/pu/invites/install_generator.rb', line 172

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_policyObject



194
195
196
197
198
# File 'lib/generators/pu/invites/install_generator.rb', line 194

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_helperObject



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/generators/pu/invites/install_generator.rb', line 217

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 = <<-RUBY
  def current_membership
    return unless entity_scope && user

    @current_membership ||= #{membership_model}.find_by(#{entity_association_name}: entity_scope, #{user_association_name}: user)
  end
  RUBY

  if file_content.include?("private\n")
    inject_into_file resource_policy_path,
      "\n#{helper_code}",
      after: "private\n"
  else
    inject_into_file resource_policy_path,
      "\n  private\n\n#{helper_code}",
      before: /^end\s*\z/
  end
end

#add_routesObject



243
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
# File 'lib/generators/pu/invites/install_generator.rb', line 243

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
      <<-RUBY

  # Invitation welcome routes (shared across all invite flows)
  scope module: :invites do
    get "invitations/welcome", to: "welcome#index", as: :invites_welcome_check
    delete "invitations/welcome", to: "welcome#skip", as: :invites_welcome_skip
  end
      RUBY
    end

    flow_block = <<-RUBY

  #{flow_marker}
  scope module: :invites do
    get "#{invitations_path}/:token", to: "#{invitations_path}#show", as: :#{invite_route_prefix}_invitation
    post "#{invitations_path}/:token/accept", to: "#{invitations_path}#accept", as: :accept_#{invite_route_prefix}_invitation
    get "#{invitations_path}/:token/signup", to: "#{invitations_path}#signup", as: :#{invite_route_prefix}_invitation_signup
    post "#{invitations_path}/:token/signup", to: "#{invitations_path}#signup"
  end
    RUBY

    inject_into_file "config/routes.rb",
      welcome_block + flow_block,
      before: /^end\s*\z/
  end

  # If no main WelcomeController exists, add /welcome route pointing to
  # Invites::WelcomeController so Rodauth's login_redirect "/welcome" works.
  routes_content = File.read(Rails.root.join("config/routes.rb"))
  unless File.exist?(Rails.root.join("app/controllers/welcome_controller.rb")) ||
      routes_content.include?(%(get "welcome", to: "invites/welcome#index"))
    welcome_route = <<-RUBY

  # Welcome route (handled by invites package — replace with pu:saas:welcome for full onboarding)
  get "welcome", to: "invites/welcome#index"
    RUBY

    inject_into_file "config/routes.rb",
      welcome_route,
      before: /^\s*# Invitation welcome routes/
  end
end

#add_user_actionObject



205
206
207
208
209
# File 'lib/generators/pu/invites/install_generator.rb', line 205

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_policyObject



211
212
213
214
215
# File 'lib/generators/pu/invites/install_generator.rb', line 211

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_classObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/generators/pu/invites/install_generator.rb', line 109

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_rodauthObject



298
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
# File 'lib/generators/pu/invites/install_generator.rb', line 298

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 = <<~RUBY.chomp
      #{indent}after_login do
      #{indent}  #{existing_code}
      #{indent}  session[:after_welcome_redirect] = session.delete(:login_redirect)
      #{indent}end
    RUBY

    gsub_file rodauth_file, original_line, new_block
    say_status :info, "Added session redirect to existing after_login block", :green
  elsif multi_line_match
    # Multi-line block - add our line before the end
    indent = multi_line_match[1]
    block_content = multi_line_match[2]
    end_indent = multi_line_match[3]
    original_block = multi_line_match[0]

    new_block = <<~RUBY.chomp
      #{indent}after_login do
      #{block_content}
      #{indent}  session[:after_welcome_redirect] = session.delete(:login_redirect)
      #{end_indent}end
    RUBY

    gsub_file rodauth_file, original_block, new_block
    say_status :info, "Added session redirect to existing after_login block", :green
  else
    # Add new after_login block
     = <<-RUBY

    # ==> User Invites - Move captured path to session for WelcomeController
    after_login do
session[:after_welcome_redirect] = session.delete(:login_redirect)
    end
    RUBY

    inject_into_file rodauth_file,
      ,
      before: /^  end\s*\n/

    say_status :info, "Added after_login block for invites", :green
  end

  # Add other config options if not present
  unless file_content.include?("login_return_to_requested_location?")
    inject_into_file rodauth_file,
      "\n    # Enable path capture so Rodauth stores the originally requested URL\n    login_return_to_requested_location? true\n",
      after: /login_redirect.*\n/
  end

  # Update login_redirect and create_account_redirect to /welcome
  update_rodauth_redirects(rodauth_file)
end

#create_controllersObject



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/generators/pu/invites/install_generator.rb', line 97

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_definitionObject



162
163
164
165
# File 'lib/generators/pu/invites/install_generator.rb', line 162

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_interactionObject



178
179
180
181
182
183
184
185
186
# File 'lib/generators/pu/invites/install_generator.rb', line 178

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_interactionsObject



154
155
156
157
158
159
160
# File 'lib/generators/pu/invites/install_generator.rb', line 154

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_mailerObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/generators/pu/invites/install_generator.rb', line 86

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_modelObject



81
82
83
84
# File 'lib/generators/pu/invites/install_generator.rb', line 81

def create_model
  template "packages/invites/app/models/invites/user_invite.rb",
    "packages/invites/app/models/invites/#{invite_underscore}.rb"
end

#create_packageObject



72
73
74
# File 'lib/generators/pu/invites/install_generator.rb', line 72

def create_package
  generate "pu:pkg:package", "invites"
end

#create_policyObject



167
168
169
170
# File 'lib/generators/pu/invites/install_generator.rb', line 167

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_interactionObject



200
201
202
203
# File 'lib/generators/pu/invites/install_generator.rb', line 200

def create_user_interaction
  template "app/interactions/user_invite_user_interaction.rb",
    "app/interactions/#{user_table}/invite_user_interaction.rb"
end

#create_user_invites_migrationObject



76
77
78
79
# File 'lib/generators/pu/invites/install_generator.rb', line 76

def create_user_invites_migration
  migration_template "db/migrate/create_user_invites.rb",
    "db/migrate/create_#{invite_table}.rb"
end

#create_viewsObject



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/generators/pu/invites/install_generator.rb', line 134

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_controllerObject



381
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
# File 'lib/generators/pu/invites/install_generator.rb', line 381

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_instructionsObject



431
432
433
# File 'lib/generators/pu/invites/install_generator.rb', line 431

def show_instructions
  readme "INSTRUCTIONS" if behavior == :invoke
end

#validate_requirementsObject



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