-
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: Invites::Create and Invites::Destroy services
- Loading branch information
Showing
13 changed files
with
144 additions
and
31 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,32 +1,36 @@ | ||
class Accounts::InvitationsController < ApplicationController | ||
def index | ||
@invitations = current_account.account_invitations.pending | ||
@account = current_account | ||
end | ||
module Accounts | ||
class InvitationsController < KiqrController | ||
def index | ||
@invitations = current_account.account_invitations.pending | ||
@account = current_account | ||
end | ||
|
||
def new | ||
@invitation = current_account.account_invitations.new | ||
end | ||
def new | ||
@invitation = current_account.account_invitations.new | ||
end | ||
|
||
def create | ||
@invitation = current_account.account_invitations.new(invitation_params) | ||
|
||
def create | ||
@invitation = current_account.account_invitations.new(invitation_params) | ||
if @invitation.save | ||
AccountMailer.invitation_email(@invitation).deliver_later | ||
redirect_to invitations_path(account_id: current_account), notice: t(".invitation_sent", email: @invitation.email) | ||
else | ||
render :new, status: :unprocessable_entity | ||
if @invitation.valid? | ||
Kiqr::Services::Invitations::Create.call!(invitation: @invitation, user: current_user) | ||
kiqr_flash_message(:notice, :invitation_sent, email: @invitation.email) | ||
redirect_to invitations_path(account_id: current_account) | ||
else | ||
render :new, status: :unprocessable_entity | ||
end | ||
end | ||
end | ||
|
||
def destroy | ||
@invitation = current_account.account_invitations.find_puid!(params[:id]) | ||
@invitation.destroy | ||
redirect_to invitations_path, notice: t(".deleted") | ||
end | ||
def destroy | ||
@invitation = current_account.account_invitations.find_puid!(params[:id]) | ||
@invitation.destroy | ||
redirect_to invitations_path, notice: t(".deleted") | ||
end | ||
|
||
private | ||
private | ||
|
||
def invitation_params | ||
params.require(:account_invitation).permit(:email) | ||
def invitation_params | ||
params.require(:account_invitation).permit(:email) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
class KiqrController < ApplicationController | ||
private | ||
|
||
def kiqr_flash_message(type, message) | ||
flash[type] = I18n.t("kiqr.flash_messages.#{message}") | ||
def kiqr_flash_message(type, message, **) | ||
flash[type] = I18n.t("kiqr.flash_messages.#{message}", **) | ||
end | ||
end |
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,22 @@ | ||
module Kiqr | ||
module Services | ||
module Invitations | ||
class Create < Kiqr::ApplicationService | ||
def call(invitation:, user:) | ||
@invitation, @user = invitation, user | ||
|
||
invitation.transaction do | ||
invitation.save! | ||
# @todo: AccountMailer.invitation_email(invitation).deliver_later | ||
end | ||
|
||
success invitation | ||
end | ||
|
||
private | ||
|
||
attr_reader :invitation, :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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Kiqr | ||
module Services | ||
module Invitations | ||
class Destroy < Kiqr::ApplicationService | ||
def call(invitation:, user:) | ||
@invitation, @user = invitation, user | ||
|
||
invitation.transaction do | ||
invitation.destroy! | ||
end | ||
|
||
success invitation | ||
end | ||
|
||
private | ||
|
||
attr_reader :invitation, :user | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FactoryBot.define do | ||
factory :account_invitation do | ||
account { create(:account) } | ||
email { Faker::Internet.email } | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require "test_helper" | ||
|
||
module Kiqr | ||
module Services | ||
module Invitations | ||
class CreateTest < ActionDispatch::IntegrationTest | ||
def setup | ||
@service = Kiqr::Services::Invitations::Create.new | ||
end | ||
|
||
test "creates invitation" do | ||
account = create(:account) | ||
user = create(:user, with_account: account) | ||
invitation = account.account_invitations.new(email: "[email protected]") | ||
@service.call(invitation: invitation, user: user) | ||
|
||
assert invitation.persisted?, "Expected invitation to be saved" | ||
assert_equal invitation.account, account, "Expected invitation to be associated with account" | ||
assert account.account_invitations.find_by(email: "[email protected]"), "Expected to find an invitation with the specified email" | ||
end | ||
|
||
test "invitation with invalid attributes raises error" do | ||
account = create(:account) | ||
user = create(:user, with_account: account) | ||
invitation = account.account_invitations.new(email: "foobar") | ||
|
||
assert_raises StandardError do | ||
@service.call(invitation: invitation, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require "test_helper" | ||
|
||
module Kiqr | ||
module Services | ||
module Invitations | ||
class DestroyTest < ActionDispatch::IntegrationTest | ||
def setup | ||
@service = Kiqr::Services::Invitations::Destroy.new | ||
end | ||
|
||
test "destroys invitation" do | ||
account = create(:account) | ||
user = create(:user, with_account: account) | ||
invitation = create(:account_invitation, account: account) | ||
@service.call(invitation: invitation, user: user) | ||
|
||
refute AccountInvitation.find_by_id(invitation.id) | ||
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