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

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}
....

All Your Records As CSV, fast and generic

Our admins are always data-greedy, so i provided a method that lets them get detailed data on any search result they like. Be careful to not let normal users access this (hide email addresses etc).

Installation

  • Provide a current_model method that returns which model the controller is working on.
  • Store the objects you want to render in @current_objects.
  • add this block to every index action response (in a DRY way…)

Usage

  • Visit /xxx/anything.csv to get all data in csv
format.csv do
  #collect data
  keys = current_model.new.attributes.keys.sort
  csv_string = FasterCSV.generate do |csv|
    csv << keys
    @current_objects.each do |record|
      csv << record.attributes.sort_by{|k,v|k}.map{|arr|arr[1]}
    end
  end

  #send data
  filename = current_model.to_s.downcase.gsub(/[^0-9a-z]/, "_") + ".csv"
  send_data(csv_string,
    :type => 'text/csv; charset=utf-8; header=present',
    :filename => filename
  )
end