Simple JavaScript countdown timer
This JavaScript displays the days, hours, minutes and seconds left to the given date:
1 function Countdown(then) { 2 3 this.then = then; 4 5 function setElement(id, value) { 6 if (value.length < 2) { 7 value = "0" + value; 8 } 9 10 window.document.getElementById(id).innerHTML = value; 11 } 12 13 function countdown() { 14 now = new Date(); 15 diff = new Date(this.then - now); 16 17 seconds_left = Math.floor(diff.valueOf() / 1000); 18 19 seconds = Math.floor(seconds_left / 1) % 60; 20 minutes = Math.floor(seconds_left / 60) % 60; 21 hours = Math.floor(seconds_left / 3600) % 24; 22 days = Math.floor(seconds_left / 86400) % 86400; 23 24 setElement('countdown-days', days); 25 setElement('countdown-hours', hours); 26 setElement('countdown-minutes', minutes); 27 setElement('countdown-seconds', seconds); 28 29 countdown.timer = setTimeout(countdown, 1000); 30 } 31 32 33 function start() { 34 this.timer = setTimeout(countdown, 1000); 35 } 36 37 start(then); 38 } 39 40 Countdown(new Date("Dec 04 2008 12:00:00"));
Required HTML:
1 <span id="countdown-days"></span> days 2 3 <span id="countdown-hours"></span>:<span id="countdown-minutes"></span>:<span id="countdown-seconds"></span>
Output is for example:
1 23 days 23:00:12
How to automatically ping search engines when your sitemap has changed
I prefer letting cron update sitemaps in the background, and at the end of the script I ping search engines to let them know it’s been updated:
1 # Recreate sitemap goes here 2 3 # Let search engines know about the update 4 [ "http://www.google.com/webmasters/tools/ping?sitemap=http://xxx/sitemap.xml", 5 "http://search.yahooapis.com/SiteExplorerService/V1/ping?sitemap=http://xxx/sitemap.xml", 6 "http://submissions.ask.com/ping?sitemap=http://xxx/sitemap.xml", 7 "http://webmaster.live.com/ping.aspx?siteMap=http://xxx/sitemap.xml" ].each do |url| 8 open(url) do |f| 9 if f.status[0] == "200" 10 puts "Sitemap successfully submitted to #{url}" 11 else 12 puts "Failed to submit sitemap to #{url}" 13 end 14 end 15 end 16
More about sitemaps: http://en.wikipedia.org/wiki/Sitemaps
How to make Rails plugins reloadable
I found this snippet on the Railshacks blog:
1 # Array of plugins that you want to be reloaded on each request 2 reloadable_plugins = ["has_markup"] 3 4 # Remove the plugins from the load_once_paths variable 5 reloadable_plugins.each do |plugin_name| 6 reloadable_path = RAILS_ROOT + "/vendor/plugins/#{plugin_name}/lib" 7 Dependencies.load_once_paths.delete(reloadable_path) 8 end
How to update the ActiveRecord counter_cache magic column
You can use the model.update_counters method to update the counter_cache column. But if you have a million rows it be very fast.
So for large tables it’s best to do it with a query such as this:
1 update categories, (select 2 id as category_id, ifnull(count, 0) as count 3 from categories left join 4 (select category_id, count(id) as count from products group by category_id) as count_table 5 on 6 categories.id = count_table.category_id) as count_table 7 set 8 categories.products_count = count_table.count 9 where 10 categories.id = count_table.category_id
This query updates the count for all rows.
The code needs to be modified for your database design.
How to fix "Mysql::Error: Duplicate entry '2147483647' for key 3: INSERT INTO `xxx`"
2147483647 is the maximum for an integer column in MySQL, so this error probably means you’ve exceeded this limit somewhere in your code.
Rails automatically detects the best type for your columns, so be sure to specify the correct limit when creating the column with migrations:
1 # from activerecord-2.1.1/lib/active_record/connection_adapters/mysql_adapter.rb 2 case limit 3 when 1; 'tinyint' 4 when 2; 'smallint' 5 when 3; 'mediumint' 6 when nil, 4, 11; 'int(11)' # compatibility with MySQL default 7 when 5..8; 'bigint' 8 else raise(ActiveRecordError, "No integer type has byte size #{limit}") 9 end
This Rails migration code would create a big integer column:
1 t.integer :product_id, :null => false, :limit => 8
See the section on Numeric Types in the MySQL documentation for more information.