Skip to content

Commit

Permalink
refactor: move account_invitations controller to gem
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellberg committed Apr 11, 2024
1 parent 241b203 commit 37188bb
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 50 deletions.
36 changes: 0 additions & 36 deletions app/controllers/accounts/invitations_controller.rb

This file was deleted.

4 changes: 2 additions & 2 deletions app/views/accounts/members/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</div>
</div>
<div class="mt-6 flex justify-start gap-x-6 items-center">
<%= link_to t(".buttons.invite"), new_invitation_path, class: "button" %>
<%= link_to t(".buttons.view_pending"), invitations_path, class: "text-primary" %>
<%= link_to t(".buttons.invite"), new_account_invitation_path, class: "button" %>
<%= link_to t(".buttons.view_pending"), account_invitations_path, class: "text-primary" %>
</div>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<td><%= invitation.email %></td>
<td><%= user_invitation_url(invitation, account_id: nil) %></td>
<td class="text-right">
<%= button_to t(".table.remove"), invitation_path(id: invitation), class: "button xs danger", method: :delete, data: { confirm: t(".confirm_delete", email: invitation.email), turbo_confirm: t(".confirm_delete", email: invitation.email) } %>
<%= button_to t(".table.remove"), account_invitation_path(id: invitation), class: "button xs danger", method: :delete, data: { confirm: t(".confirm_delete", email: invitation.email), turbo_confirm: t(".confirm_delete", email: invitation.email) } %>
</td>
</tr>
<% end %>
Expand All @@ -39,6 +39,6 @@
<%= link_to members_path, class: "button alt" do %>
<i class="fa fa-arrow-left"></i> <%= t(".buttons.back") %>
<% end %>
<%= link_to t(".buttons.invite"), new_invitation_path, class: "button" %>
<%= link_to t(".buttons.invite"), new_account_invitation_path, class: "button" %>
</div>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<div class="prose dark:prose-invert">
<p><%= t(".instructions") %></p>

<%= simple_form_for(@invitation, url: invitations_path) do |f| %>
<%= simple_form_for(@invitation, url: account_invitations_path) do |f| %>
<%= f.input :email, placeholder: t(".form.email.placeholder"), required: true, autofocus: true %>
<div class="mt-4 flex justify-between items-center">
<%= f.button :submit, t(".form.submit") %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/partials/navigations/_settings.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
description: t('.items.members.description'),
icon: "fa fa-users",
path: members_path,
active: current_base_path?(members_path) || current_base_path?(invitations_path)
active: current_base_path?(members_path) || current_base_path?(account_invitations_path)
)) %>
<% end %>

Expand Down
1 change: 0 additions & 1 deletion config/routes/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@

scope "(/team/:account_id)", account_id: %r{[^/]+} do
resources :members, controller: "accounts/members", only: [:index, :edit, :update, :destroy]
resources :invitations, controller: "accounts/invitations", only: [:index, :new, :create, :destroy]
end
38 changes: 38 additions & 0 deletions gems/kiqr/app/controllers/kiqr/accounts/invitations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Kiqr
module Accounts
class InvitationsController < KiqrController
def index
@invitations = current_account.account_invitations.pending
@account = current_account
end

def new
@invitation = current_account.account_invitations.new
end

def create
@invitation = current_account.account_invitations.new(invitation_params)

if @invitation.valid?
Kiqr::Services::Invitations::Create.call!(invitation: @invitation, user: current_user)
kiqr_flash_message(:notice, :invitation_sent, email: @invitation.email)
redirect_to account_invitations_path(account_id: current_account)
else
render :new, status: :unprocessable_entity
end
end

def destroy
@invitation = current_account.account_invitations.find_puid!(params[:id])
@invitation.destroy
redirect_to account_invitations_path, notice: t(".deleted")
end

private

def invitation_params
params.require(:account_invitation).permit(:email)
end
end
end
end
4 changes: 4 additions & 0 deletions gems/kiqr/lib/kiqr/rails/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ def kiqr_routes(options = {})

account_routes(options)
devise_routes(options)

teamable_scope do
resources :account_invitations, controller: "kiqr/accounts/invitations", only: [:index, :new, :create, :destroy]
end
end

private
Expand Down
14 changes: 7 additions & 7 deletions test/controllers/accounts/invitations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class Accounts::InvitationsControllerTest < ActionDispatch::IntegrationTest
sign_in user

assert_difference -> { account.account_invitations.count } do
post invitations_path(account_id: account), params: {account_invitation: {email: "[email protected]"}}
post account_invitations_path(account_id: account), params: {account_invitation: {email: "[email protected]"}}
end

assert_redirected_to invitations_path(account_id: account)
assert_redirected_to account_invitations_path(account_id: account)
end

test "can't invite a user to someone else team" do
Expand All @@ -25,7 +25,7 @@ class Accounts::InvitationsControllerTest < ActionDispatch::IntegrationTest
sign_in user

assert_raises(PublicUid::RecordNotFound) do
post invitations_path(account_id: foreign_account), params: {account_invitation: {email: "[email protected]"}}
post account_invitations_path(account_id: foreign_account), params: {account_invitation: {email: "[email protected]"}}
end
end

Expand All @@ -36,11 +36,11 @@ class Accounts::InvitationsControllerTest < ActionDispatch::IntegrationTest

sign_in user

post invitations_path(account_id: account), params: {account_invitation: {email: "[email protected]"}}
assert_redirected_to invitations_path(account_id: account)
post account_invitations_path(account_id: account), params: {account_invitation: {email: "[email protected]"}}
assert_redirected_to account_invitations_path(account_id: account)

assert_no_difference -> { account.account_invitations.count } do
post invitations_path(account_id: account), params: {account_invitation: {email: "[email protected]"}}
post account_invitations_path(account_id: account), params: {account_invitation: {email: "[email protected]"}}
end
end

Expand All @@ -52,7 +52,7 @@ class Accounts::InvitationsControllerTest < ActionDispatch::IntegrationTest
sign_in user

assert_no_difference -> { account.account_invitations.count } do
post invitations_path(account_id: account), params: {account_invitation: {email: "foo"}}
post account_invitations_path(account_id: account), params: {account_invitation: {email: "foo"}}
end

assert_response :unprocessable_entity
Expand Down

0 comments on commit 37188bb

Please sign in to comment.