How to Load Javascript Last with Inline JS

Load JS last, or it will halt the browser while loading… AMEN!

You may protest:
But i use some inline javascript and nothing will work, when jQuery / Prototype / Mootolls / … is not loaded already!!

Version 1: content_for :js

#in your body: (HAML example, works for ERB too...)
%h1 My great title
- content_for :js do
  :javascript
    $(function(){
      $('h1').html('inline js rocks!')
    });

#in you footer
  =js_tag 'jquery', 'application', 'whatever'
  yield :js #this will output all content_for blocks...

Instantly better load times, and no need to abandon inline js!!

Version 2: Caching !?
When action/fragment-caching, this can get problematic, since the content_for will not be call, and hence the js will be missing.
This can be fixed by introducing a ‘later’ method, which gets filled with everything that should be done once your js framework is loaded.

#in your views
later(function(){
  $('#xxx').do_something();
})

#in you footer
later(); //call all those methods that have been stacked...

#in your head
later = function(){
  var stack = [], i_ran=0;
  return function(execute_later){
   if(typeof(execute_later)=='undefined'){
     i_ran=1;
     for(var i=0;i<stack.length;i++)stack[i].call();//execute them all
   } else {
     if(i_ran)execute_later.call()//page is loaded already, just execute
     else stack.push(execute_later)//page not finished, store
   }
 };
}()

Compressing a Folder to a Zip Archive with Ruby

Since downloading a .zip is easier then explaining how to git clone something, i packed my programming pearls in ruby book into a nice little .zip file.

Original folderresulting zip

def compress(path)
  gem 'rubyzip'
  require 'zip/zip'
  require 'zip/zipfilesystem'

  path.sub!(%r[/$],'')
  archive = File.join(path,File.basename(path))+'.zip'
  FileUtils.rm archive, :force=>true

  Zip::ZipFile.open(archive, 'w') do |zipfile|
    Dir["#{path}/**/**"].reject{|f|f==archive}.each do |file|
      zipfile.add(file.sub(path+'/',''),file)
    end
  end
end

Simple Meta Tags with MetaOnRails

Simple meta tag rules (from google webmaster tools):

  1. do not add useless meta tags
  2. add site-wide-unique meta tags
  3. add max 10 keywords
  4. in doubt add no meta tags, search engines will grab something

Obey or be penalized.

For simplicity use: MetaOnRails Rails plugin

Usage

#in your head (no not that head...)
=display_meta

#in your views, add unique keywords/description that matter
set_meta(
  :keywords=> [@movie.category, @movie.genre,@user.name]*' ',
  :description=>@movie.description
)

Output

<meta name="description" content="my description" />
<meta name="keywords" content="my,keyword" />

Ruby Simple Http Post without NET::HTTP Complexity

I just got brain damage again while trying to use NET::HTTP, its so ridiculous complex, why is there no simple alternative ?
(you can use open-uri for get, but post/delete/update ?)

  def post(url,data)
    require 'activesupport'
    require 'net/http'
    url = URI.parse(url)
    http = Net::HTTP.new(url.host, url.port)
    if url.scheme == 'https'
      require 'net/https'
      http.use_ssl = true
    end
    resp, data = http.post(url.path, data.to_query)
    raise "POST FAILED:"+resp.inspect unless resp.is_a? Net::HTTPOK or resp.is_a? Net::HTTPFound
    return data
  end

I am willing to make a SimpleHTTP gem if no one comes up with a satisfactory solution 😦

SimpleHTTP::get(url,data)
SimpleHTTP::post(url,data)
SimpleHTTP::delete(url,data)
SimpleHTTP::update(url,data)

response = page content, throw exception when response code not 200

UPDATE:
rest-client seems to be a good solution, although it does not support sending files.

Upgrading Ruby from 1.8.6 to 1.8.7 from Source

UPDATE: Just use RVM or install ruby-enterprise-edition, thats far easier …

Just writing this because it took a lot of time to find a working tutorial
Ubuntu Hardy(8.04) has 1.8.6 installed, and Ibex(8.10) already comes with 1.8.7.

sudo apt-get install build-essential libssl-dev libreadline5-dev zlib1g-dev
wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.7-p72.tar.gz
tar zxvf ruby-1.8.7-*
cd ruby-1.8.7-*
./configure --prefix=/usr/local --with-openssl-dir=/usr --with-readline-dir=/usr --with-zlib-dir=/usr
make
sudo make install

Hope it helps but so far did not work out for me, when using /usr/bin/local/ruby xxx.rb rubygems are not found, and when installed using –prefix=/usr , openssl is broken, since it is the 1.8.6 version and not even manually installing ruby-1.8.7 openssl seemed to work…