module Avo class App class_attribute :app, default: { resources: [], cache_store: nil } class_attribute :fields, default: [] class_attribute :request, default: nil class_attribute :context, default: nil class_attribute :license, default: nil class << self # def load_controllers # BaseResource.descendants.each do |resource| # controller_name = "#{resource.to_s.gsub('Resource', '').pluralize}Controller" # puts controller_name.inspect # # eval < text # Avo::Fields::DateTimeField -> date_time def init_fields Avo::Fields::BaseField.descendants.each do |class_name| next if class_name.to_s == 'BaseField' if class_name.to_s.end_with? 'Field' load_field class_name.get_field_name, class_name end end end def load_field(method_name, klass) self.fields.push( name: method_name, class: klass, ) # Avo::Loaders::FieldsLoader.define_method method_name.to_sym do |*args, &block| # puts ['Avo::Loaders::FieldsLoader.define_method->', args, block.present?].inspect # if block.present? # puts '111->'.inspect # field = klass.new(args[0], **args[1] || {}, &block) # else # puts '222->'.inspect # field = klass.new(args[0], **args[1] || {}) # end # puts field.inspect # self.bag.push field # # field # # if block.present? # # field_class = klass::new(args[0], **args[1] || {}, &block) # # else # # field_class = klass::new(args[0], **args[1] || {}) # # end # # klass_entity.add_field(self, field_class) # end end def init_resources self.app[: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 self.app[:resources] end # Returns the Avo resource by camelized name # # get_resource_by_name('User') => UserResource def get_resource(resource) self.app[:resources].find do |available_resource| "#{resource}Resource".safe_constantize == available_resource.class end end # Returns the Avo resource by singular snake_cased name # # get_resource_by_name('user') => UserResource def get_resource_by_name(name) self.get_resource name.singularize.camelize end # 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| 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| resource.model_class.to_s.pluralize.underscore.gsub('/', '_') == name.to_s end end # 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 = nil) App.get_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 def get_navigation_items(user = nil) get_available_resources(user).select do |resource| resource.model_class.present? end end def draw_routes # We should eager load all the classes so we find all descendants Rails.application.eager_load! Proc.new do BaseResource.descendants .select do |resource| resource != :BaseResource end .map do |resource| if resource.is_a? Class if resource.model_class.present? route_key = resource.model_class.model_name.route_key else route_key = resource.to_s.underscore.gsub('_resource', '').downcase.pluralize.to_sym end resources route_key end end end end end end end