Move #give_time from users to transfer controller
This duplicates the UsersController#give_time to TransferController#new as it should be by Rails conventions. We aim to stick to REST conventions of the Transfer resource instead of having custom actions on users and organizations controllers.
This commit is contained in:
@ -14,6 +14,23 @@ class TransfersController < ApplicationController
|
||||
redirect_to redirect_target
|
||||
end
|
||||
|
||||
def new
|
||||
@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
|
||||
end
|
||||
|
||||
def delete_reason
|
||||
@transfer = Transfer.find(params[:id])
|
||||
@transfer.update_columns(reason: nil)
|
||||
@ -25,6 +42,26 @@ class TransfersController < ApplicationController
|
||||
|
||||
private
|
||||
|
||||
def find_transfer_sources_for_admin
|
||||
return unless admin?
|
||||
[current_organization.account] +
|
||||
current_organization.member_accounts.where("members.active is true")
|
||||
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 scoped_users
|
||||
current_organization.users
|
||||
end
|
||||
|
||||
def find_source
|
||||
if admin?
|
||||
Account.find(transfer_params[:source])
|
||||
|
||||
@ -31,7 +31,7 @@ Rails.application.routes.draw do
|
||||
|
||||
resources :users, concerns: :accountable, except: :destroy, :path => "members"
|
||||
|
||||
resources :transfers, only: [:create] do
|
||||
resources :transfers, only: [:create, :new] do
|
||||
member do
|
||||
put :delete_reason
|
||||
end
|
||||
|
||||
@ -5,13 +5,82 @@ describe TransfersController do
|
||||
let (:member_admin) { Fabricate(:member, organization: test_organization, manager: true)}
|
||||
let (:member_giver) { Fabricate(:member, organization: test_organization) }
|
||||
let (:member_taker) { Fabricate(:member, organization: test_organization) }
|
||||
|
||||
include_context "stub browser locale"
|
||||
|
||||
before { set_browser_locale('ca') }
|
||||
|
||||
describe '#new' do
|
||||
let(:user) { member_giver.user }
|
||||
let(:params) { { id: user.id } }
|
||||
|
||||
before { login(user) }
|
||||
|
||||
it 'renders the :new template' do
|
||||
expect(get :new, params).to render_template(:new)
|
||||
end
|
||||
|
||||
it 'finds the user' do
|
||||
get :new, params
|
||||
expect(assigns(:user)).to eq(user)
|
||||
end
|
||||
|
||||
it 'finds the destination account' do
|
||||
get :new, params
|
||||
account = user.members.find_by(organization: user.organizations.first).account
|
||||
|
||||
expect(assigns(:destination)).to eq(account.id)
|
||||
end
|
||||
|
||||
it 'finds the transfer source' do
|
||||
get :new, params
|
||||
source = user.members.find_by(organization: user.organizations.first).account.id
|
||||
|
||||
expect(assigns(:source)).to eq(source)
|
||||
end
|
||||
|
||||
context 'when the offer is specified' do
|
||||
let(:offer) { Fabricate(:offer, organization: user.organizations.first) }
|
||||
|
||||
before { params.merge!(offer: offer.id) }
|
||||
|
||||
it 'finds the transfer offer' do
|
||||
get :new, params
|
||||
offer = user.organizations.first.offers.find(params[:offer])
|
||||
|
||||
expect(assigns(:offer)).to eq(offer)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the offer is not specified' do
|
||||
it 'does not find any offer' do
|
||||
get :new, params
|
||||
expect(assigns(:offer)).to be_nil
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the user is admin of the current organization' do
|
||||
let(:user) { member_admin.user }
|
||||
|
||||
it 'finds all accounts in the organization as sources' do
|
||||
get :new, params
|
||||
expect(assigns(:sources)).to contain_exactly(
|
||||
test_organization.account, member_admin.account
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context 'when the user is not admin of the current organization' do
|
||||
it 'does not assign :sources' do
|
||||
get :new, params
|
||||
expect(assigns(:sources)).to be_nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe "POST #create" do
|
||||
before { login(user) }
|
||||
|
||||
|
||||
context "with valid params" do
|
||||
context "with an admin user logged" do
|
||||
|
||||
|
||||
Reference in New Issue
Block a user