From bc66e3730ed9e51b85c1a0281e94833df99f45d4 Mon Sep 17 00:00:00 2001 From: Rasmus Kjellberg <2277443+kjellberg@users.noreply.github.com> Date: Mon, 15 Apr 2024 22:13:33 +0200 Subject: [PATCH] refactor: move members controller to gem as account_users controller --- .../accounts/members_controller.rb | 26 ---------------- .../account_users}/_form.html.erb | 0 .../account_users}/edit.html.erb | 4 +-- .../account_users}/index.html.erb | 2 +- .../kiqr/accounts/invitations/index.html.erb | 2 +- .../kiqr/accounts/invitations/new.html.erb | 2 +- .../partials/navigations/_settings.html.erb | 4 +-- config/locales/kiqr.en.yml | 30 ------------------- config/routes/authentication.rb | 2 +- .../kiqr/account_users_controller.rb | 29 ++++++++++++++++++ gems/kiqr/config/locales/kiqr/en.yml | 26 ++++++++++++++++ ...st.rb => account_users_controller_test.rb} | 12 ++++---- .../accounts/invitations_controller_test.rb | 2 +- 13 files changed, 70 insertions(+), 71 deletions(-) delete mode 100644 app/controllers/accounts/members_controller.rb rename app/views/{accounts/members => kiqr/account_users}/_form.html.erb (100%) rename app/views/{accounts/members => kiqr/account_users}/edit.html.erb (64%) rename app/views/{accounts/members => kiqr/account_users}/index.html.erb (91%) create mode 100644 gems/kiqr/app/controllers/kiqr/account_users_controller.rb rename test/controllers/{accounts/members_controller_test.rb => account_users_controller_test.rb} (77%) diff --git a/app/controllers/accounts/members_controller.rb b/app/controllers/accounts/members_controller.rb deleted file mode 100644 index bac8c03..0000000 --- a/app/controllers/accounts/members_controller.rb +++ /dev/null @@ -1,26 +0,0 @@ -class Accounts::MembersController < ApplicationController - before_action :ensure_team_account - before_action :setup_account - - def edit - @account_user = @account.account_users.find_puid!(params[:id]) - end - - def destroy - @account_user = @account.account_users.find_puid!(params[:id]) - @account_user.destroy! - flash[:notice] = I18n.t("account_users.destroy.success") - redirect_to members_path - end - - private - - def ensure_team_account - redirect_to edit_account_path if current_account.personal? - end - - def setup_account - @account = current_account - @members = current_account.account_users - end -end diff --git a/app/views/accounts/members/_form.html.erb b/app/views/kiqr/account_users/_form.html.erb similarity index 100% rename from app/views/accounts/members/_form.html.erb rename to app/views/kiqr/account_users/_form.html.erb diff --git a/app/views/accounts/members/edit.html.erb b/app/views/kiqr/account_users/edit.html.erb similarity index 64% rename from app/views/accounts/members/edit.html.erb rename to app/views/kiqr/account_users/edit.html.erb index d45cafd..104c487 100644 --- a/app/views/accounts/members/edit.html.erb +++ b/app/views/kiqr/account_users/edit.html.erb @@ -9,11 +9,11 @@

<%= t(".delete.description", name: @account_user.name) %>

- <%= button_to t(".delete.button"), member_path(@account_user), class: "button danger", method: :delete, data: { confirm: t(".confirmation_message", name: @account_user.name), turbo_confirm: t(".confirmation_message", name: @account_user.name) } %> + <%= button_to t(".delete.button"), account_user_path(@account_user), class: "button danger", method: :delete, data: { confirm: t(".confirmation_message", name: @account_user.name), turbo_confirm: t(".confirmation_message", name: @account_user.name) } %>
- <%= link_to members_path, class: "button alt" do %> + <%= link_to account_users_path, class: "button alt" do %> <%= t(".back_to_members") %> <% end %>
diff --git a/app/views/accounts/members/index.html.erb b/app/views/kiqr/account_users/index.html.erb similarity index 91% rename from app/views/accounts/members/index.html.erb rename to app/views/kiqr/account_users/index.html.erb index c56201b..0723502 100644 --- a/app/views/accounts/members/index.html.erb +++ b/app/views/kiqr/account_users/index.html.erb @@ -24,7 +24,7 @@ <%= member.user.name %> <%= member.user.email %> - <%= link_to t(".table.edit"), edit_member_path(id: member), class: "button xs" %> + <%= link_to t(".table.edit"), edit_account_user_path(id: member), class: "button xs" %> <% end %> diff --git a/app/views/kiqr/accounts/invitations/index.html.erb b/app/views/kiqr/accounts/invitations/index.html.erb index 2e73295..7353e81 100644 --- a/app/views/kiqr/accounts/invitations/index.html.erb +++ b/app/views/kiqr/accounts/invitations/index.html.erb @@ -36,7 +36,7 @@ <% end %>
- <%= link_to members_path, class: "button alt" do %> + <%= link_to account_users_path, class: "button alt" do %> <%= t(".buttons.back") %> <% end %> <%= link_to t(".buttons.invite"), new_account_invitation_path, class: "button" %> diff --git a/app/views/kiqr/accounts/invitations/new.html.erb b/app/views/kiqr/accounts/invitations/new.html.erb index c98e727..6172ff7 100644 --- a/app/views/kiqr/accounts/invitations/new.html.erb +++ b/app/views/kiqr/accounts/invitations/new.html.erb @@ -20,7 +20,7 @@
- <%= link_to members_path, class: "button alt" do %> + <%= link_to account_users_path, class: "button alt" do %> <%= t(".back_to_members") %> <% end %>
diff --git a/app/views/partials/navigations/_settings.html.erb b/app/views/partials/navigations/_settings.html.erb index 4495f91..e46cf32 100644 --- a/app/views/partials/navigations/_settings.html.erb +++ b/app/views/partials/navigations/_settings.html.erb @@ -15,8 +15,8 @@ label: t('.items.members.label'), description: t('.items.members.description'), icon: "fa fa-users", - path: members_path, - active: current_base_path?(members_path) || current_base_path?(account_invitations_path) + path: account_users_path, + active: current_base_path?(account_users_path) || current_base_path?(account_invitations_path) )) %> <% end %> diff --git a/config/locales/kiqr.en.yml b/config/locales/kiqr.en.yml index 96aca92..efe8374 100644 --- a/config/locales/kiqr.en.yml +++ b/config/locales/kiqr.en.yml @@ -32,36 +32,6 @@ en: languages: en: "English" sv: "Swedish" - accounts: - members: - index: - title: "Team members" - description: "Manage team members." - box_title: "Members of %{team_name}" - table: - name: "Name" - email: "Email" - actions: "Actions" - edit: "Edit" - buttons: - invite: "Invite a user" - view_pending: "View pending invitations" - edit: - title: "Edit team member" - description: "Change settings for a team member." - box_title: "Edit team member: %{name}" - confirmation_message: "Are you sure you want to REMOVE %{name} from the team?" - back_to_members: "Back to team members" - delete: - box_title: "Remove team member" - description: "Remove %{name} from the team. They will no longer have access to the team account, but you can always invite them back later." - button: "Remove team member" - form: - submit: "Save changes" - account_users: - destroy: - success: "Member has successfully been removed from the team." - failure: "Member could not be removed. Please contact support if this issue persists." users: sessions: new: diff --git a/config/routes/authentication.rb b/config/routes/authentication.rb index 831cdbe..77d607e 100644 --- a/config/routes/authentication.rb +++ b/config/routes/authentication.rb @@ -11,5 +11,5 @@ end scope "(/team/:account_id)", account_id: %r{[^/]+} do - resources :members, controller: "accounts/members", only: [:index, :edit, :update, :destroy] + resources :account_users, controller: "kiqr/account_users", only: [:index, :edit, :update, :destroy] end diff --git a/gems/kiqr/app/controllers/kiqr/account_users_controller.rb b/gems/kiqr/app/controllers/kiqr/account_users_controller.rb new file mode 100644 index 0000000..62f2920 --- /dev/null +++ b/gems/kiqr/app/controllers/kiqr/account_users_controller.rb @@ -0,0 +1,29 @@ +module Kiqr + class AccountUsersController < KiqrController + before_action :ensure_team_account + before_action :setup_account + + def edit + @account_user = @account.account_users.find_puid!(params[:id]) + end + + def destroy + @account_user = @account.account_users.find_puid!(params[:id]) + @account_user.destroy! + kiqr_flash_message(:alert, :account_user_destroyed) + flash[:notice] = I18n.t("kiqr.flash_messages") + redirect_to account_users_path + end + + private + + def ensure_team_account + redirect_to edit_account_path if current_account.personal? + end + + def setup_account + @account = current_account + @members = current_account.account_users + end + end +end diff --git a/gems/kiqr/config/locales/kiqr/en.yml b/gems/kiqr/config/locales/kiqr/en.yml index 78f7b4f..f5ff97a 100644 --- a/gems/kiqr/config/locales/kiqr/en.yml +++ b/gems/kiqr/config/locales/kiqr/en.yml @@ -8,6 +8,7 @@ en: invitation_accepted: "You have accepted the invitation to join %{account}." invitation_rejected: "You have rejected the invitation to join %{account}." invitation_expired: "The invitation has expired." + account_user_destroyed: "Member has successfully been removed from the team." accounts: new: title: "Create a new team account" @@ -52,6 +53,31 @@ en: buttons: invite: "Invite a new user" back: "Back to team members" + account_users: + index: + title: "Team members" + description: "Manage team members." + box_title: "Members of %{team_name}" + table: + name: "Name" + email: "Email" + actions: "Actions" + edit: "Edit" + buttons: + invite: "Invite a user" + view_pending: "View pending invitations" + edit: + title: "Edit team member" + description: "Change settings for a team member." + box_title: "Edit team member: %{name}" + confirmation_message: "Are you sure you want to REMOVE %{name} from the team?" + back_to_members: "Back to team members" + delete: + box_title: "Remove team member" + description: "Remove %{name} from the team. They will no longer have access to the team account, but you can always invite them back later." + button: "Remove team member" + form: + submit: "Save changes" invitations: show: title: "Accept invitation" diff --git a/test/controllers/accounts/members_controller_test.rb b/test/controllers/account_users_controller_test.rb similarity index 77% rename from test/controllers/accounts/members_controller_test.rb rename to test/controllers/account_users_controller_test.rb index d361820..4ddd748 100644 --- a/test/controllers/accounts/members_controller_test.rb +++ b/test/controllers/account_users_controller_test.rb @@ -1,10 +1,10 @@ require "test_helper" -class Accounts::MembersControllerTest < ActionDispatch::IntegrationTest +class AccountUsersControllerTest < ActionDispatch::IntegrationTest test "can't show members as personal account" do user = create(:user) sign_in user - get members_path + get account_users_path assert_redirected_to edit_account_path(account_id: nil) end @@ -18,7 +18,7 @@ class Accounts::MembersControllerTest < ActionDispatch::IntegrationTest account.account_users << AccountUser.create(user: some_user) sign_in user - get edit_member_path(account_id: account, id: some_user.account_users.first) + get edit_account_user_path(account_id: account, id: some_user.account_users.first) assert_response :success end @@ -29,7 +29,7 @@ class Accounts::MembersControllerTest < ActionDispatch::IntegrationTest account.account_users << AccountUser.create(user:, owner: true) sign_in user - get members_path(account_id: account) + get account_users_path(account_id: account) assert_response :success end @@ -45,9 +45,9 @@ class Accounts::MembersControllerTest < ActionDispatch::IntegrationTest assert_includes account.reload.users, some_user sign_in user - delete member_path(account_id: account, id: some_user.account_users.first) + delete account_user_path(account_id: account, id: some_user.account_users.first) - assert_redirected_to members_path(account_id: account) + assert_redirected_to account_users_path(account_id: account) assert_not_includes account.reload.users, some_user end diff --git a/test/controllers/accounts/invitations_controller_test.rb b/test/controllers/accounts/invitations_controller_test.rb index b5233d8..0a9121b 100644 --- a/test/controllers/accounts/invitations_controller_test.rb +++ b/test/controllers/accounts/invitations_controller_test.rb @@ -1,6 +1,6 @@ require "test_helper" -class Accounts::InvitationsControllerTest < ActionDispatch::IntegrationTest +class InvitationsControllerTest < ActionDispatch::IntegrationTest test "can invite a user" do user = create(:user) account = create(:account, name: "Team account")