简体   繁体   中英

Can a Rails helper_method use yield as if it was invoked in the corresponding view?

I have the following accordion generator which works fine when included directly in the view:

<%
def collapser(name)
  fad = {
    class: 'collapsed',
    data: {toggle: 'collapse', parent: '#accordion_id'},
    href: "##{name}",
    aria: {expanded: 'true', controls: name}
  }
  tag.div(class: 'panel panel-default') do
    tag.div(class: 'panel-heading', role: 'tab') do
      tag.p(class: 'panel-title') do
        tag.a(fad) do
          tag.span do
            t("section.#{name}.title")
          end
        end
      end
    end +
    tag.div(id: name, class: 'panel-collapse collapse', role: 'tabpanel', style: 'height: 0px;', aria: {labelledby: name}, data: {parent: '#accordion_id'}) do
      tag.div(class: 'panel-body') do
        tag.div(class: 'uncode_text_column') do
          yield
        end
      end
    end
  end
end
%>

<%= tag.div(id: 'accordion_id', class: 'panel-group', role: 'tablist', aria: {multiselectable: 'true'}) do %>    
    <%= collapser('example') do %>
      <%= tag.p t('section.example.nub.row1') %>
    <% end %>
<% end %>

Now I wanted to move toward a more clean implementation by:

  • moving collapser to the matching controller
  • make a generic_collapser(name, parent) so
    • it's more broadly accessible in other part of the code base
    • this specific collapser can be implemented as a call to generic_collapeser(name, 'accordion_id')

But I'm stuck with the first step, as I'm not able to handle the context change properly. First, tag is no longer accessible, but simply assigning tag = view_context.tag seems to do the job. However, I didn't found a way to transpose yield statement. I tried the following

  • keep tag.div(class: 'uncode_text_column') { yield }
  • use tag.div(class: 'uncode_text_column') { view_contex{yield} }
  • use tag.div(class: 'uncode_text_column') { view_contex(&block) } , together with def collapser(name, &block)

But none gave the expected result.

Hints toward good resources to better understand view_context , yield and block management would also be welcome, especially tutorial with exercises.

What's this &block in Ruby? And how does it get passed in a method here?

So, the key feature to use here is the capture method. Here is how it was used to solve this issue as it was specified in the question:

<% # in the view %>
<% content[:section_1] = capture do %>
  <%= tag.ul do %>
    <%= tag.li item 1 %>
    <%= tag.li item 2 %>
    <%= tag.li item 3 %>
  <% end %>
<% end %>

<% content[:section_2] = capture do %>
  <%= tag.p 'some paragraphe %>
<% end %>

<% bib = "accordion_#{SecureRandom.hex}" %>
<% title = ->(name){t("section.#{name}.title")} %>
<%= tag.div(id: bib, class: 'panel-group', role: 'tablist', aria: {multiselectable: 'true'}) do %>
  <% content.each do |key, nub| %>
    <%= collapser(key, title[key], nub, bib) %>
  <% end %>
<% end %>

And on the other side

  # Within helpers
  # Returns a collapsable html div usable as an accordion item
  # Params:
  # +name+:: identifier for this div
  # +title+:: text used as title of the collapsable div
  # +content+:: text used as contont of the collapsable div
  # +parent+:: identifier of the parent accordion div
  def collapser(name, title, content, parent)
    link_fad = {
      class: 'collapsed',
      data: {toggle: 'collapse', parent: "##{parent}"},
      href: "##{name}",
      aria: {expanded: 'true', controls: name}
    }
    content_fad = {
      id: name,
      class: 'panel-collapse collapse',
      role: 'tabpanel',
      style: 'height: 0px;',
      aria: {labelledby: name},
      data: {parent: "##{parent}"},
    }
    tag.div(class: 'panel panel-default') do
      tag.div(class: 'panel-heading', role: 'tab') do
        tag.p(class: 'panel-title') do
          tag.a(link_fad) do
            tag.h2 do
              title
            end
          end
        end
      end + # note the plus here so we return a single string with the whole HTML
      tag.div(content_fad) do
        tag.div(class: 'panel-body') do
          tag.div(class: 'uncode_text_column' ) do
            content
          end
        end
      end
    end
  end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM