ETag support for collections

I was frustrated to learn that fresh_when() doesn’t support
Enumerable. Since I would think that the bulk of the ETag savings
would be on large index actions, rather than individual show actions,
I wrote a quick-and-dirty helper for application_controller.rb that I
thought others might find helpful, too.

Handle conditional rendering for a single collection. Depends on

unique IDs, so will not work with data from more than one model at

a

time.

def fresh_collection? collection
id_array = []
collection.each { |c| id_array << c.id }
id_array.sort!
fresh_when etag: id_array, public: true
end

There’s probably a better way to do this, especially if you have an
index action that’s pulling data from multiple models, but this was
enough to scratch my immediate itch. Does anyone know of a better
solution?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs