Class: Collavre::ViewExtensions
- Inherits:
-
Object
- Object
- Collavre::ViewExtensions
- Includes:
- Singleton
- Defined in:
- lib/collavre/view_extensions.rb
Overview
ViewExtensions provides a registry for engines to register UI components into named extension slots in core views. This avoids core views needing to check ‘defined?(SomeEngine)` and enables a clean plugin architecture.
Usage in engine initializers:
Collavre::ViewExtensions.register(:creative_toolbar, partial: "collavre_plan/creatives/set_plan_button")
Collavre::ViewExtensions.register(:creative_modals, partial: "collavre/creatives/set_plan_modal")
Collavre::ViewExtensions.register(:navigation_panels, partial: "collavre_plan/shared/navigation/panels")
Usage in core views:
<%= render_extension_slot(:creative_toolbar) %>
<%= render_extension_slot(:creative_modals, locals: { parent: @parent_creative }) %>
Class Method Summary collapse
-
.for_slot(slot) ⇒ Object
Get all registered partials for a slot, sorted by priority.
-
.register(slot, partial:, priority: 100) ⇒ Object
Register a partial into a named slot.
-
.reset! ⇒ Object
Clear all registrations (for testing).
-
.unregister(slot, partial:) ⇒ Object
Unregister a partial from a slot.
Instance Method Summary collapse
- #for_slot(slot) ⇒ Object
-
#initialize ⇒ ViewExtensions
constructor
A new instance of ViewExtensions.
- #register(slot, partial:, priority: 100) ⇒ Object
- #reset! ⇒ Object
- #unregister(slot, partial:) ⇒ Object
Constructor Details
#initialize ⇒ ViewExtensions
Returns a new instance of ViewExtensions.
20 21 22 23 |
# File 'lib/collavre/view_extensions.rb', line 20 def initialize @slots = {} @mutex = Mutex.new end |
Class Method Details
.for_slot(slot) ⇒ Object
Get all registered partials for a slot, sorted by priority
39 40 41 |
# File 'lib/collavre/view_extensions.rb', line 39 def self.for_slot(slot) instance.for_slot(slot) end |
.register(slot, partial:, priority: 100) ⇒ Object
Register a partial into a named slot
29 30 31 |
# File 'lib/collavre/view_extensions.rb', line 29 def self.register(slot, partial:, priority: 100) instance.register(slot, partial: partial, priority: priority) end |
.reset! ⇒ Object
Clear all registrations (for testing)
44 45 46 |
# File 'lib/collavre/view_extensions.rb', line 44 def self.reset! instance.reset! end |
.unregister(slot, partial:) ⇒ Object
Unregister a partial from a slot
34 35 36 |
# File 'lib/collavre/view_extensions.rb', line 34 def self.unregister(slot, partial:) instance.unregister(slot, partial: partial) end |
Instance Method Details
#for_slot(slot) ⇒ Object
63 64 65 66 67 |
# File 'lib/collavre/view_extensions.rb', line 63 def for_slot(slot) @mutex.synchronize do (@slots[slot.to_sym] || []).dup end end |
#register(slot, partial:, priority: 100) ⇒ Object
48 49 50 51 52 53 54 55 |
# File 'lib/collavre/view_extensions.rb', line 48 def register(slot, partial:, priority: 100) @mutex.synchronize do @slots[slot.to_sym] ||= [] @slots[slot.to_sym].reject! { |entry| entry[:partial] == partial } @slots[slot.to_sym] << { partial: partial, priority: priority } @slots[slot.to_sym].sort_by! { |entry| entry[:priority] } end end |
#reset! ⇒ Object
69 70 71 72 73 |
# File 'lib/collavre/view_extensions.rb', line 69 def reset! @mutex.synchronize do @slots = {} end end |
#unregister(slot, partial:) ⇒ Object
57 58 59 60 61 |
# File 'lib/collavre/view_extensions.rb', line 57 def unregister(slot, partial:) @mutex.synchronize do @slots[slot.to_sym]&.reject! { |entry| entry[:partial] == partial } end end |