Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/api/v1/event_procedures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def update

def destroy
authorize(event_procedure)
result = EventProcedures::Destroy.result(id: event_procedure.id.to_s)
result = EventProcedures::Destroy.result(id: event_procedure.id.to_s, scope: policy_scope(EventProcedure))

if result.success?
deleted_successfully_render(result.event_procedure)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/hospitals_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def update

def destroy
authorize(hospital)
result = Hospitals::Destroy.result(id: hospital.id.to_s)
result = Hospitals::Destroy.result(id: hospital.id.to_s, scope: policy_scope(Hospital))

if result.success?
deleted_successfully_render(result.hospital)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/medical_shifts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def update
def destroy
authorize(medical_shift)

result = MedicalShifts::Destroy.result(id: medical_shift.id.to_s)
result = MedicalShifts::Destroy.result(id: medical_shift.id.to_s, scope: policy_scope(MedicalShift))

if result.success?
deleted_successfully_render(result.medical_shift)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/patients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def update

def destroy
authorize(patient)
result = Patients::Destroy.result(id: patient.id.to_s)
result = Patients::Destroy.result(id: patient.id.to_s, scope: policy_scope(Patient))

if result.success?
deleted_successfully_render(result.patient)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/procedures_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def update

def destroy
authorize(procedure)
result = Procedures::Destroy.result(id: procedure.id.to_s)
result = Procedures::Destroy.result(id: procedure.id.to_s, scope: policy_scope(Procedure))

if result.success?
deleted_successfully_render(result.procedure)
Expand Down
3 changes: 2 additions & 1 deletion app/operations/event_procedures/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
module EventProcedures
class Destroy < Actor
input :id, type: String
input :scope, type: Enumerable, default: -> { EventProcedure.all }

output :event_procedure, type: EventProcedure

def call
self.event_procedure = EventProcedure.find(id)
self.event_procedure = scope.find(id)

fail!(error: :cannot_destroy) unless event_procedure.destroy
end
Expand Down
3 changes: 2 additions & 1 deletion app/operations/hospitals/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
module Hospitals
class Destroy < Actor
input :id, type: String
input :scope, type: Enumerable, default: -> { Hospital.all }

output :hospital, type: Hospital

def call
self.hospital = Hospital.find(id)
self.hospital = scope.find(id)

fail!(error: :cannot_destroy) unless hospital.destroy
end
Expand Down
3 changes: 2 additions & 1 deletion app/operations/medical_shifts/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
module MedicalShifts
class Destroy < Actor
input :id, type: String
input :scope, type: Enumerable, default: -> { MedicalShift.all }
output :medical_shift, type: MedicalShift

def call
self.medical_shift = MedicalShift.find(id)
self.medical_shift = scope.find(id)

fail!(error: :cannot_destroy) unless medical_shift.destroy
end
Expand Down
3 changes: 2 additions & 1 deletion app/operations/patients/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
module Patients
class Destroy < Actor
input :id, type: String
input :scope, type: Enumerable, default: -> { Patient.all }

output :patient, type: Patient

def call
self.patient = Patient.find(id)
self.patient = scope.find(id)

fail!(error: :cannot_destroy) unless patient.destroy
end
Expand Down
3 changes: 2 additions & 1 deletion app/operations/procedures/destroy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
module Procedures
class Destroy < Actor
input :id, type: String
input :scope, type: Enumerable, default: -> { Procedure.all }

output :procedure, type: Procedure

def call
self.procedure = Procedure.find(id)
self.procedure = scope.find(id)

fail!(error: :cannot_destroy) unless procedure.destroy
end
Expand Down
2 changes: 1 addition & 1 deletion app/pdfs/medical_shifts_report_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def add_item_line(left_text, right_text)
end

def truncate_text(text, length = 35)
text.length > length ? "#..." : text
text.length > length ? "#{text[0, length]}..." : text
end

def item_shift(item)
Expand Down
24 changes: 14 additions & 10 deletions spec/operations/event_procedures/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,31 @@
context "when event_procedure cannot be destroyed" do
let!(:event_procedure) { create(:event_procedure) }

it "is failure" do
allow(EventProcedure).to receive(:find).with(event_procedure.id.to_s).and_return(event_procedure)
allow(event_procedure).to receive(:destroy).and_return(false)
before do
allow_any_instance_of(EventProcedure).to receive(:destroy).and_return(false) # rubocop:disable RSpec/AnyInstance
end

it "is failure" do
expect(described_class.result(id: event_procedure.id.to_s)).to be_failure
end

it "does not destroy event_procedure" do
allow(EventProcedure).to receive(:find).with(event_procedure.id.to_s).and_return(event_procedure)
allow(event_procedure).to receive(:destroy).and_return(false)

expect { described_class.result(id: event_procedure.id.to_s) }.not_to change(EventProcedure, :count)
end

it "returns error message" do
allow(EventProcedure).to receive(:find).with(event_procedure.id.to_s).and_return(event_procedure)
allow(event_procedure).to receive(:destroy).and_return(false)
expect(described_class.result(id: event_procedure.id.to_s).error).to eq(:cannot_destroy)
end
end

result = described_class.result(id: event_procedure.id.to_s)
context "when event_procedure is outside the given scope" do
let!(:event_procedure) { create(:event_procedure) }
let(:empty_scope) { EventProcedure.none }

expect(result.error).to eq(:cannot_destroy)
it "raises ActiveRecord::RecordNotFound" do
expect do
described_class.result(id: event_procedure.id.to_s, scope: empty_scope)
end.to raise_error(ActiveRecord::RecordNotFound)
end
end

Expand Down
24 changes: 15 additions & 9 deletions spec/operations/hospitals/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,34 @@
context "when hospital cannot be destroyed" do
let!(:hospital) { create(:hospital) }

it "is failure" do
allow(Hospital).to receive(:find).with(hospital.id.to_s).and_return(hospital)
allow(hospital).to receive(:destroy).and_return(false)
before do
allow_any_instance_of(Hospital).to receive(:destroy).and_return(false) # rubocop:disable RSpec/AnyInstance
end

it "is failure" do
expect(described_class.result(id: hospital.id.to_s)).to be_failure
end

it "does not destroy hospital" do
allow(Hospital).to receive(:find).with(hospital.id.to_s).and_return(hospital)
allow(hospital).to receive(:destroy).and_return(false)

expect { described_class.result(id: hospital.id.to_s) }.not_to change(Hospital, :count)
end

it "returns error cannot_destroy" do
allow(Hospital).to receive(:find).with(hospital.id.to_s).and_return(hospital)
allow(hospital).to receive(:destroy).and_return(false)

expect(described_class.result(id: hospital.id.to_s).error).to eq(:cannot_destroy)
end
end

context "when hospital is outside the given scope" do
let!(:hospital) { create(:hospital) }
let(:empty_scope) { Hospital.none }

it "raises ActiveRecord::RecordNotFound" do
expect do
described_class.result(id: hospital.id.to_s, scope: empty_scope)
end.to raise_error(ActiveRecord::RecordNotFound)
end
end

context "when hospital with given id does not exist" do
it "raises ActiveRecord::RecordNotFound" do
expect { described_class.result(id: "non-existent-id") }.to raise_error(ActiveRecord::RecordNotFound)
Expand Down
17 changes: 14 additions & 3 deletions spec/operations/medical_shifts/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@
let(:medical_shift) { create(:medical_shift) }

before do
allow(MedicalShift).to receive(:find).with(medical_shift.id.to_s).and_return(medical_shift)
allow(medical_shift).to receive(:destroy).and_return(false)
medical_shift
allow_any_instance_of(MedicalShift).to receive(:destroy).and_return(false) # rubocop:disable RSpec/AnyInstance
end

it { expect(result).to be_failure }
it { expect { result }.not_to change(MedicalShift, :count) }
it { expect(result.error).to eq(:cannot_destroy) }
end

context "when event_procedure with given id doesn't exist" do
context "when medical_shift is outside the given scope" do
let(:medical_shift) { create(:medical_shift) }
let(:empty_scope) { MedicalShift.none }

it "raises ActiveRecord::RecordNotFound" do
expect do
described_class.result(id: medical_shift.id.to_s, scope: empty_scope)
end.to raise_error(ActiveRecord::RecordNotFound)
end
end

context "when medical_shift with given id doesn't exist" do
subject(:result) { described_class.result(id: "nonexistent") }

it { expect { result }.to raise_error(ActiveRecord::RecordNotFound) }
Expand Down
24 changes: 15 additions & 9 deletions spec/operations/patients/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,34 @@
context "when the patient cannot be destroyed" do
let!(:patient) { create(:patient) }

it "is failure" do
allow(Patient).to receive(:find).with(patient.id.to_s).and_return(patient)
allow(patient).to receive(:destroy).and_return(false)
before do
allow_any_instance_of(Patient).to receive(:destroy).and_return(false) # rubocop:disable RSpec/AnyInstance
end

it "is failure" do
expect(described_class.result(id: patient.id.to_s)).to be_failure
end

it "does not destroy the patient" do
allow(Patient).to receive(:find).with(patient.id.to_s).and_return(patient)
allow(patient).to receive(:destroy).and_return(false)

expect { described_class.result(id: patient.id.to_s) }.not_to change(Patient, :count)
end

it "returns error :cannot_destroy" do
allow(Patient).to receive(:find).with(patient.id.to_s).and_return(patient)
allow(patient).to receive(:destroy).and_return(false)

expect(described_class.result(id: patient.id.to_s).error).to eq(:cannot_destroy)
end
end

context "when patient is outside the given scope" do
let!(:patient) { create(:patient) }
let(:empty_scope) { Patient.none }

it "raises ActiveRecord::RecordNotFound" do
expect do
described_class.result(id: patient.id.to_s, scope: empty_scope)
end.to raise_error(ActiveRecord::RecordNotFound)
end
end

context "when the patient with given id does not exist" do
it "raises ActiveRecord::RecordNotFound" do
expect { described_class.result(id: "non-existent-id") }.to raise_error(ActiveRecord::RecordNotFound)
Expand Down
24 changes: 14 additions & 10 deletions spec/operations/procedures/destroy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,31 @@
context "when procedure cannot be destroyed" do
let!(:procedure) { create(:procedure) }

it "is failure" do
allow(Procedure).to receive(:find).with(procedure.id.to_s).and_return(procedure)
allow(procedure).to receive(:destroy).and_return(false)
before do
allow_any_instance_of(Procedure).to receive(:destroy).and_return(false) # rubocop:disable RSpec/AnyInstance
end

it "is failure" do
expect(described_class.result(id: procedure.id.to_s)).to be_failure
end

it "does not destroy procedure" do
allow(Procedure).to receive(:find).with(procedure.id.to_s).and_return(procedure)
allow(procedure).to receive(:destroy).and_return(false)

expect { described_class.result(id: procedure.id.to_s) }.not_to change(Procedure, :count)
end

it "returns error message" do
allow(Procedure).to receive(:find).with(procedure.id.to_s).and_return(procedure)
allow(procedure).to receive(:destroy).and_return(false)
expect(described_class.result(id: procedure.id.to_s).error).to eq(:cannot_destroy)
end
end

result = described_class.result(id: procedure.id.to_s)
context "when procedure is outside the given scope" do
let!(:procedure) { create(:procedure) }
let(:empty_scope) { Procedure.none }

expect(result.error).to eq(:cannot_destroy)
it "raises ActiveRecord::RecordNotFound" do
expect do
described_class.result(id: procedure.id.to_s, scope: empty_scope)
end.to raise_error(ActiveRecord::RecordNotFound)
end
end

Expand Down
33 changes: 33 additions & 0 deletions spec/pdfs/medical_shifts_report_pdf_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,39 @@
end
end

context "when hospital name exceeds 35 characters" do
it "truncates the name with ellipsis" do
long_name = "A" * 36
medical_shift = create(:medical_shift, user_id: user.id, hospital_name: long_name)
pdf = Prawn::Document.new

described_class.new(
pdf: pdf, amount: amount, items: [medical_shift], title: "Plantões", email: user.email
).generate
rendered_pdf = pdf.render
text_analysis = PDF::Inspector::Text.analyze(rendered_pdf)

expect(text_analysis.strings).to include("#{long_name[0, 35]}...")
expect(text_analysis.strings).not_to include(long_name)
end
end

context "when hospital name is within 35 characters" do
it "displays the name without truncation" do
short_name = "A" * 35
medical_shift = create(:medical_shift, user_id: user.id, hospital_name: short_name)
pdf = Prawn::Document.new

described_class.new(
pdf: pdf, amount: amount, items: [medical_shift], title: "Plantões", email: user.email
).generate
rendered_pdf = pdf.render
text_analysis = PDF::Inspector::Text.analyze(rendered_pdf)

expect(text_analysis.strings).to include(short_name)
end
end

context "when hide_values is true" do
it "does not include monetary values in the report" do
pdf = Prawn::Document.new
Expand Down
Loading
Loading