On 19 October 2010 22:00, Greg Ma <[email protected]> wrote:
> Hi,
>
> I want to cache objects only if they were published at least 5 days ago.
> He is the code I've done, but I don't like because it is not dry...
> How could I improve it?
>
> <% if job.published_at < 5.days.from_now %>

Is that not testing for before 5 days in the future rather than 5 days ago?

>  <p class="title"><%= job.title %></p>
>  <p class="location"><%= job.location%></p>
>  <p><%= distance_of_time_in_words(DateTime.now, job.published_at)
> %></p>
> <% else %>
>  <% cache job %>
>    <p class="title"><%= job.title %></p>
>    <p class="location"><%= job.location %></p>
>    <p><%= job.published_at %></p>
>  <% end %>
> <% end %>

In terms of DRYness you could perform the test and cache or otherwise
and set a variable to the text for published_at, then output the
title, location and pre-determined text.  Still not very pretty
though.

Colin

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to