Class: Pu::Invites::InstallGenerator
Instance Method Summary
collapse
#update_rodauth_redirects
derive_association_name, find_shared_namespace, included
#debug, #error, #exception, #info, #success, #warn
#read_config, #write_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 = <<-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_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
<<-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
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_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}"
return if content =~ /#{Regexp.escape(new_class)}(?!\w)/
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
|
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))
if file_content.include?("after_welcome_redirect")
say_status :skip, "Rodauth already configured for invites", :yellow
return
end
single_line_match = file_content.match(/^(\s*)after_login\s*\{\s*(.+?)\s*\}/)
multi_line_match = file_content.match(/^(\s*)after_login\s+do\s*\n(.*?)\n(\s*)end/m)
if single_line_match
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
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
after_login_code = <<-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,
after_login_code,
before: /^ end\s*\n/
say_status :info, "Added after_login block for invites", :green
end
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_rodauth_redirects(rodauth_file)
end
|
#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_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
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"
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/
inject_into_file relative_path,
" return redirect_to(invites_welcome_check_path) if pending_invite\n\n",
after: /def index\n/
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
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
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
432
433
434
|
# File 'lib/generators/pu/invites/install_generator.rb', line 432
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
|