-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract accounts controller and added Accounts::Update service
- Loading branch information
Showing
16 changed files
with
142 additions
and
54 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters