Hello, Can someone tell me if there''s an easy way to make this statement in my .rhtml file... <%= page.updated_on %> ...show a friendlier date? Like, "2 days ago" or "one week ago"? It''s just the standard database field that Rails maintains for me, I''m just trying to show it more friendly-like in my list views. Much thanks, in advance, for everything that you do. Kindest regards, Raymond
On Friday, April 22, 2005, 10:24:02 AM, Raymond wrote:> ...show a friendlier date? Like, "2 days ago" or "one week ago"?Wrote this about a week ago, along with a date parser; has many unit tests if anyone wants to include it in a general library. Improvements welcome. Public domain. Don''t know of an existing Ruby solution. Cheers, Gavin require ''date'' # # Renders a Date object into a human-interesting form. Gives accurate readings up to 12 # days, then rough ideas (e.g. 4-5 weeks, 6 months). When something''s that far into the # future, we don''t need accurate information. # # The different outputs for the <tt>render_date</tt> method are: # * accurate days # * today # * tomorrow # * 2 days ... 12 days # * weeks # * 2 weeks (give or take a day) # * 2-3 weeks # * 3 weeks (give or take a day) # * 3-4 weeks # * ... # * 6-7 weeks # * months # * 2 months (give or take a week) # * 2-3 months # * 3 months (give or take a week) # * ... # * 5-6 months # * 6 months # * 7 months (no more in-between periods) # * ... # * 12 months (give a week) # * years # * 1 year (give or take 2 months) # * 1-2 years # * 2 years (give or take 2 months) # * 2-3 years # * ... # * 5 years # * 6 years (no more in-between periods) # * 7 years # * ... # # Another method, <tt>render_ndays</tt> gives a more basic output (no "today" etc., or # "ago"). It''s simply giving a more human-interesting rendition of a number of days. # class DateRenderer # # Render a string describing the date in terms of its distance from today. Example return # values: # # today # yesterday # 7 weeks # 3 months ago # 1-2 years # # Parameter: +date+ (a Date object) # def self.render_date(date, today=nil) today ||= Date.today diff_in_days = (date - today).abs.to_i in_the_past = (date < today) ago = (in_the_past ? '' ago'' : '''') case diff_in_days when 0 then "today" when 1 then (in_the_past ? ''yesterday'' : ''tomorrow'') else render_ndays(diff_in_days) + ago end end class << self alias_method :render, :render_date end # # Return a string describing the given number of days. # # render_ndays(0) # -> 0 days # render_ndays(1) # -> 1 day # render_ndays(16) # -> 2-3 weeks # render_ndays(21) # -> 3 weeks # render_ndays(65) # -> 2-3 months # def self.render_ndays(ndays) case d = ndays.abs when 0 then return ''0 days'' when 1 then return ''1 day'' when 2..12 then return "#{d} days" when 13..54 then return "#{weeks(d)} weeks" when 55..182 then return "#{months(d, :fuzzy)} months" when 183..365+7 then return "#{months(d, :round)} months" when 365+8..365*5 then return "#{y = years(d, :fuzzy)} year#{plural(y)}" else return "#{years(d, :round)} years" end end class << self private # Returns, e.g. ''3'', ''5-6'', etc. def weeks(days) nweeks = days / 7 case days % 7 when 0,1 then return nweeks.to_s when 6 then return (nweeks+1).to_s else return "#{nweeks}-#{nweeks+1}" end end DAYS_PER_MONTH = 30.4375 # 365.25 / 12.0 def months(days, output=:fuzzy) if output == :fuzzy nmonths, remainder = days.divmod(DAYS_PER_MONTH) nmonths = nmonths.to_i case remainder.round when 0..6 then return nmonths.to_s when 24..31 then return (nmonths+1).to_s else return "#{nmonths}-#{nmonths+1}" end elsif output == :round nmonths = days / DAYS_PER_MONTH nmonths.round.to_i.to_s # Not fuzzy, so just nearest number of months. else raise ArgumentError, "Bad output value: #{output.inspect}" end end DAYS_PER_YEAR = 365.25 def years(days, output = :fuzzy) if output == :fuzzy nyears, remainder = days.divmod(DAYS_PER_YEAR) nyears = nyears.to_i case remainder.round when 0..60 then return nyears.to_s when 305..366 then return (nyears+1).to_s else return "#{nyears}-#{nyears+1}" end elsif output == :round nyears = days / DAYS_PER_YEAR nyears.round.to_i.to_s # Not fuzzy, so just nearest number of years. else raise ArgumentError, "Bad output value: #{output.inspect}" end end # str could be ''3'', ''5-6'', or whatever. We''re interested in ''1''. def plural(str) str == ''1'' ? '''' : ''s'' end end # class << self end # class DateRenderer
Raymond Brigleb wrote:> Can someone tell me if there''s an easy way to make this statement in my > .rhtml file... > > <%= page.updated_on %> > > ...show a friendlier date?Well, you can use strftime to turn it into a nicer date. I forget the exact syntax, but you can look that up in the API docs, shouldn''t be hard to find. <%= page.updated_on.strftime("format string goes here") %>> Like, "2 days ago" or "one week ago"?Something like that would take a bit more effort. I''m not sure if rails has anything like this available in it''s date handling libraries, but it shouldn''t be too hard to write a helper method that takes a date and returns a "fuzzy" date like you describe. -- Urban Artography http://artography.ath.cx
On 22-apr-05, at 2:24, Raymond Brigleb wrote:> Hello, > > Can someone tell me if there''s an easy way to make this statement in > my .rhtml file... > > <%= page.updated_on %> > > ...show a friendlier date? Like, "2 days ago" or "one week ago"? It''s > just the standard database field that Rails maintains for me, I''m just > trying to show it more friendly-like in my list views. >There was some uber-fancy function like ''distance_of_time_in_words'' showcased somewhere in one of Rails movies, try to scout the docs. I also got a question - how do I switch locales under strftime in Ruby? -- Julian "Julik" Tarkhanov
> Can someone tell me if there''s an easy way to make this statement in > my .rhtml file... > > <%= page.updated_on %> > > ...show a friendlier date? Like, "2 days ago" or "one week ago"? It''sThere are methods in action pack like distance_of_time_in_words and distance_of_time_in_words_to_now. Check http://ap.rubyonrails.com Jason
Thank you all, very helpful replies all around! Turns out what I wanted was indeed this: <%= distance_of_time_in_words_to_now(page.updated_on) %></td> Lovely. Best, Raymond On Apr 21, 2005, at 5:24 PM, Raymond wrote:> Hello, > > Can someone tell me if there''s an easy way to make this statement > in my .rhtml file... > > <%= page.updated_on %> > > ...show a friendlier date? Like, "2 days ago" or "one week ago"? > It''s just the standard database field that Rails maintains for me, > I''m just trying to show it more friendly-like in my list views.
hi all, does anyone have a reference, production VirtualHost configuration that is known to work with rails and apache2 with FastCGI? I can''t find one, and my local config is... not working :( any help much appreciated - I think there should be a page somewhere that I can just copy this stuff off of. my rails app is happy under webbrick. Also, I''m not using .htaccess - all of the config is in the virthost for security. (I copied the content of the default .htaccess into my virthost config). tia, _alex
Josef Pospíšil
2005-Apr-22 10:20 UTC
Re: locales strftime (was: Human-friendly, readable dates?)
In archives there is some thread about it (sorry it''s my own archive so I won''t give you url to list archives, but it''s from 31. 1. 2005) called Library: MLL. Here is the link for library: http://dev.digitpaint.nl/projects/mll. But trac on that site is not working right and whole project seems to sleep. Site claims that strftime is working. I''ve never used it though. If I got it right :-). -- Pepe On 22.4.2005, at 3:14, Julian ''Julik'' Tarkhanov wrote:> > I also got a question - how do I switch locales under strftime in Ruby?
> <VirtualHost *:80> > ServerName rails > DocumentRoot /Users/ocean/Sites/rails/public > ErrorLog /Users/ocean/Sites/rails/log/apache.log > <Directory /Users/ocean/Sites/rails/public> > Options ExecCGI FollowSymLinks > AddHandler cgi-script .cgi > AllowOverride all > Order allow,deny > Allow from all > </Directory> > </VirtualHost>I have all of that in my config - it assumes .haccess, so I copied the rewriterules into my vhost config - right now I get a bad request error. It''s on debian. On OS X I don''t bother with apache, webbrick works beautifully and it''s suuuuuper-convenient. apache2, fcgi, rewrite.... Any other pointers to docs? I''m also using FCGI.> Nice to see you over here at Ruby on Rails! I was wondering how long > you would stick with binarycloud ;-).Heh - I think rails might actually have some things to learn from binarycloud conceptually - but there''s no arguing that ruby kicks the crap out of PHP :) PHP5 is a huge disappointment, to the point that I''m abandoning the language. But before I tell the faithful where to go, I want to do a second "real" project with rails, which I''m doing now. Rails is missing some stuff right now - the setup of the system itself is excellent, but there is still a bit of a lack of documentation on serious production use. Like, using .htaccess is not acceptable for any production box (at least in my mind) - the community is a bit distributed which is understandable for something so new. So far, I am _extremely_ impressed with one exception: the template syntax is a bit ASP-sh*t like. Other than that, documentation good, MVC good + simple, etc. Next we''ll see about auth and more complex things. I haven''t done a huge amount of app-like stuff yet, so I''ll have more to report later.> You might want to give lighttpd a try, though. It''s very fast and has > very low resource requirements, and it works very well with rails and > routes,I''m using apache for more than just rails in this install, so it needs to work :(> since you can specify a default handler for files (rather than having > to piggy-back off of the error handler).how does lighttpd handle certificates? I have a _lot_ of production experience with apache and clients like to hear that name....> Anyway, hope you''re doing well. Please send me questions about Rails; > I''ve been using it for a while now and would love to help bring you up > to speed. I kind of miss talking to you--another film guy doing web > development. :-):)> Oh, you also have to set up the name (e.g rails, above) in NetInfo. I > can send more info if you need it.debian don''t need no stinkin'' LDAP :) -- alex black, founder the turing studio, inc. 510.666.0074 root-16h2cdTTKgpzNNFeSAH1EA@public.gmane.org http://www.turingstudio.com 2600 10th street, suite 635 berkeley, ca 94710
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 alex black wrote: | So far, I am _extremely_ impressed with one exception: the template | syntax is a bit ASP-sh*t like. Other than that, documentation good, MVC *sigh* I''m sorry, but that''s just FUD from the php camp. I fail to see why <?php print $foo ?> is so much better than <%= foo %> <rant> The argument I hear from #php is that <% looks too much like ASP. And looking anything like ASP is bad. That''s like saying terrorists use cars, therefore we shouldn''t, or else we look like terrorists! That''s really immature. ASP got a few things right. In fact, its main problem is simply that it is associated with VB. Apache::ASP in perl is much better. (but still not as good as ruby) The php crowd even throws a fit if you suggest using the short form of <? ?> A major philosophy of RoR is "DRY" - Don''t Repeat Yourself In that line of thought, why would you want to type all that extra stuff. Would you rather it be <?ruby puts foo ?> Yuck!!!!!! </rant> as an aside... I wonder if there is any extensions to Godwin''s Law yet concerning the use of comparisons to terrorists? :) - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCaTzJSIxC85HZHLMRAtyJAJ0Vyk4ukWSA0/ziQ6wb5XXES7JQKgCeK922 C/Wka9VAW6ZitqrvnIQ+aWU=w3Vr -----END PGP SIGNATURE-----
uhm - can someone send me their virtualhost conf which they actually use in production on a real webserver? please please?> *sigh* I''m sorry, but that''s just FUD from the php camp. I fail to > see > why <?php print $foo ?> is so much better than <%= foo %>uhm, php''s native syntax is worse that rails! did you hear me singing its praises? I suggest you take a look at the smarty syntax: http://smarty.php.net/manual/en/language.basic.syntax.php and cheetah, http://www.cheetahtemplate.org/ those are both very, very nice.> The argument I hear from #php is that <% looks too much like ASP. And > looking anything like ASP is bad.<?php is just as stupid. I am not a PHP bigot, I happen to have used it in very large installations - by and large it works, but the language is full of ugly hacks and bad design.> That''s like saying terrorists use cars, therefore we shouldn''t, or else > we look like terrorists!haha, well yeah.> In that line of thought, why would you want to type all that extra > stuff. Would you rather it be <?ruby puts foo ?> Yuck!!!!!!Er "embedded ruby" (I guess that''s what it''s called) is ugly too - not quite as ugly as PHP, about as ugly as ASP. Smarty or cheetah-like syntax is MUCH preferable. Or just pure XML syntax, that''s ok too because the documents will validate with a little DTD magic :)> as an aside... I wonder if there is any extensions to Godwin''s Law yet > concerning the use of comparisons to terrorists? :)haha. if we don''t hear from you again, we''ll know what happened... _a -- alex black, founder the turing studio, inc. 510.666.0074 root-16h2cdTTKgpzNNFeSAH1EA@public.gmane.org http://www.turingstudio.com 2600 10th street, suite 635 berkeley, ca 94710
+1 for Cheetah syntax. One thing that I really dig about erb syntax is you can easily comment a whole block of code out by changing <%= to <%#. I''m perfectly fine with the syntax, personally. The fact that ruby is so elegant allows me to do stuff like: <%= @item.name unless @item.nil? %> or <ul> <% if @items.each do |item| %> <li><%= item.name %></li> <% end.empty? %> <li>No items</li> <% end %> </ul> -- rick http://techno-weenie.net
On 22-apr-05, at 12:20, Josef Pospíšil wrote:> In archives there is some thread about it (sorry it''s my own archive > so I won''t give you url to list archives, but it''s from 31. 1. 2005) > called Library: MLL. Here is the link for library: > http://dev.digitpaint.nl/projects/mll. But trac on that site is not > working right and whole project seems to sleep. Site claims that > strftime is working. > > I''ve never used it though.Hmm.. gotta check it out. This is something that I really need though, so it;s quite pity that Ruby doesn''t support locales out of the box. -- Julian "Julik" Tarkhanov
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 alex black wrote: | uhm, php''s native syntax is worse that rails! did you hear me singing | its praises? I''m sorry, I interpreted your other other statement as such, because it''s what php people say.... and concludes their entire rational argument... :) | Er "embedded ruby" (I guess that''s what it''s called) is ugly too - not | quite as ugly as PHP, about as ugly as ASP. Smarty or cheetah-like | syntax is MUCH preferable. Or just pure XML syntax, that''s ok too | because the documents will validate with a little DTD magic :) Ah yes. Smarty is usefull. The thing with ERB is that there''s no need to learn yet another language to work with rails. Ruby is embedded directly in the html, so you have it''s full power and simplicity at your disposal. I wouldn''t mind seeing some other delimiters for erb, such as the single % at the beginning of a line. Using {} as smarty does would be neat too. But I imagine the difficulty is in parsing it out of the template correctly without tripping of valid ruby code. I''d have to disagree on the XML syntax. XML, XSLT and such are extremely ugly beasts. I place them in the same category as Java and COBOL... languages designed for people who like to type... a lot. |> as an aside... I wonder if there is any extensions to Godwin''s Law yet |> concerning the use of comparisons to terrorists? :) | | haha. if we don''t hear from you again, we''ll know what happened... oops! - -- David Morton Maia Mailguard server side anti-spam/anti-virus solution: http://www.maiamailguard.com -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.5 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCaV44SIxC85HZHLMRAjnQAJ9tF6sVR2PI82eugFk63xDl6+YWBACeLK7E K5fq2Zmnc7UWOjWywkRcEP8=j/c/ -----END PGP SIGNATURE-----
There are the distance_of_time_in_words helper methods. See: http://api.rubyonrails.com/classes/ActionView/Helpers/DateHelper.html#M000378 Kind Regards, Flurin Egger Rob Park wrote:> Raymond Brigleb wrote: > >> Can someone tell me if there''s an easy way to make this statement in >> my .rhtml file... >> >> <%= page.updated_on %> >> >> ...show a friendlier date? > > > Well, you can use strftime to turn it into a nicer date. I forget the > exact syntax, but you can look that up in the API docs, shouldn''t be > hard to find. > > <%= page.updated_on.strftime("format string goes here") %> > >> Like, "2 days ago" or "one week ago"? > > > Something like that would take a bit more effort. I''m not sure if > rails has anything like this available in it''s date handling > libraries, but it shouldn''t be too hard to write a helper method that > takes a date and returns a "fuzzy" date like you describe. >
I''m the developper of MLL, and yes, the project is sleeping at the moment. I''ll fixed the trac though. Kind Regards, Flurin Egger Josef Pospíšil wrote:> In archives there is some thread about it (sorry it''s my own archive > so I won''t give you url to list archives, but it''s from 31. 1. 2005) > called Library: MLL. Here is the link for library: > http://dev.digitpaint.nl/projects/mll. But trac on that site is not > working right and whole project seems to sleep. Site claims that > strftime is working. > > I''ve never used it though. > > If I got it right :-). > > -- > Pepe > On 22.4.2005, at 3:14, Julian ''Julik'' Tarkhanov wrote: > >> >> I also got a question - how do I switch locales under strftime in Ruby? > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks alot, maybe you need some hands and brains to wake up it. I think there is many people which need something like this. And if we add Sasha''s work with gettext we are closer to have some internationalization for rails. If I got it right :-) -- Pepe On 23.4.2005, at 23:32, Flurin Egger wrote:> I''m the developper of MLL, and yes, the project is sleeping at the > moment. I''ll fixed the trac though. > > Kind Regards, > Flurin Egger > > Josef Pospíšil wrote: > >> In archives there is some thread about it (sorry it''s my own archive >> so I won''t give you url to list archives, but it''s from 31. 1. 2005) >> called Library: MLL. Here is the link for library: >> http://dev.digitpaint.nl/projects/mll. But trac on that site is not >> working right and whole project seems to sleep. Site claims that >> strftime is working. >> >> I''ve never used it though. >> >> If I got it right :-). >> >> -- >> Pepe >> On 22.4.2005, at 3:14, Julian ''Julik'' Tarkhanov wrote: >> >>> >>> I also got a question - how do I switch locales under strftime in >>> Ruby? >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
That''s right. I''d be very delighted to get some help! I have some upcomming projects in which I want to take MLL to the next level. Integration of Sasha''s work with gettext is indeed a priority then. Kind regards, Flurin Josef Pospíšil wrote:> Thanks alot, maybe you need some hands and brains to wake up it. I > think there is many people which need something like this. And if we > add Sasha''s work with gettext we are closer to have some > internationalization for rails. > > If I got it right :-) > -- > Pepe > On 23.4.2005, at 23:32, Flurin Egger wrote: > >> I''m the developper of MLL, and yes, the project is sleeping at the >> moment. I''ll fixed the trac though. >> >> Kind Regards, >> Flurin Egger >> >> Josef Pospíšil wrote: >> >>> In archives there is some thread about it (sorry it''s my own archive >>> so I won''t give you url to list archives, but it''s from 31. 1. 2005) >>> called Library: MLL. Here is the link for library: >>> http://dev.digitpaint.nl/projects/mll. But trac on that site is not >>> working right and whole project seems to sleep. Site claims that >>> strftime is working. >>> >>> I''ve never used it though. >>> >>> If I got it right :-). >>> >>> -- >>> Pepe >>> On 22.4.2005, at 3:14, Julian ''Julik'' Tarkhanov wrote: >>> >>>> >>>> I also got a question - how do I switch locales under strftime in >>>> Ruby? >>> >>> >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
So I''m volunteering half day a week (startups are hard on time :-), thus if we can make any plan to work on this, here I''m. So for the first time I''ll try to checkout the source and start to use it :-). Anyone else on roster from noneng :-). Maybe united internationalization before The Big 1.0tm :-)? If I got it right :-) -- Pepe On 25.4.2005, at 17:32, Flurin Egger wrote:> That''s right. I''d be very delighted to get some help! I have some > upcomming projects in which I want to take MLL to the next level. > Integration of Sasha''s work with gettext is indeed a priority then. > > Kind regards, > Flurin > > Josef Pospíšil wrote: > >> Thanks alot, maybe you need some hands and brains to wake up it. I >> think there is many people which need something like this. And if we >> add Sasha''s work with gettext we are closer to have some >> internationalization for rails. >> >> If I got it right :-) >> -- >> Pepe >> On 23.4.2005, at 23:32, Flurin Egger wrote: >> >>> I''m the developper of MLL, and yes, the project is sleeping at the >>> moment. I''ll fixed the trac though. >>> >>> Kind Regards, >>> Flurin Egger >>> >>> Josef Pospíšil wrote: >>> >>>> In archives there is some thread about it (sorry it''s my own >>>> archive so I won''t give you url to list archives, but it''s from 31. >>>> 1. 2005) called Library: MLL. Here is the link for library: >>>> http://dev.digitpaint.nl/projects/mll. But trac on that site is not >>>> working right and whole project seems to sleep. Site claims that >>>> strftime is working. >>>> >>>> I''ve never used it though. >>>> >>>> If I got it right :-). >>>> >>>> -- >>>> Pepe >>>> On 22.4.2005, at 3:14, Julian ''Julik'' Tarkhanov wrote: >>>> >>>>> >>>>> I also got a question - how do I switch locales under strftime in >>>>> Ruby? >>>> >>>> >>>> >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >