From 021afdf06ebc6509e62fa434650cecaa977e8336 Mon Sep 17 00:00:00 2001 From: Mario Oprea Date: Thu, 13 Jan 2022 11:52:26 +0200 Subject: [PATCH] Ensure ForestLiana.models holds unique models MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Given the following model hierarchy ``` module Screen class ApplicationRecord < ActiveRecord::Base self.abstract_class = true end end module Screen class Remediation < Screen::ApplicationRecord self.abstract_class = true end end module Screen class SctInRemediation < Screen::Remediation self.table_name = "remediations" end end module Screen class SctOutRemediation < Screen::Remediation self.table_name = "remediations" end end ``` `ForestLiana.models` will include these child models twice, which will cause Forest to generate a JSON schema with duplicated fields for the duplicated models. ```ruby ForestLiana.models.group_by{ |e| e }.select { |k, v| v.size > 1 }.map(&:first) => [Screen::SctInRemediation (call 'Screen::SctInRemediation.connection' to establish a connection), Screen::SctOutRemediation (call 'Screen::SctOutRemediation.connection' to establish a connection), Screen::SddInRemediation (call 'Screen::SddInRemediation.connection' to establish a connection)] ``` ```bash ❯ cat .forestadmin-schema.json | jq '.collections[24].name' "Screen__SctInRemediation" ❯ cat .forestadmin-schema.json | jq '.collections[24].fields[] | .field ' | sort comment" "created_at" "created_at" "id" "id" "status" "status" "updated_at" "updated_at" ``` --- lib/forest_liana/bootstrapper.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/forest_liana/bootstrapper.rb b/lib/forest_liana/bootstrapper.rb index 48810c2a..1e493429 100644 --- a/lib/forest_liana/bootstrapper.rb +++ b/lib/forest_liana/bootstrapper.rb @@ -109,6 +109,7 @@ def analyze_model?(model) def fetch_models ActiveRecord::Base.subclasses.each { |model| fetch_model(model) } + ForestLiana.models.uniq! end def fetch_model(model)