Save All Empty Strings as NIL

With the  clear_empty_attributes plugin, all empty string worries are gone!

  • Complicate queries for empty fields (WHERE field IS NULL OR field = '')
  • Use of unless field.blank? (opposed to only if field)
  • Late-detected bugs because most of the time columns were filled or '' and suddenly they are nil
  • Some validations do not support :allow_blank=>true
  • Datebases can handle NULL better & faster than '' (especially when using LIKE)

Install

script/plugin install git://github.com/grosser/clear_empty_attributes.git

Now all empty strings will be saves as NULL.

Migrate

To take care of all other/old models we have to run a migration.
Remove any blank strings/texts from your Models:

rake clear_empty_attributes:clear_all_blank_strings MODELS=User,Movie,...

No More Whitespace Worries — Use HAML for Emails!

Tired of all the erb whitespace and those <%-end-%> madness ?

Switch to HAML for emails!!

How to?

  • remove any leading whitespace with capture and gsub
  • use == for easy string replacement
  • use == for newlines

Example

==Hello #{@user},
Your order has been completed on #{Time.now}.
You may now access the bought items:
==
- content = capture do
  - for item in @order.items
    - if item.shipping?
      We will send it to you soon.
    - else
      please download it here:
      ==#{item.orderable} -- #{polymorphic_url(item.orderable)}
    ==

=content.gsub(/^(  )*/,'')
==
=mail_signature

Optional Atrributes for Partials

I just stumbled about a nice and only a little dirty approach to make partial attributes optional.

Helper

#app/helpers/application_helper.rb
def options;{};end
def item;nil;end

View

render :partial=>'xyz', :item=>@user, :options=>{:show_something=>true}
OR
render :partial=>'xyz'

Inside partial

...
text = options[:show_something] if item
...

Automatic Translations with Autolang for Gettext

Problem

  1. Translation = boring
  2. Translation time = €
  3. Cannot be automated

Solution

  1. Translate everything automatically
  2. Correct any errors

Magic

  1. Use Google Translate API
  2. Remove Namespaces (Movie|Name = Nombre )
  3. Preserve String replacements (hello %{name} = Olla %{name} )

Install

http://github.com/grosser/autolang
Copy autolang.rake OR checkout into lib/tasks(if you are using rails, otherwise place it where rake can find it)

(Non-ruby?: install ruby + rubygems + rake-gem)

Usage

rake autolang:translate L=es POT_FILE=x.pot

Example Output

(commandline output, translations are written to "es.po")
Translating...
register
registro
--------------------------------------------------------------------------------
login
inicio de sesión
--------------------------------------------------------------------------------
Your DVD is being built and will be finished soon.
Su DVD se está construyendo y será terminado pronto.
--------------------------------------------------------------------------------
Invitation sent to %{email}
Invitación enviada a %{email}
....

Invoke Any Rake Task via Capistrano

Just run a rake task without having to setup a special capistrano task.

Usage

cap rake_task:invoke COMMAND="db:migrate" #yes, its a silly example...

Setup

namespace :rake_task do
  task :invoke do
    if ENV['COMMAND'].to_s.strip == ''
      puts "USAGE:   cap rake:invoke COMMAND='db:migrate'"
    else
      run "cd #{current_path} && RAILS_ENV=production sudo rake #{ENV['COMMAND']}"
    end
  end
end