Class: Avo::App
- Inherits:
-
Object
- Object
- Avo::App
- Defined in:
- lib/avo/app.rb
Class Method Summary collapse
- .boot ⇒ Object
- .draw_routes ⇒ Object
- .get_available_resources(user = nil) ⇒ Object
-
.get_model_class_by_name(name) ⇒ Object
Returns the Rails model class by singular snake_cased name.
-
.get_resource(resource) ⇒ Object
Returns the Avo resource by camelized name.
-
.get_resource_by_controller_name(name) ⇒ Object
Returns the Avo resource by singular snake_cased name.
-
.get_resource_by_model_name(name) ⇒ Object
Returns the Avo resource by singular snake_cased name.
-
.get_resource_by_name(name) ⇒ Object
Returns the Avo resource by singular snake_cased name.
-
.get_sidebar_partials ⇒ Object
Insert any partials that we find in app/views/avo/sidebar/items.
- .init(request:, context:, current_user:, root_path:, view_context:) ⇒ Object
-
.init_fields ⇒ Object
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.
- .init_resources ⇒ Object
- .load_field(method_name, klass) ⇒ Object
- .resources_navigation(user = nil) ⇒ Object
Class Method Details
.boot ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/avo/app.rb', line 14 def boot init_fields I18n.locale = Avo.configuration.language_code if Rails.cache.instance_of?(ActiveSupport::Cache::NullStore) self.cache_store ||= ActiveSupport::Cache::MemoryStore.new else self.cache_store = Rails.cache end end |
.draw_routes ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/avo/app.rb', line 159 def draw_routes # We should eager load all the classes so we find all descendants Rails.application.eager_load! proc do BaseResource.descendants .select do |resource| resource != :BaseResource end .select do |resource| resource.is_a? Class end .map do |resource| resources resource.new.model_key end end end |
.get_available_resources(user = nil) ⇒ Object
128 129 130 131 132 133 |
# File 'lib/avo/app.rb', line 128 def get_available_resources(user = nil) resources.select do |resource| Services::AuthorizationService. user, resource.model_class, Avo.configuration..stringify_keys["index"], raise_exception: false end .sort_by { |r| r.name } end |
.get_model_class_by_name(name) ⇒ Object
Returns the Rails model class by singular snake_cased name
get_model_class_by_name('user') => User
124 125 126 |
# File 'lib/avo/app.rb', line 124 def get_model_class_by_name(name) name.to_s.camelize.singularize end |
.get_resource(resource) ⇒ Object
Returns the Avo resource by camelized name
get_resource_by_name('User') => UserResource
88 89 90 91 92 |
# File 'lib/avo/app.rb', line 88 def get_resource(resource) resources.find do |available_resource| "#{resource}Resource".safe_constantize == available_resource.class end end |
.get_resource_by_controller_name(name) ⇒ Object
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
115 116 117 118 119 |
# File 'lib/avo/app.rb', line 115 def get_resource_by_controller_name(name) resources.find do |resource| resource.model_class.to_s.pluralize.underscore.tr("/", "_") == name.to_s end end |
.get_resource_by_model_name(name) ⇒ Object
Returns the Avo resource by singular snake_cased name
get_resource_by_name('User') => UserResource get_resource_by_name(User) => UserResource
105 106 107 108 109 |
# File 'lib/avo/app.rb', line 105 def get_resource_by_model_name(name) resources.find do |resource| resource.model_class.model_name.name == name.to_s end end |
.get_resource_by_name(name) ⇒ Object
Returns the Avo resource by singular snake_cased name
get_resource_by_name('user') => UserResource
97 98 99 |
# File 'lib/avo/app.rb', line 97 def get_resource_by_name(name) get_resource name.singularize.camelize end |
.get_sidebar_partials ⇒ Object
Insert any partials that we find in app/views/avo/sidebar/items.
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/avo/app.rb', line 145 def Dir.glob(Rails.root.join("app", "views", "avo", "sidebar", "items", "*.html.erb")) .map do |path| File.basename path end .map do |filename| # remove the leading underscore (_) filename[0] = "" # remove the extension filename.gsub!(".html.erb", "") filename end end |
.init(request:, context:, current_user:, root_path:, view_context:) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/avo/app.rb', line 26 def init(request:, context:, current_user:, root_path:, view_context:) self.request = request self.context = context self.current_user = current_user self.root_path = root_path self.view_context = view_context self.license = Licensing::LicenseManager.new(Licensing::HQ.new(request).response).license # Set the current host for ActiveStorage begin if Rails::VERSION::MAJOR === 6 ActiveStorage::Current.host = request.base_url elsif Rails::VERSION::MAJOR === 7 ActiveStorage::Current. = request.base_url end rescue => exception Rails.logger.debug "[Avo] Failed to set ActiveStorage::Current.url_options, #{exception.inspect}" end init_resources end |
.init_fields ⇒ Object
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:
Avo::Fields::TextField -> text Avo::Fields::DateTimeField -> date_time
56 57 58 59 60 61 62 63 64 |
# File 'lib/avo/app.rb', line 56 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 |
.init_resources ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/avo/app.rb', line 73 def init_resources self.resources = BaseResource.descendants .select do |resource| resource != BaseResource end .map do |resource| if resource.is_a? Class resource.new end end end |
.load_field(method_name, klass) ⇒ Object
66 67 68 69 70 71 |
# File 'lib/avo/app.rb', line 66 def load_field(method_name, klass) fields.push( name: method_name, class: klass ) end |
.resources_navigation(user = nil) ⇒ Object
135 136 137 138 139 140 141 142 |
# File 'lib/avo/app.rb', line 135 def (user = nil) get_available_resources(user).select do |resource| resource.model_class.present? end .select do |resource| resource. end end |