Skip to content

Commit

Permalink
refactor: extract accounts controller and added Accounts::Update service
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellberg committed Apr 9, 2024
1 parent b7cfaa7 commit 5bc4724
Show file tree
Hide file tree
Showing 16 changed files with 142 additions and 54 deletions.
46 changes: 0 additions & 46 deletions app/controllers/accounts_controller.rb

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion app/views/users/onboarding/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
description: "Finish the registration by setting up your user profile."
) do %>
<main class="flex flex-col gap-4 w-full max-w-2xl">
<%= render "accounts/form", account: @account %>
<%= render "kiqr/accounts/form", account: @account %>
</main>
<% end %>
<% end %>
5 changes: 0 additions & 5 deletions config/routes/authentication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@
resources :invitations, only: %i[show update destroy], as: :user_invitation
end

get "select-account", to: "accounts#select", as: :select_account

resources :accounts, only: [:new, :create]

scope "(/team/:account_id)", account_id: %r{[^/]+} do
resource :account, only: [:edit, :update], path: "profile"
resources :members, controller: "accounts/members", only: [:index, :edit, :update, :destroy]
resources :invitations, controller: "accounts/invitations", only: [:index, :new, :create, :destroy]
end
49 changes: 49 additions & 0 deletions gems/kiqr/app/controllers/kiqr/accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
module Kiqr
class AccountsController < ApplicationController
before_action :setup_account, only: %i[edit update]

def new
@account = Account.new
end

def create
@account = Account.new(account_permitted_parameters)

if @account.valid?
Kiqr::Services::Accounts::Create.call!(account: @account, user: current_user)
flash[:notice] = I18n.t("accounts.create.success")
redirect_to dashboard_path(account_id: @account)
else
render :new, status: :unprocessable_entity
end
end

def update
@account.assign_attributes(account_permitted_parameters)

if @account.valid?
Kiqr::Services::Accounts::Update.call!(account: @account, user: current_user)
flash[:notice] = I18n.t("accounts.update.success")
redirect_to edit_account_path
else
render :edit, status: :unprocessable_entity
end
end

private

def setup_account
@account = Account.find(current_account.id)
end

def form_submit_path
@account.persisted? ? account_path : accounts_path
end
helper_method :form_submit_path

def form_method
@account.persisted? ? :patch : :post
end
helper_method :form_method
end
end
8 changes: 8 additions & 0 deletions gems/kiqr/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,12 @@
registrations: "users/registrations",
sessions: "users/sessions"
}

# Account management
resources :accounts, only: [:new, :create], controller: "kiqr/accounts"
get "select-account", controller: "kiqr/accounts", action: :select, as: :select_account

scope "(/team/:account_id)", account_id: %r{[^/]+} do
resource :account, only: [:edit, :update], path: "profile", controller: "kiqr/accounts"
end
end
1 change: 1 addition & 0 deletions gems/kiqr/lib/kiqr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module Kiqr
module Services
module Accounts
autoload :Create, "kiqr/services/accounts/create"
autoload :Update, "kiqr/services/accounts/update"
end
end

Expand Down
27 changes: 27 additions & 0 deletions gems/kiqr/lib/kiqr/services/accounts/update.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Kiqr
module Services
module Accounts
class Update < Kiqr::ApplicationService
def call(account:, user:)
@account, @user = account, user

permission_check
account.save!

success account
end

private

# Check if user has permission to edit the account
# User can edit their personal account or if they are part of the team.
def permission_check
return if user.personal_account == account || account.account_users.find_by(user: user)
raise StandardError, "User does not have permission to edit this account"
end

attr_reader :account, :user
end
end
end
end
10 changes: 10 additions & 0 deletions gems/kiqr/test/factories/user.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
FactoryBot.define do
factory :user do
transient do
with_account { nil }
end

sequence(:email) { |n| "generic-user-#{n}@example.com" }
password { "th1s1sp4ssw0rd" }
password_confirmation { "th1s1sp4ssw0rd" }
confirmed_at { Time.zone.now }
personal_account { build(:account, personal: true) }

after(:create) do |user, evaluator|
if evaluator.with_account
# Build the association with 'owner: true'
user.account_users.create(account: evaluator.with_account, owner: true)
end
end
# trait :unconfirmed do
# confirmed_at { nil }
# end
Expand Down
Empty file.
41 changes: 41 additions & 0 deletions gems/kiqr/test/services/accounts/update_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require "test_helper"

module Kiqr
module Services
module Accounts
class UpdateTest < ActionDispatch::IntegrationTest
def setup
@service = Kiqr::Services::Accounts::Update.new
end

test "updates personal account" do
user = create(:user)
account = user.personal_account
account.assign_attributes(name: "New Name")

@service.call(account: account, user: user)
assert_equal "New Name", account.reload.name, "Expected account name to be updated"
end

test "updates team account" do
account = create(:account)
user = create(:user, with_account: account)
account.assign_attributes(name: "New Team Name")

@service.call(account: account, user: user)
assert_equal "New Team Name", account.reload.name, "Expected account name to be updated"
end

test "account with invalid attributes raises error" do
user = create(:user)
alien_account = create(:account)
alien_account.assign_attributes(name: "no")

assert_raises(StandardError) do
@service.call(account: alien_account, user: user)
end
end
end
end
end
end
5 changes: 4 additions & 1 deletion gems/kiqr/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
ENV["RAILS_ENV"] = "test"

require "simplecov"
SimpleCov.start "rails" do
SimpleCov.start do
add_filter %r{^/test/}
add_filter "lib/kiqr/version.rb"

add_group "Models", "app/models"
add_group "Services", "lib/kiqr/services"
end

require_relative "../test/dummy/config/environment"
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 @@ -30,7 +30,7 @@ class AccountsControllerTest < ActionDispatch::IntegrationTest

post accounts_path, params: {account: {name: "no"}}
assert_response :unprocessable_entity
assert_template :new
# assert_template :new
end

test "can update personal accounts" do
Expand Down

0 comments on commit 5bc4724

Please sign in to comment.