From eb9e53002468ce398dc9fcd53eb1f8dac4b68e55 Mon Sep 17 00:00:00 2001 From: Eric Greer Date: Tue, 26 Nov 2024 14:36:38 -0500 Subject: [PATCH] adding checks if the method is already defined --- lib/phi_attrs/phi_record.rb | 48 ++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/lib/phi_attrs/phi_record.rb b/lib/phi_attrs/phi_record.rb index 46d6e1a..ba6e3af 100644 --- a/lib/phi_attrs/phi_record.rb +++ b/lib/phi_attrs/phi_record.rb @@ -633,23 +633,25 @@ def phi_wrap_method(method_name) wrapped_method = :"__#{method_name}_phi_wrapped" unwrapped_method = :"__#{method_name}_phi_unwrapped" - self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block| - PhiAttrs::Logger.tagged(*phi_log_keys) do - unless phi_allowed? - raise PhiAttrs::Exceptions::PhiAccessException, "Attempted PHI access for #{self.class.name} #{@__phi_user_id}" - end + unless self.class.method_defined?(wrapped_method) + self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block| + PhiAttrs::Logger.tagged(*phi_log_keys) do + unless phi_allowed? + raise PhiAttrs::Exceptions::PhiAccessException, "Attempted PHI access for #{self.class.name} #{@__phi_user_id}" + end - unless all_phi_context_logged? - PhiAttrs::Logger.info("#{self.class.name} access by [#{all_phi_allowed_by}]. Triggered by method: #{method_name}") - set_all_phi_context_logged - end + unless all_phi_context_logged? + PhiAttrs::Logger.info("#{self.class.name} access by [#{all_phi_allowed_by}]. Triggered by method: #{method_name}") + set_all_phi_context_logged + end - send(unwrapped_method, *args, **kwargs, &block) + send(unwrapped_method, *args, **kwargs, &block) + end end end # method_name => wrapped_method => unwrapped_method - self.class.send(:alias_method, unwrapped_method, method_name) + self.class.send(:alias_method, unwrapped_method, method_name) unless self.class.method_defined?(unwrapped_method) self.class.send(:alias_method, method_name, wrapped_method) self.class.__phi_methods_wrapped << method_name @@ -669,23 +671,25 @@ def phi_wrap_extension(method_name) wrapped_method = wrapped_extended_name(method_name) unwrapped_method = unwrapped_extended_name(method_name) - self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block| - relation = send(unwrapped_method, *args, **kwargs, &block) + unless self.class.method_defined?(wrapped_method) + self.class.send(:define_method, wrapped_method) do |*args, **kwargs, &block| + relation = send(unwrapped_method, *args, **kwargs, &block) - if phi_allowed? && (relation.present? && relation_klass(relation).included_modules.include?(PhiRecord)) - relations = relation.is_a?(Enumerable) ? relation : [relation] - relations.each do |r| - r.allow_phi!(phi_allowed_by, phi_access_reason) unless @__phi_relations_extended.include?(r) + if phi_allowed? && (relation.present? && relation_klass(relation).included_modules.include?(PhiRecord)) + relations = relation.is_a?(Enumerable) ? relation : [relation] + relations.each do |r| + r.allow_phi!(phi_allowed_by, phi_access_reason) unless @__phi_relations_extended.include?(r) + end + @__phi_relations_extended.merge(relations) + self.class.__instances_with_extended_phi.add(self) end - @__phi_relations_extended.merge(relations) - self.class.__instances_with_extended_phi.add(self) - end - relation + relation + end end # method_name => wrapped_method => unwrapped_method - self.class.send(:alias_method, unwrapped_method, method_name) + self.class.send(:alias_method, unwrapped_method, method_name) unless self.class.method_defined?(unwrapped_method) self.class.send(:alias_method, method_name, wrapped_method) self.class.__phi_methods_to_extend << method_name