Class: Decidim::Snippets

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/lib/decidim/snippets.rb

Overview

A utility for managing snippets that need to be registered during the view display and displayed in another part of the application. For example, maps can register their snippets when the map is displayed but they need to be added to the <head> section of the document.

Instance Method Summary collapse

Constructor Details

#initializeSnippets

Returns a new instance of Snippets.



9
10
11
# File 'decidim-core/lib/decidim/snippets.rb', line 9

def initialize
  @snippets = {}
end

Instance Method Details

#add(category, *snippet) ⇒ Object



13
14
15
16
# File 'decidim-core/lib/decidim/snippets.rb', line 13

def add(category, *snippet)
  @snippets[category] ||= []
  @snippets[category].push(*snippet.flatten)
end

#any?(category) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'decidim-core/lib/decidim/snippets.rb', line 22

def any?(category)
  self.for(category).present?
end

#display(category) ⇒ Object



26
27
28
# File 'decidim-core/lib/decidim/snippets.rb', line 26

def display(category)
  self.for(category)&.join("\n")&.html_safe
end

#for(category) ⇒ Object



18
19
20
# File 'decidim-core/lib/decidim/snippets.rb', line 18

def for(category)
  @snippets[category]
end