Skip to content

Commit 2960a86

Browse files
committed
adding checks if the method is already defined
1 parent 484e4d2 commit 2960a86

File tree

1 file changed

+26
-22
lines changed

1 file changed

+26
-22
lines changed

lib/phi_attrs/phi_record.rb

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -633,23 +633,25 @@ def phi_wrap_method(method_name)
633633
wrapped_method = :"__#{method_name}_phi_wrapped"
634634
unwrapped_method = :"__#{method_name}_phi_unwrapped"
635635

636-
self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block|
637-
PhiAttrs::Logger.tagged(*phi_log_keys) do
638-
unless phi_allowed?
639-
raise PhiAttrs::Exceptions::PhiAccessException, "Attempted PHI access for #{self.class.name} #{@__phi_user_id}"
640-
end
636+
unless self.class.method_defined?(wrapped_method)
637+
self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block|
638+
PhiAttrs::Logger.tagged(*phi_log_keys) do
639+
unless phi_allowed?
640+
raise PhiAttrs::Exceptions::PhiAccessException, "Attempted PHI access for #{self.class.name} #{@__phi_user_id}"
641+
end
641642

642-
unless all_phi_context_logged?
643-
PhiAttrs::Logger.info("#{self.class.name} access by [#{all_phi_allowed_by}]. Triggered by method: #{method_name}")
644-
set_all_phi_context_logged
645-
end
643+
unless all_phi_context_logged?
644+
PhiAttrs::Logger.info("#{self.class.name} access by [#{all_phi_allowed_by}]. Triggered by method: #{method_name}")
645+
set_all_phi_context_logged
646+
end
646647

647-
send(unwrapped_method, *args, **kwargs, &block)
648+
send(unwrapped_method, *args, **kwargs, &block)
649+
end
648650
end
649651
end
650652

651653
# method_name => wrapped_method => unwrapped_method
652-
self.class.send(:alias_method, unwrapped_method, method_name)
654+
self.class.send(:alias_method, unwrapped_method, method_name) unless self.class.method_defined?(unwrapped_method)
653655
self.class.send(:alias_method, method_name, wrapped_method)
654656

655657
self.class.__phi_methods_wrapped << method_name
@@ -669,23 +671,25 @@ def phi_wrap_extension(method_name)
669671
wrapped_method = wrapped_extended_name(method_name)
670672
unwrapped_method = unwrapped_extended_name(method_name)
671673

672-
self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block|
673-
relation = send(unwrapped_method, *args, **kwargs, &block)
674+
unless self.class.method_defined?(wrapped_method)
675+
self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block|
676+
relation = send(unwrapped_method, *args, **kwargs, &block)
674677

675-
if phi_allowed? && (relation.present? && relation_klass(relation).included_modules.include?(PhiRecord))
676-
relations = relation.is_a?(Enumerable) ? relation : [relation]
677-
relations.each do |r|
678-
r.allow_phi!(phi_allowed_by, phi_access_reason) unless @__phi_relations_extended.include?(r)
678+
if phi_allowed? && (relation.present? && relation_klass(relation).included_modules.include?(PhiRecord))
679+
relations = relation.is_a?(Enumerable) ? relation : [relation]
680+
relations.each do |r|
681+
r.allow_phi!(phi_allowed_by, phi_access_reason) unless @__phi_relations_extended.include?(r)
682+
end
683+
@__phi_relations_extended.merge(relations)
684+
self.class.__instances_with_extended_phi.add(self)
679685
end
680-
@__phi_relations_extended.merge(relations)
681-
self.class.__instances_with_extended_phi.add(self)
682-
end
683686

684-
relation
687+
relation
688+
end
685689
end
686690

687691
# method_name => wrapped_method => unwrapped_method
688-
self.class.send(:alias_method, unwrapped_method, method_name)
692+
self.class.send(:alias_method, unwrapped_method, method_name) unless self.class.method_defined?(unwrapped_method)
689693
self.class.send(:alias_method, method_name, wrapped_method)
690694

691695
self.class.__phi_methods_to_extend << method_name

0 commit comments

Comments
 (0)