Class: Cybertrain::ComponentCollection

Inherits:
Component
  • Object
show all
Defined in:
cybertrain/component.rb

Overview

A component rendered once per item, ViewComponent’s CommentComponent.with_collection(@comments): gen/components.rb writes with_collection for a component whose only required keyword is named after it (comment:), and its build arm answers render component: "comment", collection: @comments with one. Each item is built through Views.component_builder as that keyword, with comment_counter: (0, 1, …) when the component declares it, and the outputs are joined; an empty collection renders “”.

Cybertrain::Views.render_component(CommentComponent.with_collection(comments))

A Component itself, so that a build arm and Views.render_component take it where they take any component.

Instance Method Summary collapse

Methods inherited from Component

#call_view_method, #content, #content=, #view_assigns

Instance Method Details

#render_collection_with(helpers, interp) ⇒ SafeString

Each item’s component, rendered on interp, joined.

Returns:



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'cybertrain/component.rb', line 201

def render_collection_with(helpers, interp)
  out = +""
  i = 0
  while i < @items.size
    kwargs = { "" => [nil, 0, 0.0, "", true][0] }
    kwargs.delete("")
    kwargs[@keyword] = @items[i]
    kwargs["#{@keyword}_counter"] = i if @counter
    # A fresh local, not the ivar: Spinel refuses an ivar's String
    # handed to a proc that could append to it.
    name = "#{@name}"
    built = Views.component_builder.call(name, kwargs)
    case built
    when Cybertrain::Component
      html = built.render_with(helpers, "", interp)
      out << html.to_s
    else raise Component::Unknown, "component_builder returned no component for '#{@name}'"
    end
    i += 1
  end
  SafeString.of(out)
end