lib/avo/app.rb in avo-1.4.0.pre.1 vs lib/avo/app.rb in avo-1.4.0
- old
+ new
@@ -1,11 +1,9 @@
module Avo
class App
- class_attribute :app, default: {
- resources: [],
- cache_store: nil
- }
+ class_attribute :resources, default: []
+ class_attribute :cache_store, default: nil
class_attribute :fields, default: []
class_attribute :request, default: nil
class_attribute :context, default: nil
class_attribute :license, default: nil
@@ -14,13 +12,13 @@
init_fields
I18n.locale = Avo.configuration.language_code
if Rails.cache.instance_of?(ActiveSupport::Cache::NullStore)
- app[:cache_store] ||= ActiveSupport::Cache::MemoryStore.new
+ self.cache_store ||= ActiveSupport::Cache::MemoryStore.new
else
- app[:cache_store] = Rails.cache
+ self.cache_store = Rails.cache
end
end
def init(request:, context:)
self.request = request
@@ -36,14 +34,10 @@
end
init_resources
end
- def cache_store
- app[:cache_store]
- end
-
# This method will find all fields available in the Avo::Fields namespace and add them to the fields class_variable array
# so later we can instantiate them on our resources.
#
# If the field has their `def_method` set up it will follow that convention, if not it will snake_case the name:
#
@@ -65,30 +59,26 @@
class: klass
)
end
def init_resources
- app[:resources] = BaseResource.descendants
+ self.resources = BaseResource.descendants
.select do |resource|
resource != BaseResource
end
.map do |resource|
if resource.is_a? Class
resource.new
end
end
end
- def get_resources
- app[:resources]
- end
-
# Returns the Avo resource by camelized name
#
# get_resource_by_name('User') => UserResource
def get_resource(resource)
- app[:resources].find do |available_resource|
+ resources.find do |available_resource|
"#{resource}Resource".safe_constantize == available_resource.class
end
end
# Returns the Avo resource by singular snake_cased name
@@ -101,21 +91,21 @@
# Returns the Avo resource by singular snake_cased name
#
# get_resource_by_name('User') => UserResource
# get_resource_by_name(User) => UserResource
def get_resource_by_model_name(name)
- get_resources.find do |resource|
+ resources.find do |resource|
resource.model_class.model_name.name == name.to_s
end
end
# Returns the Avo resource by singular snake_cased name
#
# get_resource_by_controller_name('delayed_backend_active_record_jobs') => DelayedJobResource
# get_resource_by_controller_name('users') => UserResource
def get_resource_by_controller_name(name)
- get_resources.find do |resource|
+ resources.find do |resource|
resource.model_class.to_s.pluralize.underscore.tr("/", "_") == name.to_s
end
end
# Returns the Rails model class by singular snake_cased name
@@ -124,11 +114,10 @@
def get_model_class_by_name(name)
name.to_s.camelize.singularize
end
def get_available_resources(user = nil)
- App.get_resources
- .select do |resource|
+ resources.select do |resource|
Services::AuthorizationService.authorize user, resource.model, Avo.configuration.authorization_methods.stringify_keys["index"], raise_exception: false
end
.sort_by { |r| r.name }
end