Module: ContentForOnce::Helpers

Defined in:
lib/content_for_once/helpers.rb

Instance Method Summary collapse

Instance Method Details

#content_for_once(name, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/content_for_once/helpers.rb', line 3

def content_for_once(name, &block)
  @contents ||= Hash.new {|h, k| h[k] = '' }
  name = name.to_sym

  @contents[name].concat(capture(&block))
  @contents[name] = @contents[name].scan(/<\w+(?:\s+[^>]+)?\/?>*<\/\w+>|<\w+(?:\s+[^>]+)?\/?>/).uniq.join

  @contents.each do |name, content|
    # ref: https://github.com/rails/rails/blob/master/actionview/lib/action_view/helpers/capture_helper.rb#L149
    _content = @view_flow.get(name).presence || ''
    _content.concat(content.html_safe)
    _content = _content.scan(/<\w+(?:\s+[^>]+)?\/?>*<\/\w+>|<\w+(?:\s+[^>]+)?\/?>/).uniq.join
    @view_flow.set(name, _content)
  end
end