Skip to content

Commit

Permalink
Refactor Resource loading to simplify and centralize behavior (#2084)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhnaranjo authored Dec 8, 2023
1 parent c96a5ac commit 507a489
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 63 deletions.
10 changes: 0 additions & 10 deletions lib/avo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 5 additions & 29 deletions lib/avo/dynamic_router.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
40 changes: 16 additions & 24 deletions lib/avo/resources/resource_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class ResourceManager
class << self
def build
instance = new
instance.init_resources
instance.check_bad_resources
instance
end
Expand All @@ -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
Expand Down

0 comments on commit 507a489

Please sign in to comment.