class Avo::App
def app
def app @@app end
def get_field_names
def get_field_names @@app[:field_names] end
def get_model_class_by_name(name)
This returns the Rails model class by singular snake_cased name
def get_model_class_by_name(name) name.to_s.camelize.singularize end
def get_resource(resource)
def get_resource(resource) @@app[:resources].find do |available_resource| "Avo::Resources::#{resource}".safe_constantize == available_resource.class end end
def get_resource_by_name(name)
This returns the Avo resource by singular snake_cased name
def get_resource_by_name(name) self.get_resource name.singularize.camelize end
def get_resources
def get_resources @@app[:resources] end
def get_resources_navigation
def get_resources_navigation App.get_resources.map { |resource| { label: resource.resource_name_plural.humanize, resource_name: resource.url.pluralize } }.to_json.to_s.html_safe end
def init
def init @@app[:root_path] = Pathname.new(File.join(__dir__, '..', '..')) # get_tools # init_tools init_fields init_resources end
def init_fields
Avo::Fields::TextField -> text
If the field has their `def_method` set up it will follow that convention, if not it will snake_case the name:
This method will take all fields available in the Avo::Fields namespace and create a method for them.
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 next unless tool_provider.present? tool_provider.boot "Avo::Fields::#{namespace}".safe_constantize.constants.each do |custom_field_class| next unless custom_field_class.to_s.end_with? 'Field' or custom_field_class.to_s == 'Field' field_class = "Avo::Fields::#{namespace}::#{custom_field_class}".safe_constantize method_name = field_class.get_field_name end end if field_class.present? and method_name.present? load_field method_name, field_class end end end
def init_resources
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 load_field(method_name, klass)
def load_field(method_name, klass) @@app[:field_names][method_name] = klass # 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] || {}) end klass_entity.add_field(self, field_class) end end end