module Avo class App class_attribute :field_manager class_attribute :cache_store, default: nil class_attribute :logger include ActiveModel::Model attr_accessor :request, :context, :current_user, :view_context attr_accessor :cache_store attr_accessor :resource_manager attr_accessor :dashboard_manager attr_accessor :tool_manager attr_accessor :error_manager class << self def 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 boot init_logger init_fields self.cache_store = get_cache_store ::Avo.plugin_manager.boot_plugins end def get_cache_store if Rails.env.production? case Rails.cache.class.to_s when "ActiveSupport::Cache::MemCacheStore", "ActiveSupport::Cache::RedisCacheStore" Rails.cache else ActiveSupport::Cache.lookup_store(:file_store, "/tmp/cache") end elsif Rails.env.test? Rails.cache else ActiveSupport::Cache.lookup_store(:memory_store) end end def init_logger file_logger = ActiveSupport::Logger.new(Rails.root.join("log", "avo.log")) file_logger.datetime_format = "%Y-%m-%d %H:%M:%S" file_logger.formatter = proc do |severity, time, progname, msg| "[Avo] #{time}: #{msg}\n".tap do |i| puts i end end self.logger = file_logger end def init_fields self.field_manager = Avo::Fields::FieldManager.build end def fields field_manager end def build(request:, context:, current_user:, view_context:) @app || new(request: request, context: context, current_user: current_user, view_context: view_context) end def license Current.license end def instance Current.app end def resources Current.app.resource_manager end def dashboards Current.app.dashboard_manager end def tools Current.app.tool_manager end def errors Current.app.error_manager end # Renerate a dynamic root path using the URIService def root_path(paths: [], query: {}, **args) Avo::Services::URIService.parse(Avo::Current.view_context.avo.root_url.to_s) .append_paths(paths) .append_query(query) .to_s end def mount_path Avo::Engine.routes.find_script_name({}) end def has_main_menu? return false if Avo::App.license.lacks_with_trial(:menu_editor) return false if Avo.configuration.main_menu.nil? true end def has_profile_menu? return false if Avo::App.license.lacks_with_trial(:menu_editor) return false if Avo.configuration.profile_menu.nil? true end def main_menu return unless Avo.plugin_manager.installed?(:avo_menu) # Return empty menu if the app doesn't have the profile menu configured return AvoMenu::Builder.new.build unless has_main_menu? AvoMenu::Builder.parse_menu(&Avo.configuration.main_menu) end def profile_menu return unless Avo.plugin_manager.installed?(:avo_menu) # Return empty menu if the app doesn't have the profile menu configured return AvoMenu::Builder.new.build unless has_profile_menu? AvoMenu::Builder.parse_menu(&Avo.configuration.profile_menu) end end def init self.error_manager = Avo::ErrorManager.build self.resource_manager = Avo::Resources::ResourceManager.build self.tool_manager = Avo::Tools::ToolManager.build end # Renerate a dynamic root path using the URIService def root_path(paths: [], query: {}, **args) Avo::Services::URIService.parse(view_context.avo.root_url.to_s) .append_paths(paths) .append_query(query) .to_s end def mount_path Avo::Engine.routes.find_script_name({}) end def app_status Current.license.valid? end end def self.logger Avo::App.logger end end