Refactor
:my
=>
'code'
Codes
Refactorings
Popular
Best
Submit
Spam
Account
Logout
Login
JavaScript doesn't seem to be activated, expect things to be ugly and sloppy!
Learn How to Create Your Own Programming Language
createyourproglang.com
Recent
Parsing of XML data has high CPU usage
Convert simple Javascript to jQuery plugin
Active Record getting unique records
List the files in a directory without the directory name or the extension
clean the code
ohs system, recruitment software, hr software, oh&s software, human resources software, ohs software
Array parsing in a block
Learning JS
Type Safety
Euler Problem 2 solution
Popular
Parsing of XML data has high CPU usage
List the files in a directory without the directory name or the extension
Convert simple Javascript to jQuery plugin
Active Record getting unique records
Breadth first cartesian product iterator
php refactoring
first BST
Learning JS
Too many if statements
clean the code
Pastable version of
How to make time_ago_in_words cachable ?
<pre class='prettyprint' language='ruby'># In one of your helper module, dump: def cachable_time_ago_in_words(from) js_call = javascript_tag "document.write(DateHelper.timeAgoInWords(#{(from.to_i * 1000).to_json}) + ' ago');" "<noscript>on #{from.to_formatted_s(:long)}</noscript>#{js_call}" end # In your application.js var DateHelper = { timeAgoInWords: function(from) { return this.distanceOfTimeInWords(new Date().getTime(), from); }, distanceOfTimeInWords: function(to, from) { seconds_ago = ((to - from) / 1000); minutes_ago = Math.floor(seconds_ago / 60); if(minutes_ago == 0) { return "less than a minute"; } if(minutes_ago == 1) { return "a minute"; } if(minutes_ago < 45) { return minutes_ago + " minutes"; } if(minutes_ago < 90) { return " about 1 hour"; } hours_ago = Math.round(minutes_ago / 60); if(minutes_ago < 1440) { return "about " + hours_ago + " hours"; } if(minutes_ago < 2880) { return "1 day"; } days_ago = Math.round(minutes_ago / 1440); if(minutes_ago < 43200) { return days_ago + " days"; } if(minutes_ago < 86400) { return "about 1 month"; } months_ago = Math.round(minutes_ago / 43200); if(minutes_ago < 525960) { return months_ago + " months"; } if(minutes_ago < 1051920) { return "about 1 year"; } years_ago = Math.round(minutes_ago / 525960); return "over " + years_ago + " years"; } } # Then in your views use: <%= cachable_time_ago_in_words Time.now %></pre> <a href="http://www.refactormycode.com/codes/7-how-to-make-time_ago_in_words-cachable" style="color:#fff" title="As seen on RefactorMyCode.com"><img alt="Small_logo" src="http://www.refactormycode.com/images/small_logo.gif" style="border:0" /></a>