Patch that adds a collection_size local to partials

I often find myself find my self doing something like this:

<%= render :partial => 'products', :collection => @products, :locals => {:collection_size => @products.size} %>

Usually when I need to identify the last item so I can give it a specific class.

I figured it would be much more useful if the collection size was automatically assigned as a local variable in the same way the counter is.

Here's a patch that does just that.

http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/836-assign-collection_size-local-variable-in-partials

It also assigns a counter based on the :as option if it's set.

Tekin Suleyman

It appears another patch that does almost exactly the same thing is also in lighthouse (#772), anyone have any comments?

Here's a patch that does just that.

#836 PATCH: assign collection counter local variable in partials based on the :as option - Ruby on Rails - rails

Tekin Suleyman

I figured it would be much more useful if the collection size was automatically assigned as a local variable in the same way the counter is.

Here's a patch that does just that.

#83 Prototype insertion - deprecation and custom functions - Ruby on Rails - rails

Copied from wontfix reasoning:

I'm really not a fan of this auto-setting of locals. I think setting the counter local is already kinda fishy, but at least it's something that would otherwise be kinda hard. Setting the size of the collection is perfectly doable through current means, so I don't think it's worth the magic spell to do it automatically.

You're of course always free to turn it into a plugin :wink:

You're of course always free to turn it into a plugin :wink:

Fair enough.