@@ -7,6 +7,34 @@ class PlatformInvitationsController < ApplicationController # rubocop:todo Style
77 before_action :set_platform_invitation , only : %i[ destroy resend ]
88 after_action :verify_authorized
99
10+ before_action only : %i[ index ] , if : -> { Rails . env . development? } do
11+ # Make sure that all Platform Invitation subclasses are loaded in dev to generate new block buttons
12+ ::BetterTogether ::PlatformInvitation . load_all_subclasses
13+ end
14+
15+ # GET /platforms/:platform_id/platform_invitations
16+ def index
17+ authorize BetterTogether ::PlatformInvitation
18+
19+ # Use optimized query with all necessary includes to prevent N+1
20+ @platform_invitations = policy_scope ( @platform . invitations )
21+ . includes (
22+ { inviter : [ :string_translations ] } ,
23+ { invitee : [ :string_translations ] }
24+ )
25+
26+ # Preload roles for the form to prevent N+1 queries during rendering
27+ @community_roles = BetterTogether ::Role . where ( resource_type : 'BetterTogether::Community' )
28+ . includes ( :string_translations )
29+ . order ( :position )
30+ @platform_roles = BetterTogether ::Role . where ( resource_type : 'BetterTogether::Platform' )
31+ . includes ( :string_translations )
32+ . order ( :position )
33+
34+ # Find the default community member role for the hidden field
35+ @default_community_role = @community_roles . find_by ( identifier : 'community_member' )
36+ end
37+
1038 # POST /platforms/:platform_id/platform_invitations
1139 def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
1240 @platform_invitation = @platform . invitations . new ( platform_invitation_params ) do |pi |
@@ -26,22 +54,27 @@ def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
2654 format . turbo_stream do
2755 render turbo_stream : [
2856 turbo_stream . prepend ( 'platform_invitations_table_body' ,
29- # rubocop:todo Layout/LineLength
30- partial : 'better_together/platform_invitations/platform_invitation' , locals : { platform_invitation : @platform_invitation } ) ,
31- # rubocop:enable Layout/LineLength
32- turbo_stream . replace ( 'flash_messages' , partial : 'layouts/better_together/flash_messages' ,
33- locals : { flash : } )
57+ partial : 'better_together/platform_invitations/platform_invitation' ,
58+ locals : { platform_invitation : @platform_invitation } ) ,
59+ turbo_stream . replace ( 'flash_messages' ,
60+ partial : 'layouts/better_together/flash_messages' ,
61+ locals : { flash : } )
3462 ]
3563 end
3664 else
3765 flash . now [ :alert ] = t ( 'flash.generic.error_create' , resource : t ( 'resources.invitation' ) )
38- format . html { redirect_to @platform , alert : @platform_invitation . errors . full_messages . to_sentence }
66+ format . html do
67+ redirect_to platform_platform_invitations_path ( @platform ) ,
68+ alert : @platform_invitation . errors . full_messages . to_sentence
69+ end
3970 format . turbo_stream do
4071 render turbo_stream : [
41- turbo_stream . update ( 'form_errors' , partial : 'layouts/better_together/errors' ,
42- locals : { object : @platform_invitation } ) ,
43- turbo_stream . replace ( 'flash_messages' , partial : 'layouts/better_together/flash_messages' ,
44- locals : { flash : } )
72+ turbo_stream . update ( 'form_errors' ,
73+ partial : 'layouts/better_together/errors' ,
74+ locals : { object : @platform_invitation } ) ,
75+ turbo_stream . replace ( 'flash_messages' ,
76+ partial : 'layouts/better_together/flash_messages' ,
77+ locals : { flash : } )
4578 ]
4679 end
4780 end
@@ -54,24 +87,24 @@ def destroy # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
5487 if @platform_invitation . destroy
5588 flash . now [ :notice ] = t ( 'flash.generic.removed' , resource : t ( 'resources.invitation' ) )
5689 respond_to do |format |
57- format . html { redirect_to @platform }
90+ format . html { redirect_to platform_platform_invitations_path ( @platform ) }
5891 format . turbo_stream do
5992 render turbo_stream : [
6093 turbo_stream . remove ( helpers . dom_id ( @platform_invitation ) ) ,
61- turbo_stream . replace ( 'flash_messages' , partial : 'layouts/better_together/flash_messages' ,
62- locals : { flash : } )
94+ turbo_stream . replace ( 'flash_messages' ,
95+ partial : 'layouts/better_together/flash_messages' ,
96+ locals : { flash : } )
6397 ]
6498 end
6599 end
66100 else
67101 flash . now [ :error ] = t ( 'flash.generic.error_remove' , resource : t ( 'resources.invitation' ) )
68102 respond_to do |format |
69- format . html { redirect_to @platform , alert : flash . now [ :error ] }
103+ format . html { redirect_to platform_platform_invitations_path ( @platform ) , alert : flash . now [ :error ] }
70104 format . turbo_stream do
71- # rubocop:todo Layout/LineLength
72- render turbo_stream : turbo_stream . replace ( 'flash_messages' , partial : 'layouts/better_together/flash_messages' ,
73- # rubocop:enable Layout/LineLength
74- locals : { flash : } )
105+ render turbo_stream : turbo_stream . replace ( 'flash_messages' ,
106+ partial : 'layouts/better_together/flash_messages' ,
107+ locals : { flash : } )
75108 end
76109 end
77110 end
@@ -85,14 +118,15 @@ def resend # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
85118 flash [ :notice ] = t ( 'flash.generic.queued' , resource : t ( 'resources.invitation_email' ) )
86119
87120 respond_to do |format |
88- format . html { redirect_to @platform , notice : flash [ :notice ] }
121+ format . html { redirect_to platform_platform_invitations_path ( @platform ) , notice : flash [ :notice ] }
89122 format . turbo_stream do
90123 render turbo_stream : [
91124 turbo_stream . replace ( helpers . dom_id ( @platform_invitation ) ,
92125 partial : 'better_together/platform_invitations/platform_invitation' ,
93126 locals : { platform_invitation : @platform_invitation } ) ,
94- turbo_stream . replace ( 'flash_messages' , partial : 'layouts/better_together/flash_messages' ,
95- locals : { flash : } )
127+ turbo_stream . replace ( 'flash_messages' ,
128+ partial : 'layouts/better_together/flash_messages' ,
129+ locals : { flash : } )
96130 ]
97131 end
98132 end
0 commit comments