From 507a489f35481d633ac967ce7a68bb59a6ecd420 Mon Sep 17 00:00:00 2001 From: Desmond Naranjo Date: Fri, 8 Dec 2023 05:37:51 -0600 Subject: [PATCH] Refactor Resource loading to simplify and centralize behavior (#2084) --- lib/avo.rb | 10 ------- lib/avo/dynamic_router.rb | 34 ++++------------------- lib/avo/resources/resource_manager.rb | 40 +++++++++++---------------- 3 files changed, 21 insertions(+), 63 deletions(-) diff --git a/lib/avo.rb b/lib/avo.rb index f81df3b45c..91d2435159 100644 --- a/lib/avo.rb +++ b/lib/avo.rb @@ -18,16 +18,6 @@ module Avo IN_DEVELOPMENT = ENV["AVO_IN_DEVELOPMENT"] == "1" PACKED = !IN_DEVELOPMENT COOKIES_KEY = "avo" - ENTITIES = { - cards: ["app", "avo", "cards"], - scopes: ["app", "avo", "scopes"], - fields: ["app", "avo", "fields"], - filters: ["app", "avo", "filters"], - actions: ["app", "avo", "actions"], - resources: ["app", "avo", "resources"], - dashboards: ["app", "avo", "dashboards"], - resource_tools: ["app", "avo", "resource_tools"] - } class LicenseVerificationTemperedError < StandardError; end diff --git a/lib/avo/dynamic_router.rb b/lib/avo/dynamic_router.rb index 2e7b58662d..deb8b88bde 100644 --- a/lib/avo/dynamic_router.rb +++ b/lib/avo/dynamic_router.rb @@ -1,39 +1,15 @@ module Avo class DynamicRouter - def self.eager_load(entity) - paths = Avo::ENTITIES.fetch entity - - return unless paths.present? - - pathname = Rails.root.join(*paths) - if pathname.directory? - Rails.autoloaders.main.eager_load_dir(pathname.to_s) - end - end - def self.routes Avo::Engine.routes.draw do scope "resources", as: "resources" do - # Check if the user chose to manually register the resource files. - # If so, eager_load the resources dir. - if Avo.configuration.resources.nil? - Avo::DynamicRouter.eager_load(:resources) unless Rails.application.config.eager_load - end - - Avo::Resources::ResourceManager.fetch_resources - .select do |resource| - resource != :BaseResource - end - .select do |resource| - resource.is_a? Class - end - .map do |resource| - resources resource.route_key do - member do - get :preview - end + Avo::Resources::ResourceManager.fetch_resources.map do |resource| + resources resource.route_key do + member do + get :preview end end + end end end end diff --git a/lib/avo/resources/resource_manager.rb b/lib/avo/resources/resource_manager.rb index bd93257fbf..843171a5fe 100644 --- a/lib/avo/resources/resource_manager.rb +++ b/lib/avo/resources/resource_manager.rb @@ -8,7 +8,6 @@ class ResourceManager class << self def build instance = new - instance.init_resources instance.check_bad_resources instance end @@ -35,37 +34,30 @@ def build # "FishResource", # ] def fetch_resources - resources = if Avo.configuration.resources.nil? - BaseResource.descendants + if Avo.configuration.resources.present? + load_configured_resources else - Avo.configuration.resources + load_resources_namespace end - resources.map do |resource| - if resource.is_a?(Class) - resource - else - resource.to_s.safe_constantize - end + BaseResource.descendants + end + + def load_resources_namespace + Rails.autoloaders.main.eager_load_namespace(Avo::Resources) + end + + def load_configured_resources + raise 'Resources configuration must be an array' unless Avo.configuration.resources.is_a? Array + + Avo.configuration.resources.each do |resource| + resource.to_s.safe_constantize end end end def initialize - @resources = [] - end - - def init_resources - self.resources = self.class.fetch_resources - .reject do |resource| - # Remove the BaseResource. We only need the descendants - resource == Avo::BaseResource - end - .uniq do |klass| - # On invalid resource configuration the resource classes get duplicated in `ObjectSpace` - # We need to de-duplicate them - klass.name - end + @resources = self.class.fetch_resources end def check_bad_resources