jGrowl in Rails for flash

Posted by kain Sat, 13 Sep 2008 17:58:00 GMT

Install JQuery and jGrowl plugin. Also include I18n.

  def display_flash
    flash_types = [:error, :warning, :notice]

    messages = ((flash_types & flash.keys).collect do |key|
      "$.jGrowl('#{flash[key]}', { header: '#{I18n.t(key, :default => key.to_s)}', theme: '#{key.to_s}'});"
    end.join("\n"))

    if messages.size > 0
      content_tag(:script, :type => "text/javascript") do
        "$(document).ready(function() { #{messages} });"
      end
    else
      ""
    end
  end

i18n url based breadcrumbs in Rails

Posted by kain Sat, 13 Sep 2008 17:50:00 GMT

in application_helper.rb

  # Generate URL-based Breadcrumbs.
  # Based on http://rubysnips.com/url-based-breadcrumbs
  # Modified and adapted for i18n by Claudio Poli (http://www.icoretech.org)
  def show_breadcrumbs(wrapper = "p", separator = " » ")
    r = []
    r << link_to(I18n.t(:home), "/")
    url = request.path.split('?')
    segments = url[0].split('/')
    segments.shift
    segments.each_with_index do |segment, i|
      title = segment.gsub(/-/, ' ').titleize
      title = I18n.t(title.downcase.to_sym, :default => title)
      r << link_to_unless_current(title, "/" + (0..(i)).collect{ |seg| segments[seg] }.join("/"))
    end
    content_tag(wrapper, "#{I18n.t(:path)}: " + r.join(separator), :class => "breadcrumbs")
  end

My attempt to beautify Ruby code in TextMate

Posted by kain Sat, 13 Sep 2008 12:26:00 GMT

This is based on Paul Lutus work, version 2.4

#!/usr/bin/env ruby

=begin
/***************************************************************************
 *   Copyright (C) 2008, Paul Lutus                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/
=end

PVERSION = "Version 2.4, 08/25/2008"

# Modified to work as a TextMate command by Claudio Poli (http://www.icoretech.org)

$tabSize = 2
$tabStr = " "

# indent regexp tests

$indentExp = [
  /^module\b/,
  /^class\b/,
  /^if\b/,
  /(=\s*|^)until\b/,
  /(=\s*|^)for\b/,
  /^unless\b/,
  /(=\s*|^)while\b/,
  /(=\s*|^)begin\b/,
  /(^| )case\b/,
  /\bthen\b/,
  /^rescue\b/,
  /^def\b/,
  /\bdo\b/,
  /^else\b/,
  /^elsif\b/,
  /^ensure\b/,
  /\bwhen\b/,
  /\{[^\}]*$/,
  /\[[^\]]*$/
]

# outdent regexp tests

$outdentExp = [
  /^rescue\b/,
  /^ensure\b/,
  /^elsif\b/,
  /^end\b/,
  /^else\b/,
  /\bwhen\b/,
  /^[^\{]*\}/,
  /^[^\[]*\]/
]

def makeTab(tab)
  return (tab < 0) ? "" : $tabStr * $tabSize * tab
end

def addLine(line,tab)
  line.strip!
  line = makeTab(tab)+line if line.length > 0
  return line + "\n"
end

def beautifyRuby

  comment_block = false
  program_end = false
  multiLine_array = []
  multiLine_str = ""
  tab = 0
  source = STDIN.read
  dest = ""
  source.split("\n").each do |line|
    if(!program_end)
      # detect program end mark
      if(line =~ /^__END__$/)
        program_end = true
      else
        # combine continuing lines
        if(!(line =~ /^\s*#/) && line =~ /[^\\]\\\s*$/)
          multiLine_array.push line
          multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
          next
        end

        # add final line
        if(multiLine_str.length > 0)
          multiLine_array.push line
          multiLine_str += line.sub(/^(.*)\\\s*$/,"\\1")
        end

        tline = ((multiLine_str.length > 0) ? multiLine_str:line).strip
        if(tline =~ /^=begin/)
          comment_block = true
        end
      end
    end
    if(comment_block || program_end)
      # add the line unchanged
      dest += line + "\n"
    else
      comment_line = (tline =~ /^#/)
      if(!comment_line)
        # throw out sequences that will
        # only sow confusion
        while tline.gsub!(/\{[^\{]*?\}/,"")
        end
        while tline.gsub!(/\[[^\[]*?\]/,"")
        end
        while tline.gsub!(/'.*?'/,"")
        end
        while tline.gsub!(/".*?"/,"")
        end
        while tline.gsub!(/\`.*?\`/,"")
        end
        while tline.gsub!(/\([^\(]*?\)/,"")
        end
        while tline.gsub!(/\/.*?\//,"")
        end
        while tline.gsub!(/%r(.).*?\1/,"")
        end
        # delete end-of-line comments
        tline.sub!(/#[^\"]+$/,"")
        # convert quotes
        tline.gsub!(/\\\"/,"'")
        $outdentExp.each do |re|
          if(tline =~ re)
            tab -= 1
            break
          end
        end
      end
      if (multiLine_array.length > 0)
        multiLine_array.each do |ml|
          dest += add_line(ml,tab)
        end
        multiLine_array.clear
        multiLine_str = ""
      else
        dest += addLine(line,tab)
      end
      if(!comment_line)
        $indentExp.each do |re|
          if(tline =~ re && !(tline =~ /\s+end\s*$/))
            tab += 1
            break
          end
        end
      end
    end
    if(tline =~ /^=end/)
      comment_block = false
    end
  end

  STDOUT.write(dest)
  # uncomment this to complain about mismatched blocks
  # if(tab != 0)
  #   STDERR.puts "#{path}: Indentation error: #{tab}"
  # end
end

beautifyRuby

iTunes 8 store arrow links sucks, how to disable them 2

Posted by kain Wed, 10 Sep 2008 01:43:00 GMT

In iTunes 7 there was an option to hide them, in iTunes 8 there isn’t. This sucks. Close iTunes, open a terminal and write:

defaults write com.apple.iTunes show-store-arrow-links -bool FALSE

Concerned about your privacy?

Posted by kain Thu, 04 Sep 2008 08:41:00 GMT

From a slashdot’s article:

Google owns any content you create using its Chrome browser and can filter your Gmail messages if it likes.

Facebook says it can sell its users’ uploaded images as stock photography.

YouTube can keep footage of your kids forever, even after you’ve deleted it from the site.

AOL can ban you for using vulgar language on AIM.

Of course nobody forces you to use those services, you have just to know that’s happening :)

Auto vacuum on mail.app database

Posted by kain Wed, 03 Sep 2008 10:42:00 GMT

As you probably know (this is an old trick) it’s possibile to speed up mail.app by vacuuming it’s database, which is created with sqlite.

There’s also a trick to perform an autovacuum on it, here’s how:

Quit mail.app then open a terminal and issue the following:

sqlite3 ~/Library/Mail/Envelope\ Index ".dump" > EnvelopeIndex.sql

Add

pragma auto_vacuum=1;

to the top of EnvelopeIndex.sql

rm ~/Library/Mail/Envelope\ Index
sqlite3 ~/Library/Mail/Envelope\ Index ".read EnvelopeIndex.sql"

Google Chrome EULA excerpt

Posted by kain Wed, 03 Sep 2008 06:10:00 GMT

11.1 You retain copyright and any other rights you already hold in Content which you submit, post or display on or through, the Services. By submitting, posting or displaying the content you give Google a perpetual, irrevocable, worldwide, royalty-free, and non-exclusive license to reproduce, adapt, modify, translate, publish, publicly perform, publicly display and distribute any Content which you submit, post or display on or through, the Services. This license is for the sole purpose of enabling Google to display, distribute and promote the Services and may be revoked for certain Services as defined in the Additional Terms of those Services.

11.2 You agree that this license includes a right for Google to make such Content available to other companies, organizations or individuals with whom Google has relationships for the provision of syndicated services, and to use such Content in connection with the provision of those services.

11.3 You understand that Google, in performing the required technical steps to provide the Services to our users, may (a) transmit or distribute your Content over various public networks and in various media; and (b) make such changes to your Content as are necessary to conform and adapt that Content to the technical requirements of connecting networks, devices, services or media. You agree that this license shall permit Google to take these actions.

11.4 You confirm and warrant to Google that you have all the rights, power and authority necessary to grant the above license.