diff --git a/app/controllers/accounts_controller.rb b/app/controllers/accounts_controller.rb index 98da758..37d4c8e 100644 --- a/app/controllers/accounts_controller.rb +++ b/app/controllers/accounts_controller.rb @@ -7,7 +7,7 @@ def new def create @account = Account.new(account_permitted_parameters) - @account.account_users.new(user: current_user, role: "owner") + @account.account_users.new(user: current_user, owner: true) if @account.save redirect_to dashboard_path(account_id: @account), notice: I18n.t("accounts.create.success") diff --git a/app/controllers/users/cancellations_controller.rb b/app/controllers/users/cancellations_controller.rb index 76d932e..976c96d 100644 --- a/app/controllers/users/cancellations_controller.rb +++ b/app/controllers/users/cancellations_controller.rb @@ -1,6 +1,6 @@ class Users::CancellationsController < ApplicationController def show # Get a list of accounts where the current user is the owner. - @conflicting_account_users = current_user.account_users.where(role: "owner") + @conflicting_account_users = current_user.account_users.where(owner: true) end end diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb index 4c4553a..38153c1 100644 --- a/app/controllers/users/registrations_controller.rb +++ b/app/controllers/users/registrations_controller.rb @@ -1,7 +1,7 @@ class Users::RegistrationsController < Devise::RegistrationsController def destroy # Don't let user cancel their accounts if they are an owner of a team. - if current_user.account_users.find_by(role: "owner") + if current_user.account_users.find_by(owner: true) flash[:alert] = I18n.t("users.registrations.destroy.owner_of_team") return redirect_to delete_user_registration_path end diff --git a/app/models/account_user.rb b/app/models/account_user.rb index b6d26dc..6e71c4a 100644 --- a/app/models/account_user.rb +++ b/app/models/account_user.rb @@ -5,16 +5,15 @@ class AccountUser < ApplicationRecord # This will generate a public_uid for the # Learn more: https://github.com/equivalent/public_uid include PublicUid::ModelConcern - # This is a list of roles that a user can have in an account. - # You can add more roles if you want, but you SHOULD NOT REMOVE - # the owner role, as it is required for the account to function. - ROLES = %w[owner admin readonly] - belongs_to :user belongs_to :account # The name of the account user. delegate :name, to: :user - validates :role, presence: true, inclusion: {in: self::ROLES} + # Prevent the deletion of account owners. + def destroy + return if owner? + super + end end diff --git a/app/views/accounts/members/index.html.erb b/app/views/accounts/members/index.html.erb index 663d15c..084ddbd 100644 --- a/app/views/accounts/members/index.html.erb +++ b/app/views/accounts/members/index.html.erb @@ -14,7 +14,6 @@ <%= t(".table.name") %> <%= t(".table.email") %> - <%= t(".table.role") %> <%= t(".table.actions") %> @@ -23,7 +22,6 @@ <%= member.user.name %> <%= member.user.email %> - <%= member.role %> <%= link_to t(".table.edit"), edit_member_path(id: member), class: "button xs" %> diff --git a/config/locales/kiqr.en.yml b/config/locales/kiqr.en.yml index dfb4961..e4b5f13 100644 --- a/config/locales/kiqr.en.yml +++ b/config/locales/kiqr.en.yml @@ -59,7 +59,6 @@ en: table: name: "Name" email: "Email" - role: "Role" actions: "Actions" edit: "Edit" edit: @@ -74,10 +73,6 @@ en: form: submit: "Save changes" account_users: - roles: - owner: "Owner" - admin: "Admin" - readonly: "Read-Only" destroy: success: "Member has successfully been removed from the team." failure: "Member could not be removed. Please contact support if this issue persists." diff --git a/db/migrate/20240404205037_replace_role_with_owner_on_account_users.rb b/db/migrate/20240404205037_replace_role_with_owner_on_account_users.rb new file mode 100644 index 0000000..b6900d9 --- /dev/null +++ b/db/migrate/20240404205037_replace_role_with_owner_on_account_users.rb @@ -0,0 +1,6 @@ +class ReplaceRoleWithOwnerOnAccountUsers < ActiveRecord::Migration[7.1] + def change + remove_column :account_users, :role, :string, default: "owner", null: false + add_column :account_users, :owner, :boolean, default: false, null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index e091cc4..9d8fbcc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,14 +10,14 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_04_04_092246) do +ActiveRecord::Schema[7.1].define(version: 2024_04_04_205037) do create_table "account_users", force: :cascade do |t| t.integer "user_id", null: false t.integer "account_id", null: false - t.string "role", default: "owner", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "public_uid" + t.boolean "owner", default: false, null: false t.index ["account_id"], name: "index_account_users_on_account_id" t.index ["public_uid"], name: "index_account_users_on_public_uid", unique: true t.index ["user_id"], name: "index_account_users_on_user_id" diff --git a/test/controllers/accounts/members_controller_test.rb b/test/controllers/accounts/members_controller_test.rb index e76a6ed..d361820 100644 --- a/test/controllers/accounts/members_controller_test.rb +++ b/test/controllers/accounts/members_controller_test.rb @@ -14,8 +14,8 @@ class Accounts::MembersControllerTest < ActionDispatch::IntegrationTest some_user = create(:user) account = create(:account, name: "Team account") - account.account_users << AccountUser.create(user: user, role: "owner") - account.account_users << AccountUser.create(user: some_user, role: "admin") + account.account_users << AccountUser.create(user: user, owner: true) + account.account_users << AccountUser.create(user: some_user) sign_in user get edit_member_path(account_id: account, id: some_user.account_users.first) @@ -26,7 +26,7 @@ class Accounts::MembersControllerTest < ActionDispatch::IntegrationTest test "can show members as team account" do user = create(:user) account = create(:account, name: "Team account") - account.account_users << AccountUser.create(user:, role: "owner") + account.account_users << AccountUser.create(user:, owner: true) sign_in user get members_path(account_id: account) @@ -39,8 +39,8 @@ class Accounts::MembersControllerTest < ActionDispatch::IntegrationTest some_user = create(:user) account = create(:account, name: "Team account") - account.account_users << AccountUser.create(user: user, role: "owner") - account.account_users << AccountUser.create(user: some_user, role: "admin") + account.account_users << AccountUser.create(user: user, owner: true) + account.account_users << AccountUser.create(user: some_user) assert_includes account.reload.users, some_user diff --git a/test/controllers/accounts_controller_test.rb b/test/controllers/accounts_controller_test.rb index 813915d..b9c96f6 100644 --- a/test/controllers/accounts_controller_test.rb +++ b/test/controllers/accounts_controller_test.rb @@ -47,7 +47,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest test "can update team accounts" do user = create(:user) account = create(:account, name: "Team account") - account.account_users << AccountUser.create(user:, role: "owner") + account.account_users << AccountUser.create(user:, owner: true) sign_in user patch account_path(account_id: account), params: {account: {name: "New company name"}} diff --git a/test/controllers/users/cancellation_controller_test.rb b/test/controllers/users/cancellation_controller_test.rb index 8121a76..86f8407 100644 --- a/test/controllers/users/cancellation_controller_test.rb +++ b/test/controllers/users/cancellation_controller_test.rb @@ -19,7 +19,7 @@ class Users::RegistrationControllerTest < ActionDispatch::IntegrationTest test "can't delete a user with owned teams" do user = create(:user) team_account = create(:account, name: "Team account") - team_account.account_users << AccountUser.create(user: user, role: "owner") + team_account.account_users << AccountUser.create(user: user, owner: true) sign_in user delete user_registration_path diff --git a/test/helpers/current_helper_test.rb b/test/helpers/current_helper_test.rb index d64e533..a7cb5d8 100644 --- a/test/helpers/current_helper_test.rb +++ b/test/helpers/current_helper_test.rb @@ -7,7 +7,7 @@ class CurrentHelperTest < ActionView::TestCase @current_user = create(:user) @team_account = create(:account, name: "Company 1") @alien_account = create(:account, name: "Someone else's account") - @current_user.account_users << AccountUser.new(account: @team_account, role: "owner") + @current_user.account_users << AccountUser.new(account: @team_account, owner: true) Current.user = @current_user end diff --git a/test/kiqr_test.rb b/test/kiqr_test.rb index 8a1c35d..087e102 100644 --- a/test/kiqr_test.rb +++ b/test/kiqr_test.rb @@ -1,3 +1,5 @@ +require "test_helper" + class KiqrTest < ActiveSupport::TestCase test "should set default_url_options to localhost:3000 in test environment" do assert_equal Kiqr.default_url_options, {host: "localhost", port: 3000, protocol: "http"} diff --git a/test/models/account_user_test.rb b/test/models/account_user_test.rb index 1abf13c..52d0257 100644 --- a/test/models/account_user_test.rb +++ b/test/models/account_user_test.rb @@ -1,7 +1,13 @@ require "test_helper" class AccountUserTest < ActiveSupport::TestCase - test "should have the owner role" do - assert_includes AccountUser::ROLES, "owner" + test "can't remove the team owner" do + user = create(:user) + account = create(:account, name: "Team account") + account.account_users << AccountUser.create(user: user, owner: true) + + assert_no_difference -> { AccountUser.count } do + account.account_users.first.destroy + end end end diff --git a/test/system/accounts/accounts_test.rb b/test/system/accounts/accounts_test.rb index 36620fc..a2e38ed 100644 --- a/test/system/accounts/accounts_test.rb +++ b/test/system/accounts/accounts_test.rb @@ -21,7 +21,7 @@ class EditAccountsTest < ApplicationSystemTestCase test "can edit team account" do user = create(:user) team_account = create(:account, name: "Team account") - team_account.account_users << AccountUser.create(user: user, role: "owner") + team_account.account_users << AccountUser.create(user: user, owner: true) sign_in(user) visit edit_account_path(account_id: team_account) @@ -51,7 +51,7 @@ class EditAccountsTest < ApplicationSystemTestCase account = user.reload.accounts.last assert_equal "New name", account.name - assert_equal "owner", account.account_users.find_by(user: user).role + assert account.account_users.find_by(user: user).owner? assert_current_path dashboard_path(account_id: account) end end diff --git a/test/system/signin_test.rb b/test/system/signin_test.rb index e6b4f03..d1b248c 100644 --- a/test/system/signin_test.rb +++ b/test/system/signin_test.rb @@ -15,7 +15,7 @@ class SigninTest < ApplicationSystemTestCase test "select account after sign in if user has teams" do user = create(:user) account = create(:account, name: "Team account") - account.account_users << AccountUser.create(user:, role: "owner") + account.account_users << AccountUser.create(user:, owner: true) visit new_user_session_path fill_in "user[email]", with: user.email diff --git a/test/system/users/cancellation_test.rb b/test/system/users/cancellation_test.rb index 8ec1b89..940d403 100644 --- a/test/system/users/cancellation_test.rb +++ b/test/system/users/cancellation_test.rb @@ -14,7 +14,7 @@ class EditAccountsTest < ApplicationSystemTestCase test "don't show delete button if user is an owner of a team" do user = create(:user) team_account = create(:account, name: "Team account") - team_account.account_users << AccountUser.create(user: user, role: "owner") + team_account.account_users << AccountUser.create(user: user, owner: true) sign_in(user) visit delete_user_registration_path