Class: Avo::App
- Inherits:
-
Object
- Object
- Avo::App
- Defined in:
- lib/avo/app/app.rb
Constant Summary collapse
- @@app =
{ root_path: '', tools: [], tool_classes: [], resources: [], field_names: {}, }
Class Method Summary collapse
- .app ⇒ Object
- .get_field_names ⇒ Object
-
.get_model_class_by_name(name) ⇒ Object
This returns the Rails model class by singular snake_cased name.
- .get_resource(resource) ⇒ Object
-
.get_resource_by_name(name) ⇒ Object
This returns the Avo resource by singular snake_cased name.
- .get_resources ⇒ Object
-
.get_resources_navigation ⇒ Object
navigation.join(”) end.
- .init ⇒ Object
-
.init_fields ⇒ Object
This method will take all fields available in the Avo::Fields namespace and create a method for them.
- .init_resources ⇒ Object
- .load_field(method_name, klass) ⇒ Object
Class Method Details
.app ⇒ Object
27 28 29 |
# File 'lib/avo/app/app.rb', line 27 def app @@app end |
.get_field_names ⇒ Object
96 97 98 |
# File 'lib/avo/app/app.rb', line 96 def get_field_names @@app[:field_names] end |
.get_model_class_by_name(name) ⇒ Object
This returns the Rails model class by singular snake_cased name
get_model_class_by_name(‘user’) => User
128 129 130 |
# File 'lib/avo/app/app.rb', line 128 def get_model_class_by_name(name) name.to_s.camelize.singularize end |
.get_resource(resource) ⇒ Object
112 113 114 115 116 |
# File 'lib/avo/app/app.rb', line 112 def get_resource(resource) @@app[:resources].find do |available_resource| "Avo::Resources::#{resource}".safe_constantize == available_resource.class end end |
.get_resource_by_name(name) ⇒ Object
This returns the Avo resource by singular snake_cased name
get_resource_by_name(‘user’) => Avo::Resources::User
121 122 123 |
# File 'lib/avo/app/app.rb', line 121 def get_resource_by_name(name) self.get_resource name.singularize.camelize end |
.get_resources ⇒ Object
108 109 110 |
# File 'lib/avo/app/app.rb', line 108 def get_resources @@app[:resources] end |
.get_resources_navigation ⇒ Object
navigation.join(”) end
147 148 149 |
# File 'lib/avo/app/app.rb', line 147 def App.get_resources.map { |resource| { label: resource.resource_name_plural.humanize, resource_name: resource.url.pluralize } }.to_json.to_s.html_safe end |
.init ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/avo/app/app.rb', line 19 def init @@app[:root_path] = Pathname.new(File.join(__dir__, '..', '..')) # get_tools # init_tools init_fields init_resources end |
.init_fields ⇒ Object
This method will take all fields available in the Avo::Fields namespace and create a method for them.
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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/avo/app/app.rb', line 45 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 |
.init_resources ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/avo/app/app.rb', line 100 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 |
.load_field(method_name, klass) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/avo/app/app.rb', line 79 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 |