-
-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
additional expenses and contact topic answer controllers
- Loading branch information
1 parent
54064b6
commit bbaf3dd
Showing
12 changed files
with
494 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
class AdditionalExpensesController < ApplicationController | ||
def create | ||
@additional_expense = AdditionalExpense.new(additional_expense_params) | ||
authorize @additional_expense | ||
|
||
respond_to do |format| | ||
if @additional_expense.save | ||
format.json { render json: @additional_expense, status: :created } | ||
else | ||
format.json { render json: @additional_expense.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
def destroy | ||
@additional_expense = AdditionalExpense.find(params[:id]) | ||
authorize @additional_expense | ||
|
||
@additional_expense.destroy! | ||
|
||
respond_to do |format| | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
|
||
def additional_expense_params | ||
params.require(:additional_expense) | ||
.permit(:case_contact_id, :other_expense_amount, :other_expenses_describe) | ||
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,32 @@ | ||
class ContactTopicAnswersController < ApplicationController | ||
def create | ||
@contact_topic_answer = ContactTopicAnswer.new(contact_topic_answer_params) | ||
authorize @contact_topic_answer | ||
|
||
respond_to do |format| | ||
if @contact_topic_answer.save | ||
format.json { render json: @contact_topic_answer, status: :created } | ||
else | ||
format.json { render json: @contact_topic_answer.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
def destroy | ||
@contact_topic_answer = ContactTopicAnswer.find(params[:id]) | ||
authorize @contact_topic_answer | ||
|
||
@contact_topic_answer.destroy! | ||
|
||
respond_to do |format| | ||
format.json { head :no_content } | ||
end | ||
end | ||
|
||
private | ||
|
||
def contact_topic_answer_params | ||
params.require(:contact_topic_answer) | ||
.permit(:id, :contact_topic_id, :case_contact_id, :value, :_destroy) | ||
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
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,27 @@ | ||
class AdditionalExpensePolicy < ApplicationPolicy | ||
class Scope < ApplicationPolicy::Scope | ||
def resolve | ||
case user | ||
when CasaAdmin, Supervisor | ||
scope.joins([:case_contact, :casa_case]).where(casa_case: {casa_org_id: user.casa_org.id}) | ||
when Volunteer | ||
scope.where(case_contact: user.case_contacts) | ||
else | ||
scope.none | ||
end | ||
end | ||
end | ||
|
||
def create? | ||
case user | ||
when Volunteer | ||
user.case_contacts.include?(record.case_contact) | ||
when CasaAdmin, Supervisor | ||
same_org? | ||
else | ||
false | ||
end | ||
end | ||
|
||
alias_method :destroy?, :create? | ||
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,27 @@ | ||
class ContactTopicAnswerPolicy < ApplicationPolicy | ||
class Scope < ApplicationPolicy::Scope | ||
def resolve | ||
case user | ||
when CasaAdmin, Supervisor | ||
scope.joins([:case_contact, :casa_case]).where(casa_case: {casa_org_id: user.casa_org&.id}) | ||
when Volunteer | ||
scope.where(case_contact: user.case_contacts) | ||
else | ||
scope.none | ||
end | ||
end | ||
end | ||
|
||
def create? | ||
case user | ||
when Volunteer | ||
user.case_contacts.include?(record.case_contact) | ||
when CasaAdmin, Supervisor | ||
same_org? | ||
else | ||
false | ||
end | ||
end | ||
|
||
alias_method :destroy?, :create? | ||
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
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,109 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe AdditionalExpensePolicy, type: :policy do | ||
let(:casa_org) { create :casa_org } | ||
let(:volunteer) { create :volunteer, :with_single_case, casa_org: } | ||
let(:supervisor) { create :supervisor, casa_org: } | ||
let(:casa_admin) { create :casa_admin, casa_org: } | ||
let(:all_casa_admin) { create :all_casa_admin } | ||
|
||
let(:casa_case) { volunteer.casa_cases.first } | ||
let(:case_contact) { create :case_contact, casa_case:, creator: volunteer } | ||
let!(:additional_expense) { create :additional_expense, case_contact: } | ||
|
||
let(:same_org_volunteer) { create :volunteer, casa_org: } | ||
let!(:same_org_volunteer_case_assignment) { create :case_assignment, volunteer: same_org_volunteer, casa_case: } | ||
|
||
subject { described_class } | ||
|
||
permissions :create?, :destroy? do | ||
it "does not permit a nil user" do | ||
expect(described_class).not_to permit(nil, additional_expense) | ||
end | ||
|
||
it "permits a volunteer assigned to the expense's case contact" do | ||
expect(described_class).to permit(volunteer, additional_expense) | ||
end | ||
|
||
it "does not permit a volunteer who did not create the case contact" do | ||
expect(same_org_volunteer.casa_cases).to include(casa_case) | ||
expect(described_class).not_to permit(same_org_volunteer, additional_expense) | ||
end | ||
|
||
it "permits a supervisor" do | ||
expect(described_class).to permit(supervisor, additional_expense) | ||
end | ||
|
||
it "does not permit a supervisor for a different casa org" do | ||
other_org_supervisor = create :supervisor, casa_org: create(:casa_org) | ||
expect(described_class).not_to permit(other_org_supervisor, additional_expense) | ||
end | ||
|
||
it "permits a casa admin" do | ||
expect(described_class).to permit(casa_admin, additional_expense) | ||
end | ||
|
||
it "does not permit a casa admin for a different casa org" do | ||
other_org_casa_admin = create :casa_admin, casa_org: create(:casa_org) | ||
expect(described_class).not_to permit(other_org_casa_admin, additional_expense) | ||
end | ||
|
||
it "does not permit an all casa admin" do | ||
expect(described_class).not_to permit(all_casa_admin, additional_expense) | ||
end | ||
end | ||
|
||
describe "Scope#resolve" do | ||
let(:same_org_volunteer_case_contact) { create :case_contact, casa_case:, creator: same_org_volunteer } | ||
let!(:same_org_other_volunteer_additional_expense) do | ||
create :additional_expense, case_contact: same_org_volunteer_case_contact | ||
end | ||
|
||
let(:other_volunteer_case_contact) { create :case_contact, casa_case:, creator: other_volunteer } | ||
let!(:other_volunteer_additional_expense) { create :additional_expense, case_contact: other_org_case_contact } | ||
|
||
let(:other_org) { create :casa_org } | ||
let(:other_org_volunteer) { create :volunteer, casa_org: other_org } | ||
let(:other_org_casa_case) { create :casa_case, casa_org: other_org } | ||
let(:other_org_case_contact) { create :case_contact, casa_case: other_org_casa_case, creator: other_org_volunteer } | ||
let!(:other_org_additional_expense) { create :additional_expense, case_contact: other_org_case_contact } | ||
|
||
subject { described_class::Scope.new(user, AdditionalExpense.all).resolve } | ||
|
||
context "when user is a visitor" do | ||
let(:user) { nil } | ||
|
||
it { is_expected.not_to include(additional_expense) } | ||
it { is_expected.not_to include(other_org_additional_expense) } | ||
end | ||
|
||
context "when user is a volunteer" do | ||
let(:user) { volunteer } | ||
|
||
it { is_expected.to include(additional_expense) } | ||
it { is_expected.not_to include(other_volunteer_additional_expense) } | ||
it { is_expected.not_to include(other_org_additional_expense) } | ||
end | ||
|
||
context "when user is a supervisor" do | ||
let(:user) { supervisor } | ||
|
||
it { is_expected.to include(additional_expense) } | ||
it { is_expected.not_to include(other_org_additional_expense) } | ||
end | ||
|
||
context "when user is a casa_admin" do | ||
let(:user) { casa_admin } | ||
|
||
it { is_expected.to include(additional_expense) } | ||
it { is_expected.not_to include(other_org_additional_expense) } | ||
end | ||
|
||
context "when user is an all_casa_admin" do | ||
let(:user) { all_casa_admin } | ||
|
||
it { is_expected.not_to include(additional_expense) } | ||
it { is_expected.not_to include(other_org_additional_expense) } | ||
end | ||
end | ||
end |
Oops, something went wrong.