Skip to content

Commit cac90c2

Browse files
committed
Update to allow phi access on new records
1 parent 7f6bd87 commit cac90c2

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

lib/phi_attrs/phi_record.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def allow_phi!(user_id = nil, reason = nil)
109109
#
110110
def allow_phi(user_id = nil, reason = nil, allow_only: nil, &block)
111111
get_phi(user_id, reason, allow_only: allow_only, &block)
112-
return
112+
nil
113113
end
114114

115115
# Enable PHI access for any instance of this class in the block given only
@@ -149,7 +149,7 @@ def get_phi(user_id = nil, reason = nil, allow_only: nil)
149149
allow_only.each { |t| t.allow_phi!(user_id, reason) }
150150
end
151151

152-
return yield
152+
yield
153153
ensure
154154
__instances_with_extended_phi.each do |obj|
155155
if frozen_instances.include?(obj)
@@ -362,7 +362,7 @@ def allow_phi!(user_id = nil, reason = nil)
362362
#
363363
def allow_phi(user_id = nil, reason = nil, &block)
364364
get_phi(user_id, reason, &block)
365-
return
365+
nil
366366
end
367367

368368
# Enable PHI access for a single instance of this class inside the block.
@@ -388,7 +388,7 @@ def get_phi(user_id = nil, reason = nil)
388388
begin
389389
allow_phi!(user_id, reason)
390390

391-
return yield
391+
yield
392392
ensure
393393
new_extensions = @__phi_relations_extended - extended_instances
394394
disallow_last_phi!(preserve_extensions: true)
@@ -487,7 +487,7 @@ def phi_access_reason
487487
# @return [Boolean] whether PHI access is allowed for this instance
488488
#
489489
def phi_allowed?
490-
!phi_context.nil? && phi_context[:phi_access_allowed]
490+
new_record? || (!phi_context.nil? && phi_context[:phi_access_allowed])
491491
end
492492

493493
# Require phi access. Raises an error pre-emptively if it has not been granted.
@@ -672,7 +672,7 @@ def phi_wrap_extension(method_name)
672672
self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block|
673673
relation = send(unwrapped_method, *args, **kwargs, &block)
674674

675-
if phi_allowed? && (relation.present? && relation_klass(relation).included_modules.include?(PhiRecord))
675+
if phi_allowed? && relation.present? && relation_klass(relation).included_modules.include?(PhiRecord)
676676
relations = relation.is_a?(Enumerable) ? relation : [relation]
677677
relations.each do |r|
678678
r.allow_phi!(phi_allowed_by, phi_access_reason) unless @__phi_relations_extended.include?(r)
@@ -720,7 +720,7 @@ def relation_klass(rel)
720720
return rel.klass if rel.is_a?(ActiveRecord::Relation)
721721
return rel.first.class if rel.is_a?(Enumerable)
722722

723-
return rel.class
723+
rel.class
724724
end
725725

726726
def wrapped_extended_name(method_name)

spec/dummy/app/models/patient_info.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ class PatientInfo < ApplicationRecord
1212
exclude_from_phi :last_name
1313
include_in_phi :birthday
1414

15+
validates :first_name, presence: true
16+
1517
def birthday
1618
Time.current
1719
end

spec/dummy/db/schema.rb

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,6 @@
2323
t.index ["patient_info_id"], name: "index_health_records_on_patient_info_id"
2424
end
2525

26-
create_table "missing_attribute_models", force: :cascade do |t|
27-
t.datetime "created_at", precision: nil, null: false
28-
t.datetime "updated_at", precision: nil, null: false
29-
end
30-
31-
create_table "missing_extend_models", force: :cascade do |t|
32-
t.datetime "created_at", precision: nil, null: false
33-
t.datetime "updated_at", precision: nil, null: false
34-
end
35-
3626
create_table "patient_details", force: :cascade do |t|
3727
t.integer "patient_info_id"
3828
t.string "detail"

spec/phi_attrs/phi_record/phi_wrapping_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,15 @@
1515
expect { missing_extend_model }.to raise_error(NameError)
1616
end
1717
end
18+
19+
context 'FactoryBot create with validation on PHI attribute' do
20+
it 'allows access during validation for new records' do
21+
expect { create(:patient_info) }.not_to raise_error
22+
end
23+
24+
it 'requires allow_phi for saved records' do
25+
patient = create(:patient_info)
26+
expect { patient.first_name }.to raise_error(PhiAttrs::Exceptions::PhiAccessException)
27+
end
28+
end
1829
end

0 commit comments

Comments
 (0)