Class: Avo::PluginManager
- Inherits:
-
Object
- Object
- Avo::PluginManager
- Defined in:
- lib/avo/plugin_manager.rb
Instance Attribute Summary collapse
-
#engines ⇒ Object
Returns the value of attribute engines.
-
#plugins ⇒ Object
(also: #all)
readonly
Returns the value of attribute plugins.
Instance Method Summary collapse
- #as_json(*arg) ⇒ Object
-
#initialize ⇒ PluginManager
constructor
A new instance of PluginManager.
- #installed?(name) ⇒ Boolean
- #mount_engine(klass, **options) ⇒ Object
- #register(name, priority: 10) ⇒ Object
- #register_field(method_name, klass) ⇒ Object
-
#register_menu_item(name, &block) ⇒ Object
Register a custom menu DSL method (e.g. ‘form`) contributed by a plugin, so it can be used inside `config.main_menu` and friends.
- #register_resource_tool ⇒ Object
- #register_tool ⇒ Object
- #register_view_type(name, component:, icon:, active_icon:, translation_key: nil) ⇒ Object
- #reset ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize ⇒ PluginManager
Returns a new instance of PluginManager.
8 9 10 11 |
# File 'lib/avo/plugin_manager.rb', line 8 def initialize @plugins = [] @engines = [] end |
Instance Attribute Details
#engines ⇒ Object
Returns the value of attribute engines.
4 5 6 |
# File 'lib/avo/plugin_manager.rb', line 4 def engines @engines end |
#plugins ⇒ Object (readonly) Also known as: all
Returns the value of attribute plugins.
3 4 5 |
# File 'lib/avo/plugin_manager.rb', line 3 def plugins @plugins end |
Instance Method Details
#as_json(*arg) ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/avo/plugin_manager.rb', line 59 def as_json(*arg) plugins.map do |plugin| { klass: plugin.to_s, priority: plugin.priority, } end end |
#installed?(name) ⇒ Boolean
76 77 78 79 80 |
# File 'lib/avo/plugin_manager.rb', line 76 def installed?(name) plugins.any? do |plugin| plugin.name.to_s == name.to_s end end |
#mount_engine(klass, **options) ⇒ Object
82 83 84 |
# File 'lib/avo/plugin_manager.rb', line 82 def mount_engine(klass, **) @engines << {klass:, options:} end |
#register(name, priority: 10) ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/avo/plugin_manager.rb', line 18 def register(name, priority: 10) # Capture the file that called `register` so the plugin can later resolve # the gem it actually ships in. Plugins register under nicknames # (e.g. `:rhino` for `avo-rhino_field`), so the name alone isn't enough. registered_from = caller_locations(1, 1)&.first&.path @plugins << Plugin.new(name:, priority: priority, registered_from: registered_from) end |
#register_field(method_name, klass) ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/avo/plugin_manager.rb', line 31 def register_field(method_name, klass) # Avo.boot method is executed multiple times. # During the first run, it correctly loads descendants of Avo::Fields::Base. # Plugins are then loaded, introducing additional descendants to Avo::Fields::Base. # On subsequent runs, Avo::Fields::Base descendants now include these plugin fields. # This field_name_attribute assign forces the field name to retain the registered name instead of being computed dynamically from the field class. klass.field_name_attribute = method_name Avo.field_manager.load_field method_name, klass end |
#register_menu_item(name, &block) ⇒ Object
Register a custom menu DSL method (e.g. ‘form`) contributed by a plugin, so it can be used inside `config.main_menu` and friends. Delegates to avo-menu’s builder when it’s installed; a no-op otherwise, so plugins can register unconditionally. The block is evaluated in the menu builder’s context, so it can call ‘link`, `resource`, etc. See Avo::Menu::Builder.register_item.
47 48 49 50 51 |
# File 'lib/avo/plugin_manager.rb', line 47 def (name, &block) return unless defined?(Avo::Menu::Builder) Avo::Menu::Builder.register_item(name, &block) end |
#register_resource_tool ⇒ Object
53 54 |
# File 'lib/avo/plugin_manager.rb', line 53 def register_resource_tool end |
#register_tool ⇒ Object
56 57 |
# File 'lib/avo/plugin_manager.rb', line 56 def register_tool end |
#register_view_type(name, component:, icon:, active_icon:, translation_key: nil) ⇒ Object
27 28 29 |
# File 'lib/avo/plugin_manager.rb', line 27 def register_view_type(name, component:, icon:, active_icon:, translation_key: nil) Avo.view_type_manager.register(name, component:, icon:, active_icon:, translation_key:) end |
#reset ⇒ Object
13 14 15 16 |
# File 'lib/avo/plugin_manager.rb', line 13 def reset @plugins = [] @engines = [] end |
#to_s ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/avo/plugin_manager.rb', line 68 def to_s plugins.map do |plugin| plugin.to_s end.join(",") rescue "Failed to fetch plugins." end |