lib/avo/app/app.rb in avo-0.4.10 vs lib/avo/app/app.rb in avo-0.5.0.beta1
- old
+ new
@@ -9,17 +9,18 @@
module Avo
class App
@@app = {
root_path: '',
resources: [],
- field_names: {},
cache_store: nil
}
@@license = nil
+ @@fields = []
class << self
def boot
+ # init_controllers
@@app[:root_path] = Pathname.new(File.join(__dir__, '..', '..'))
init_fields
I18n.locale = Avo.configuration.language_code
if Rails.cache.class == ActiveSupport::Cache::NullStore
@@ -28,43 +29,49 @@
@@app[:cache_store] = Rails.cache
end
end
def init(current_request = nil)
- init_resources
+ # Set the current host for ActiveStorage
+ ActiveStorage::Current.host = current_request.base_url
+
+ init_resources current_request
@@license = LicenseManager.new(HQ.new(current_request).response).license
end
def app
@@app
end
+ def fields
+ @@fields
+ end
+
def license
@@license
end
def cache_store
@@app[:cache_store]
end
- # This method will take all fields available in the Avo::Fields namespace and create a method for them.
+ # This method will find all fields available in the Avo::Fields namespace and add them to a @@fields 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:
#
# Avo::Fields::TextField -> text
- # Avo::Fields::TextDateTime -> date_time
+ # Avo::Fields::DateTimeField -> date_time
def init_fields
Avo::Fields.constants.each do |class_name|
next if class_name.to_s == 'Field'
field_class = method_name = nil
if class_name.to_s.end_with? 'Field'
field_class = "Avo::Fields::#{class_name.to_s}".safe_constantize
method_name = field_class.get_field_name
-
- next if Avo::Resources::Resource.method_defined? method_name.to_sym
else
# Try one level deeper for custom fields
namespace = class_name
tool_provider = "Avo::Fields::#{namespace}::ToolProvider".safe_constantize
@@ -85,85 +92,90 @@
end
end
end
def load_field(method_name, klass)
- @@app[:field_names][method_name] = klass
+ @@fields.push(
+ name: method_name,
+ class: klass,
+ )
+ end
- # Load field to concerned classes
- [Avo::Resources::Resource, Avo::Actions::Action].each do |klass_entity|
- klass_entity.define_singleton_method method_name.to_sym do |*args, &block|
- if block.present?
- field_class = klass::new(args[0], **args[1] || {}, &block)
- else
- field_class = klass::new(args[0], **args[1] || {})
+ def init_resources(request)
+ @@app[:resources] = Avo::Resources.constants
+ .select do |r|
+ r != :Resource
+ end
+ .map do |c|
+ if Avo::Resources.const_get(c).is_a? Class
+ "Avo::Resources::#{c}".safe_constantize.new request
end
-
- klass_entity.add_field(self, field_class)
end
- end
end
- def get_field_names
- @@app[:field_names]
- end
-
- def init_resources
- @@app[:resources] = Avo::Resources.constants.select { |r| r != :Resource }.map do |c|
- if Avo::Resources.const_get(c).is_a? Class
- "Avo::Resources::#{c}".safe_constantize.new
- end
- end
- end
-
def get_resources
@@app[:resources]
end
+ # Returns the Avo resource by camelized name
+ #
+ # get_resource_by_name('User') => Avo::Resources::User
def get_resource(resource)
@@app[:resources].find do |available_resource|
"Avo::Resources::#{resource}".safe_constantize == available_resource.class
end
end
- # This returns the Avo resource by singular snake_cased name
+ # Returns the Avo resource by singular snake_cased name
#
# get_resource_by_name('user') => Avo::Resources::User
def get_resource_by_name(name)
self.get_resource name.singularize.camelize
end
- # This returns the Avo resource by singular snake_cased name
+ # Returns the Avo resource by singular snake_cased name
#
# get_resource_by_name('User') => Avo::Resources::User
# get_resource_by_name(User) => Avo::Resources::User
def get_resource_by_model_name(name)
get_resources.find do |resource|
- resource.model.model_name.name == name.to_s
+ resource.model_class.model_name.name == name.to_s
end
end
- # This returns the Rails model class by singular snake_cased name
+ # Returns the Rails model class by singular snake_cased name
#
# get_model_class_by_name('user') => User
def get_model_class_by_name(name)
name.to_s.camelize.singularize
end
- def get_available_resources(user)
+ def get_available_resources(user = nil)
App.get_resources
- .select { |resource| AuthorizationService::authorize user, resource.model, Avo.configuration.authorization_methods.stringify_keys['index'] }
- .map do |resource|
- {
- label: resource.plural_name.humanize(keep_id_suffix: true),
- resource_name: resource.url.pluralize,
- translation_key: resource.translation_key
- }
+ .select do |resource|
+ AuthorizationService::authorize user, resource.model, Avo.configuration.authorization_methods.stringify_keys['index'], raise_exception: false
end
- .reject { |i| i.blank? }
- .to_json
- .to_s
- .html_safe
+ .sort_by { |r| r.name }
+ end
+
+ def get_navigation_items(user = nil)
+ get_available_resources(user).select do |resource|
+ resource.model_class.present?
+ end
+ end
+
+ def draw_routes
+ Proc.new do
+ Avo::Resources.constants
+ .select do |r|
+ r != :Resource
+ end
+ .map do |r|
+ if Avo::Resources.const_get(r).is_a? Class
+ resources r.to_s.underscore.downcase.pluralize.to_sym
+ end
+ end
+ end
end
end
end
end