Users can upload photos (and possibly videos, music etc. in future)
each of which must be approved by an administrator before it becomes
public. The photos are being displayed in a multitude of views
(search, best photos, user profile, random photo, recently added
photos etc.) of which each (except the photo owner's private view and
some administrative views) must only display photos that have been
approved by an administrator. That means in every of those actions, I
have to specify :conditions => 'approved is true' which is not DRY at
all, which is error prone etc.
Does anyone know of a good solution to this?
Erik
P.S. I've also thought of a solution in which unapproved/rejected
photos reside in a different table, which would also be a little
better performing for most select queries but adds complexity to the
domain model.
[...] must only display photos that have been
approved by an administrator. That means in every of those actions, I
have to specify :conditions => 'approved is true' which is not DRY at
all, which is error prone etc.
Does anyone know of a good solution to this?
My first thought is to add a method to the model:
def find_approved_photos
return find(:all, :conditions => 'approved is true')
end
and then you can reuse Model.find_approved_photos in your controllers. Then
I remembered that rails allows you to use find_all_by_#{column_name}, which
I think might be better, and works just like find. So then:
Model.find_all_by_approved(true)
would give you all approved photos. Change to false for unapproved. Also,
you can still pass find options like:
and so on. Also, if you want, you can omit the all in find_all_by_approved
(ie. find_by_approved(true)) and I think it works like find(:first). Cheers!
I realized that this is, in fact, a problem of modeling the domain:
unapproved photos aren't photos at all in the domain sense, there is
instead
an entity called PotentialPhoto or similar. I realized this when I
noticed that,
when using a boolean "approved" field in the table, PhotosController's
index isn't showing the same list of photos than what is in the
database anymore
which adds unecessary complexity.