Skip to content

Commit

Permalink
chore: replace role column with owner boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellberg committed Apr 4, 2024
1 parent a670587 commit a32a2b0
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 32 deletions.
2 changes: 1 addition & 1 deletion app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/users/cancellations_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion app/controllers/users/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
11 changes: 5 additions & 6 deletions app/models/account_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 0 additions & 2 deletions app/views/accounts/members/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
<tr>
<th><%= t(".table.name") %></th>
<th><%= t(".table.email") %></th>
<th><%= t(".table.role") %></th>
<th class="text-right"><%= t(".table.actions") %></th>
</tr>
</thead>
Expand All @@ -23,7 +22,6 @@
<tr>
<td><%= member.user.name %></td>
<td><%= member.user.email %></td>
<td><%= member.role %></td>
<td class="text-right">
<%= link_to t(".table.edit"), edit_member_path(id: member), class: "button xs" %>
</td>
Expand Down
5 changes: 0 additions & 5 deletions config/locales/kiqr.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ en:
table:
name: "Name"
email: "Email"
role: "Role"
actions: "Actions"
edit: "Edit"
edit:
Expand All @@ -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."
Expand Down
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions test/controllers/accounts/members_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion test/controllers/accounts_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"}}
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/users/cancellation_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/current_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/kiqr_test.rb
Original file line number Diff line number Diff line change
@@ -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"}
Expand Down
10 changes: 8 additions & 2 deletions test/models/account_user_test.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions test/system/accounts/accounts_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion test/system/signin_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/system/users/cancellation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a32a2b0

Please sign in to comment.