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

Rails: transform Path to Url

Almost 1 hour of digging before i gave up and built this…
Update: thanks for the improvement @ Will Sulzer

  # abs or /abs -> http://myx.com/abs
  # https://grosser.it/2008/11/24/rails-transform-path-to-url
  def path_to_url(path)
    "#{request.protocol}#{request.host_with_port.sub(/:80$/,"")}/#{path.sub(/^\//,"")}"
  end

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

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