Helpful Error Messages in-direct from your App

Custom error pages directly from your app are far better than hand-made pages or those that rails gives you by default. But if they use your controllers/helpers they may send a user into a crash-loop (arriving on /500 and boom-> /500…)

So simple add an errors controller, build some templates(name is e500, since” def 500″ will not work), set them to page caching and ping them after each deploy.

#routes.rb as lat rule
  %w[500 404 422].each do |error|
    map.connect error, :controller=>'errors', :action=>"e#{error}"
  end

#errors_controller.rb
class ErrorsController < ActionController::Base
  helper :all
  layout 'blank' #new layout, without dynamic gimmicks
  caches_page 'e500', 'e404', 'e422'

  #no authentification, so just nil
  def current_user
    nil
  end
  helper_method :current_user
end

#e500.html.erb
<h1>BOOM!</h1>

#deploy.rb
task :ping_error_pages do
  %w[500 404 422].each do |error|
    run "wget -O /dev/null localhost/#{error} --quiet"
  end
end

after "deploy:restart", *%w(
  rs:ping_error_pages
  ...
)

MarkDown, Textile, RDoc, choose your foe

Recently i used all 3 of them for various project documentation, and somehow all of the are broken…

MarkDown

Pro:

  • Looks like natural text
  • Simple, mostly elegant syntax

Con:

  • Lists can destroy your layout, try this:
     - test
    
    ### Heading

    The heading will be in your list, thats just wtf… (adding <span></span> before the heading helps)

  • No tables…
  • Some editors truncate the double space used for linebreaks
  • No numbered list, except you add 1. 2. etc yourself

Textile

Con:

  • Looks weird with h2. h3. but thats kind of ok…
  • You need a free line after each heading, which makes the layout look bloated, especially if you got a lot of sub-headings
  • Line break = <br /> very elegant…

RDoc

Pro:

  • You can use your RDoc skills for documenting ruby

Con:

  • No linebreaks, not even <br/> works….
  • to highlight code you have to use <tt>CODE</tt> very elegant…

All

  • You can use html to hack most of the things that do not work, but that somehow defies the purpose…

So my personal favorite is markdown, elegant, and if you know you have to look out for those lists its somewhat ok…

But if possible i would switch to RDoc, since then i would not have to make a mental switch while documenting code.

Id like to hear your opinion, or at best a “no thats wrong, you can do xxx with yyy” 🙂

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