Module: Sunrise::WidgetsHelper

Defined in:
app/helpers/sunrise/widgets_helper.rb

Instance Method Summary collapse

Instance Method Details

#render_accordion_widget(content, _args = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/helpers/sunrise/widgets_helper.rb', line 27

def render_accordion_widget(content, _args = {})
  return content unless content.include?('[WIDGET:ACCORDION:START]')

  content.gsub(/\[WIDGET:ACCORDION:START\](.+)\[WIDGET:ACCORDION:END\]/m) do |text|
    items = []
    text.gsub(/\[WIDGET:ACCORDION:ITEM:START\](.+?)\[WIDGET:ACCORDION:ITEM:END\]/m) do |item_text|
      items << {
        title: item_text.gsub(/.*\[WIDGET:ACCORDION:TITLE:START\](.+)\[WIDGET:ACCORDION:TITLE:END\].*/m, '\1'),
        content: item_text.gsub(/.*\[WIDGET:ACCORDION:CONTENT:START\](.+)\[WIDGET:ACCORDION:CONTENT:END\].*/m, '\1')
      }
      ''
    end
    render_cell(:widgets, :accordion, text: text, items: items)
  end
end

#render_content_widget(widget, content, params = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
# File 'app/helpers/sunrise/widgets_helper.rb', line 5

def render_content_widget(widget, content, params = {})
  content.gsub(widget.regexp) do |sub|
    if widget.cells?
      render_cell :widgets, widget.code, matcher: widget.regexp.match(sub), params: params
    else
      render file: widget.file, locals: { matcher: widget.regexp.match(sub), params: params }
    end
  end.html_safe
end

#render_widgets(content, params = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'app/helpers/sunrise/widgets_helper.rb', line 15

def render_widgets(content, params = {})
  return if content.blank?

  ContentWidget.all.each do |widget|
    content = render_content_widget(widget, content, params[widget.code])
  end

  content = render_accordion_widget(content, params)

  content
end