trailblazer / cells

View components for Ruby and Rails.
https://trailblazer.to/2.1/docs/cells.html
3.07k stars 236 forks source link

[suggestion] collection counter #392

Open dreyks opened 8 years ago

dreyks commented 8 years ago

Similar to rails providing item_counter variable for render partial: 'blah', collection: items, index could be passed as a parameter for cell method. This simplifies things for something (say javascripts) that has to be rendered only once for entire collection Cell::Collection#call almost has it, just needs to pass that index further

def call(state=:show)
  # original
  # join(@join) { |cell, i| cell.(@method || state) }.html_safe

  # proposed
  join(@join) { |cell, i| cell.(@method || state, i) }.html_safe
end
apotonick commented 8 years ago

In 4.1, this is implemented: https://github.com/apotonick/cells/blob/4d39f994e6dd288367e75d30cec2d30ac7e1aec6/test/public_test.rb#L70

Is that cool?

dreyks commented 8 years ago

so Cell::Collection#join is the public API? that's good then, waiting for that 4.1 to release. Thx

dreyks commented 8 years ago

ok, how is this supposed to look like in an erb template?

<%= cell('item', collection: @item).join do |cell, index| %>
  <% cell.(:show, first_call: index.zero?) %>
<% end.html_safe %>

Am I doing this right? Looks not particularly nice, especially because of the need of html_safe

apotonick commented 8 years ago

The html_safe shouldn't be necessary since that's done via cells-rails automatically. Other than that, what API would you like to have?

dreyks commented 8 years ago

Uhm, that gem does not exist yet, does it?

Other than that, would be nice if index was merged into options, passed to @cell_class#build, though you may consider this being too much magic)

apotonick commented 8 years ago

We actually thought of a "collection context object". We could totally do that, that's not really magic.

dreyks commented 8 years ago

:+1: