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 %> <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 %> Greg -- Posted via http://www.ruby-forum.com/. -- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 19 October 2010 22:00, Greg Ma <lists-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
How about something like this: <% if job.published_at > 5.days.ago %> #credit to Colin for catching this <%= render :partial => "job" %> <% else %> <% cache job %> <%= render :partial => "job" %> <% end %> <% end %> # _job.html.erb <p class="title"><%= job.title %></p> <p class="location"><%= job.location %></p> <p><%= job.published_at %></p> I''d also look at turning the logic on that first line into a method def recently_published? job.published_at > 5.days.ago end Luke On 2010-10-19, at 2:00 PM, Greg Ma 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 %> > <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 %> > > Greg > > -- > Posted via http://www.ruby-forum.com/. > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 20 October 2010 03:03, Luke Cowell <lcowell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> How about something like this: > <% if job.published_at > 5.days.ago %> #credit to Colin for catching this > <%= render :partial => "job" %> > <% else %> > <% cache job %> > <%= render :partial => "job" %> > <% end %> > <% end %> > > # _job.html.erb > <p class="title"><%= job.title %></p> > <p class="location"><%= job.location %></p> > <p><%= job.published_at %></p>I think that does not provide the required alternative versions for display of published_at. Colin> > > I''d also look at turning the logic on that first line into a method > > def recently_published? > job.published_at > 5.days.ago > end > > Luke > > On 2010-10-19, at 2:00 PM, Greg Ma 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 %> >> <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 %> >> >> Greg >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> -- >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hi Colin, can you please explain ? Luke On 2010-10-20, at 12:44 AM, Colin Law wrote:> On 20 October 2010 03:03, Luke Cowell <lcowell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> How about something like this: >> <% if job.published_at > 5.days.ago %> #credit to Colin for catching this >> <%= render :partial => "job" %> >> <% else %> >> <% cache job %> >> <%= render :partial => "job" %> >> <% end %> >> <% end %> >> >> # _job.html.erb >> <p class="title"><%= job.title %></p> >> <p class="location"><%= job.location %></p> >> <p><%= job.published_at %></p> > > I think that does not provide the required alternative versions for > display of published_at. > > Colin > >> >> >> I''d also look at turning the logic on that first line into a method >> >> def recently_published? >> job.published_at > 5.days.ago >> end >> >> Luke >> >> On 2010-10-19, at 2:00 PM, Greg Ma 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 %> >>> <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 %> >>> >>> Greg >>> >>> -- >>> Posted via http://www.ruby-forum.com/. >>> >>> -- >>> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >>> >> >> -- >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 20 October 2010 15:49, Luke Cowell <lcowell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Colin, can you please explain ?In the OPs original post he has two ways of rendering job.published_at. For the case where it is recent <%= distance_of_time_in_words(DateTime.now, job.published_at) %> and otherwise just <%= job.published_at %> Colin> > Luke > > On 2010-10-20, at 12:44 AM, Colin Law wrote: > >> On 20 October 2010 03:03, Luke Cowell <lcowell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>> How about something like this: >>> <% if job.published_at > 5.days.ago %> #credit to Colin for catching this >>> <%= render :partial => "job" %> >>> <% else %> >>> <% cache job %> >>> <%= render :partial => "job" %> >>> <% end %> >>> <% end %> >>> >>> # _job.html.erb >>> <p class="title"><%= job.title %></p> >>> <p class="location"><%= job.location %></p> >>> <p><%= job.published_at %></p> >> >> I think that does not provide the required alternative versions for >> display of published_at. >> >> Colin >> >>> >>> >>> I''d also look at turning the logic on that first line into a method >>> >>> def recently_published? >>> job.published_at > 5.days.ago >>> end >>> >>> Luke >>> >>> On 2010-10-19, at 2:00 PM, Greg Ma 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 %> >>>> <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 %> >>>> >>>> Greg >>>> >>>> -- >>>> Posted via http://www.ruby-forum.com/. >>>> >>>> -- >>>> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org. >>>> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >>>> >>> >>> -- >>> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >>> >>> >> >> -- >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
If you make the requirement to be always cached or never cached then I see us having more options. #the partial can worry about how to render itself; keep the main view clean. <%= render :partial => "job" %> # _job.html.erb -- you could optionally wrap this in in a cache block <p class="title"><%= job.title %></p> <p class="location"><%= job.location %></p> <p><%= job_time(job) %></p> <p><%= job.published_at %></p> #job_helper.rb def job_time(job) if job.published_at > 5.days.ago distance_of_time_in_words(DateTime.now, job.published_at) else job.published_at end end Luke On 2010-10-20, at 8:37 AM, Colin Law wrote:> On 20 October 2010 15:49, Luke Cowell <lcowell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hi Colin, can you please explain ? > > In the OPs original post he has two ways of rendering job.published_at. > For the case where it is recent > <%= distance_of_time_in_words(DateTime.now, job.published_at) %> > and otherwise just > <%= job.published_at %> > > Colin > >> >> Luke >> >> On 2010-10-20, at 12:44 AM, Colin Law wrote: >> >>> On 20 October 2010 03:03, Luke Cowell <lcowell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >>>> How about something like this: >>>> <% if job.published_at > 5.days.ago %> #credit to Colin for catching this >>>> <%= render :partial => "job" %> >>>> <% else %> >>>> <% cache job %> >>>> <%= render :partial => "job" %> >>>> <% end %> >>>> <% end %> >>>> >>>> # _job.html.erb >>>> <p class="title"><%= job.title %></p> >>>> <p class="location"><%= job.location %></p> >>>> <p><%= job.published_at %></p> >>> >>> I think that does not provide the required alternative versions for >>> display of published_at. >>> >>> Colin >>> >>>> >>>> >>>> I''d also look at turning the logic on that first line into a method >>>> >>>> def recently_published? >>>> job.published_at > 5.days.ago >>>> end >>>> >>>> Luke >>>> >>>> On 2010-10-19, at 2:00 PM, Greg Ma 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 %> >>>>> <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 %> >>>>> >>>>> Greg >>>>> >>>>> -- >>>>> Posted via http://www.ruby-forum.com/. >>>>> >>>>> -- >>>>> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>>> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>>> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >>>>> >>>> >>>> -- >>>> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>>> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >>>> >>>> >>> >>> -- >>> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >>> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >>> >> >> -- >> 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >> >> > > -- > 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en. >-- 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.