From 4da4ae77daa242a72a3bfd2786a38ceda5f31fca Mon Sep 17 00:00:00 2001 From: Mateusz Kluge Date: Tue, 25 Sep 2018 13:14:47 +0200 Subject: [PATCH 1/2] Exclude associations with invalid models --- lib/rails_erd/domain.rb | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/rails_erd/domain.rb b/lib/rails_erd/domain.rb index f1d93b5..49e3510 100644 --- a/lib/rails_erd/domain.rb +++ b/lib/rails_erd/domain.rb @@ -141,7 +141,10 @@ def check_association_validity(association) if association.options[:polymorphic] entity_name = association.class_name - entity_by_name(entity_name) or raise "polymorphic interface #{entity_name} does not exist" + entity = entity_by_name(entity_name) + if entity.nil? || entity.generalized? + raise "polymorphic interface #{entity_name} does not exist" + end else entity_name = association.klass.name # Raises NameError if the associated class cannot be found. entity_by_name(entity_name) or raise "model #{entity_name} exists, but is not included in domain" From 4ac590306493a7a3e104b4c61359b410ae6d9a57 Mon Sep 17 00:00:00 2001 From: Mateusz Kluge Date: Wed, 12 Dec 2018 21:51:31 +0100 Subject: [PATCH 2/2] Extract method to make codeclimate happy --- lib/rails_erd/domain.rb | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/rails_erd/domain.rb b/lib/rails_erd/domain.rb index 49e3510..ce95209 100644 --- a/lib/rails_erd/domain.rb +++ b/lib/rails_erd/domain.rb @@ -140,11 +140,7 @@ def check_association_validity(association) association.check_validity! if association.options[:polymorphic] - entity_name = association.class_name - entity = entity_by_name(entity_name) - if entity.nil? || entity.generalized? - raise "polymorphic interface #{entity_name} does not exist" - end + check_polymorphic_association_validity(association) else entity_name = association.klass.name # Raises NameError if the associated class cannot be found. entity_by_name(entity_name) or raise "model #{entity_name} exists, but is not included in domain" @@ -153,6 +149,14 @@ def check_association_validity(association) warn "Ignoring invalid association #{association_description(association)} (#{e.message})" end + def check_polymorphic_association_validity(association) + entity_name = association.class_name + entity = entity_by_name(entity_name) + if entity.nil? || entity.generalized? + raise "polymorphic interface #{entity_name} does not exist" + end + end + def association_description(association) "#{association.name.inspect} on #{association.active_record}" end