Register now and start sharing your code snippets.
-->

Using Rails helpers from controllers or anywhere you want

Ruby posted about 1 year ago by christian

Use this snippet if you need to use one of the many Rails helpers in controllers or elsewhere:

   1  class Helpers
   2      include Singleton
   3      include ActionView::Helpers::TextHelper
   4      include ActionView::Helpers::UrlHelper
   5      include ActionView::Helpers::DateHelper
   6      include ActionView::Helpers::TagHelper
   7      include ActionView::Helpers::ActiveRecordHelper
   8    end

For example, to obfuscate email use the mail_to helper and set the encoding to JavaScript:

   1  Helpers.instance.mail_to "me@domain.com", "My email", :encode => "javascript"

Tagged ruby, rails, helpers

Capistrano 2 task for backing up your MySQL production database before each deployment

Ruby posted about 1 year ago by christian

This Capistrano task connects to your production database and dumps the contents to a file. The file is compressed and put in a directory specified with set :backup_dir, ”#{deploy_to}/backups”. This is a slight modification of http://pastie.caboo.se/42574. All credit to court3nay.

   1  task :backup, :roles => :db, :only => { :primary => true } do
   2    filename = "#{backup_dir}/#{application}.dump.#{Time.now.to_f}.sql.bz2"
   3    text = capture "cat #{deploy_to}/current/config/database.yml"
   4    yaml = YAML::load(text)
   5  
   6    on_rollback { run "rm #{filename}" }
   7    run "mysqldump -u #{yaml['production']['username']} -p #{yaml['production']['database']} | bzip2 -c > #{filename}" do |ch, stream, out|
   8      ch.send_data "#{yaml['production']['password']}\n" if out =~ /^Enter password:/
   9    end
  10  end

To automatically backup your data before you deploy a new version add this to config/deploy.rb:

   1  task :before_deploy do
   2      backup
   3    end

To restore the backup run the following command:

   1  mysql database_name -uroot < filename.sql

Tagged ruby, rails, mysql, backup, capistrano

Find the installation directory of a RubyGem package programmatically

Ruby posted about 1 year ago by christian

This snippet prints the full path to the Rails installation directory—the path is retrieved using the RubyGems API:

   1  require 'rubygems'
   2  puts Gem.cache.search('rails').first.full_gem_path
   3  # Specify version number
   4  puts Gem.cache.search('rails', '=1.0.0').first.full_gem_path

Example output:

   1  /usr/lib/ruby/gems/1.8/gems/rails-1.0.0

To find the directory where your gems are hiding, use:

   1  gem env

Tagged ruby, rubygems

Improve page load times by combining JavaScript and CSS files

HTML (Rails) posted about 1 year ago by christian

Add cache => true to combine JavaScript and CSS files and improve page load times.

See changeset 6164 for more information

   1  <%= stylesheet_link_tag     'all',      :cache => true %>
   2      <%= javascript_include_tag  :defaults,  :cache => true %>

Tagged performance, ruby, rails, javascript, css