Extract GiveTime service
I only tried to make a transfer and it worked. I followed EDD; going through each of the errors until it worked, because I didn't find any test for this action in the controller. Unit testing the service should be the next task.
This commit is contained in:
@ -62,15 +62,12 @@ class UsersController < ApplicationController
|
||||
end
|
||||
|
||||
def give_time
|
||||
@user = scoped_users.find(params[:id])
|
||||
@destination = @user.members.
|
||||
find_by(organization: current_organization).account.id
|
||||
@source = find_transfer_source
|
||||
@offer = find_transfer_offer
|
||||
@transfer = Transfer.new(source: @source,
|
||||
destination: @destination,
|
||||
post: @offer)
|
||||
@sources = find_transfer_sources_for_admin
|
||||
give_time = GiveTime.new(self)
|
||||
|
||||
@user = give_time.user
|
||||
@transfer = give_time.transfer
|
||||
@sources = give_time.sources
|
||||
@offer = give_time.offer
|
||||
end
|
||||
|
||||
private
|
||||
@ -85,22 +82,6 @@ class UsersController < ApplicationController
|
||||
params.require(:user).permit *fields_to_permit
|
||||
end
|
||||
|
||||
def find_transfer_offer
|
||||
current_organization.offers.
|
||||
find(params[:offer]) if params[:offer].present?
|
||||
end
|
||||
|
||||
def find_transfer_source
|
||||
current_user.members.
|
||||
find_by(organization: current_organization).account.id
|
||||
end
|
||||
|
||||
def find_transfer_sources_for_admin
|
||||
return unless admin?
|
||||
[current_organization.account] +
|
||||
current_organization.member_accounts.where("members.active is true")
|
||||
end
|
||||
|
||||
def find_user
|
||||
if current_user.id == params[:id].to_i
|
||||
current_user
|
||||
|
||||
@ -19,6 +19,12 @@ class Transfer < ActiveRecord::Base
|
||||
|
||||
after_create :make_movements
|
||||
|
||||
# TODO: Extract it along with destination, source and amount accessors. It
|
||||
# absolutely violates encapsulation and adds high coupling.
|
||||
#
|
||||
# EDIT: It might not be that easy. The simple_form_for in give_time.html.erb
|
||||
# depends on this model having the destination column. A form object could be
|
||||
# a solution.
|
||||
def make_movements
|
||||
movements.create(account: Account.find(source_id), amount: -amount.to_i)
|
||||
movements.create(account: Account.find(destination_id), amount: amount.to_i)
|
||||
|
||||
57
app/services/give_time.rb
Normal file
57
app/services/give_time.rb
Normal file
@ -0,0 +1,57 @@
|
||||
class GiveTime
|
||||
def initialize(controller)
|
||||
@controller = controller
|
||||
@scope = controller.scoped_users
|
||||
@params = controller.params
|
||||
@organization = controller.send :current_organization
|
||||
@user = controller.current_user
|
||||
end
|
||||
|
||||
def call
|
||||
@source = find_transfer_source
|
||||
end
|
||||
|
||||
def user
|
||||
scope.find(params[:id])
|
||||
end
|
||||
|
||||
# Transfer object used to populate the simple_form_for in give_time.html.erb
|
||||
def transfer
|
||||
Transfer.new(
|
||||
source: @source,
|
||||
destination: destination,
|
||||
post: @offer
|
||||
)
|
||||
end
|
||||
|
||||
def sources
|
||||
find_transfer_sources_for_admin
|
||||
end
|
||||
|
||||
def offer
|
||||
find_transfer_offer
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :scope, :params, :organization, :controller
|
||||
|
||||
def find_transfer_source
|
||||
user.members.find_by(organization: organization).account.id
|
||||
end
|
||||
|
||||
def find_transfer_offer
|
||||
organization.offers.find(params[:offer]) if params[:offer].present?
|
||||
end
|
||||
|
||||
def find_transfer_sources_for_admin
|
||||
return unless controller.send :admin?
|
||||
[organization.account] + organization.member_accounts.where("members.active is true")
|
||||
end
|
||||
|
||||
# TODO: Tighlty coupld with Transfer#make_movements. You don't notice that
|
||||
# model depends on this until it fails
|
||||
def destination
|
||||
user.members.find_by(organization: organization).account.id
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user