Create a current_supporter to include in controllers

This commit is contained in:
Eric Schultz 2021-01-14 15:05:54 -06:00 committed by Eric Schultz
parent f55a676bbf
commit 9aec278449
6 changed files with 65 additions and 10 deletions

View file

@ -80,12 +80,4 @@ class ApplicationController < ActionController::Base
def after_inactive_sign_up_path_for(_resource)
profile_path(current_user.profile)
end
# /devise config
private
def current_user_id
current_user&.id
end
end

View file

@ -7,6 +7,7 @@ module Controllers::Nonprofit::Authorization
include Controllers::User::Authorization
included do
helper_method :current_nonprofit_user?
private
def authenticate_nonprofit_user!(type: :web)
reject_with_sign_in 'Please sign in' unless current_nonprofit_user?

View file

@ -5,8 +5,6 @@
module Controllers::Nonprofit::Current
extend ActiveSupport::Concern
included do
helper_method :current_nonprofit_user?
def current_nonprofit
@nonprofit = current_nonprofit_without_exception
raise ActionController::RoutingError, 'Nonprofit not found' if @nonprofit.nil?

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
module Controllers::Supporter::Current
include Controllers::Nonprofit::Current
extend ActiveSupport::Concern
included do
def current_supporter
current_nonprofit.supporters.find(params[:supporter_id] || params[:id])
end
end
end

View file

@ -75,5 +75,9 @@ module Controllers::User::Authorization
key = "administered_nonprofit_user_#{current_user_id}_nonprofit"
::Nonprofit.where(id: QueryRoles.host_ids(current_user_id, %i[nonprofit_admin nonprofit_associate])).last
end
def current_user_id
current_user&.id
end
end
end

View file

@ -0,0 +1,47 @@
#frozen_string_literal: true
# License: AGPL-3.0-or-later WITH WTO-AP-3.0-or-later
# Full license explanation at https://github.com/houdiniproject/houdini/blob/master/LICENSE
require 'rails_helper'
describe 'Controllers::Supporter::Current', type: :controller do
let(:nonprofit) { force_create(:nm_justice)}
let(:supporter) { force_create(:supporter)}
class TestController < ApplicationController
include Controllers::User::Authorization
include Controllers::Supporter::Current
end
controller(TestController) do
def index
render json: {
supporter: "supporters: #{current_supporter.id}",
nonprofit: "nonprofit: #{current_nonprofit.id}"
}
end
end
it 'handles situations where we use id' do
nonprofit
supporter
get :index, params: {nonprofit_id: nonprofit.id, id: supporter.id}
expect(JSON::parse(response.body)).to eq({
'supporter' => "supporters: #{supporter.id}",
'nonprofit' => "nonprofit: #{nonprofit.id}"
})
end
it 'handles situations where we use supporter_id' do
nonprofit
supporter
get :index, params: {nonprofit_id: nonprofit.id, supporter_id: supporter.id, id: 1}
expect(JSON::parse(response.body)).to eq({
'supporter' => "supporters: #{supporter.id}",
'nonprofit' => "nonprofit: #{nonprofit.id}"
})
end
end