Refactoring user_controller separating some classes to service (#10384)

* Refactoring user_controller separating some classes to service

* Replace service Credit::Manager and improve yours specs

* improve method with memoize
This commit is contained in:
Brunno Souza 2020-10-05 05:41:59 -03:00 committed by GitHub
parent 07c69b35ef
commit e5973ebf9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 132 additions and 30 deletions

View file

@ -33,7 +33,7 @@ module Admin
def update
@user = User.find(params[:id])
manage_credits
Credits::Manage.call(@user, user_params)
add_note if user_params[:new_note]
redirect_to "/admin/users/#{params[:id]}"
end
@ -119,13 +119,6 @@ module Admin
private
def manage_credits
add_credits if user_params[:add_credits]
add_org_credits if user_params[:add_org_credits]
remove_org_credits if user_params[:remove_org_credits]
remove_credits if user_params[:remove_credits]
end
def add_note
Note.create(
author_id: current_user.id,
@ -136,28 +129,6 @@ module Admin
)
end
def add_credits
amount = user_params[:add_credits].to_i
Credit.add_to(@user, amount)
end
def remove_credits
amount = user_params[:remove_credits].to_i
Credit.remove_from(@user, amount)
end
def add_org_credits
org = Organization.find(user_params[:organization_id])
amount = user_params[:add_org_credits].to_i
Credit.add_to(org, amount)
end
def remove_org_credits
org = Organization.find(user_params[:organization_id])
amount = user_params[:remove_org_credits].to_i
Credit.remove_from(org, amount)
end
def set_feedback_messages
@related_reports = FeedbackMessage.where(id: @user.reporter_feedback_messages.ids)
.or(FeedbackMessage.where(id: @user.affected_feedback_messages.ids))

View file

@ -0,0 +1,60 @@
module Credits
class Manage
def self.call(user, user_params)
new(
user,
user_params.slice(:add_credits, :remove_credits),
user_params.slice(:organization_id, :add_org_credits, :remove_org_credits),
).call
end
def initialize(user, user_params, org_params)
@user = user
@user_params = user_params
@org_params = org_params
end
def call
add_user_credits
remove_user_credits
add_org_credits
remove_org_credits
end
private
attr_reader :user, :user_params, :org_params
def add_user_credits
return unless user_params[:add_credits]
amount = user_params[:add_credits].to_i
Credit.add_to(user, amount)
end
def remove_user_credits
return unless user_params[:remove_credits]
amount = user_params[:remove_credits].to_i
Credit.remove_from(user, amount)
end
def add_org_credits
return unless org_params[:add_org_credits]
amount = org_params[:add_org_credits].to_i
Credit.add_to(organization, amount)
end
def remove_org_credits
return unless org_params[:remove_org_credits]
amount = org_params[:remove_org_credits].to_i
Credit.remove_from(organization, amount)
end
def organization
@organization ||= Organization.find(org_params[:organization_id])
end
end
end

View file

@ -0,0 +1,71 @@
require "rails_helper"
RSpec.describe Credits::Manage, type: :service do
describe "When the the service is called" do
subject(:invoke_service) { described_class.call(user, user_params) }
let(:user) { create(:user) }
let(:organization) { create(:organization) }
context "when user add an amount of credits" do
let(:user_params) do
{ add_credits: "4" }
end
it "adds user credits" do
expect do
invoke_service
end.to change { user.reload.credits_count }.by(user_params[:add_credits].to_i)
end
end
context "when user removes the proper amount of credits" do
let(:user_params) do
{ remove_credits: "4" }
end
before do
Credit.add_to(user, 10)
end
it "removes user credits" do
expect do
invoke_service
end.to change { user.reload.credits_count }.by(user_params[:remove_credits].to_i * -1)
end
end
context "when remove the proper amount of credits for organizations" do
let(:user_params) do
{ remove_org_credits: "6", organization_id: organization.id }
end
before do
create(:organization_membership, user: user, organization: organization, type_of_user: "admin")
Credit.add_to(organization, 10)
end
it "removes org credits" do
expect do
invoke_service
end.to change { organization.reload.credits_count }.by(user_params[:remove_org_credits].to_i * -1)
end
end
context "when adds an amount of credits for organizations" do
let(:user_params) do
{ add_org_credits: "7", organization_id: organization.id }
end
before do
create(:organization_membership, user: user, organization: organization, type_of_user: "admin")
end
it "adds org credits" do
expect do
invoke_service
end.to change { organization.reload.credits_count }.by(user_params[:add_org_credits].to_i)
end
end
end
end