<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: rails code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 18:25:02 GMT</pubDate>
    <description>DZone Snippets: rails code</description>
    <item>
      <title>TextMate snippet for load_model methods in Rails controllers</title>
      <link>http://snippets.dzone.com/posts/show/4952</link>
      <description>With nested routes, I find I create a lot of controller methods like:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def load_user&lt;br /&gt;  @user = User.find(params[:user_id]) if params[:user_id]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Here's a TextMate snippet, so you can just type: defmodel, TAB, user, TAB, and you're done.&lt;br /&gt;&lt;br /&gt;Snippet:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def load_${1:model}&lt;br /&gt;	@$1 = ${1/[[:alpha:]]+|(_)/(?1::\u$0)/g}.find(params[${2::$1_}id])${3: if params[:$1_id]}&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Activation: defmodel&lt;br /&gt;Scope: source.ruby.rails &lt;br /&gt;&lt;br /&gt;I have this in the Ruby on Rails bundle, but you can put it anywhere. Its the scope that is important.</description>
      <pubDate>Mon, 07 Jan 2008 00:26:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4952</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Upgrade rubygems and/or specific gems themselves via capistrano</title>
      <link>http://snippets.dzone.com/posts/show/4905</link>
      <description>When a latest RubyGems is released (e.g. 0.9.5 recently) or Rails (e.g. 2.0.2) you might want to push those upgrades to all your production machines.&lt;br /&gt;&lt;br /&gt;Add these two tasks to your deploy.rb capistrano file (add namespaces for cap2.0 if you like)&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  desc "Updates RubyGems version"&lt;br /&gt;  task :gem_update_system do&lt;br /&gt;    sudo "gem update --system"&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;    &lt;br /&gt;  desc "Install a RubyGem from remote source"&lt;br /&gt;  task :gem_install do&lt;br /&gt;    puts "USAGE: GEM=gemname cap gems_install" and next unless ENV['GEM']&lt;br /&gt;    sudo "gem install #{ENV['GEM']} --no-ri --no-rdoc"&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;To upgrade rubygems and rails, for example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;cap gem_update_system&lt;br /&gt;GEM=rails cap gem_install&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Wed, 19 Dec 2007 22:10:32 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4905</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Auto-populate socket value in rails database.yml using TextMate snippet</title>
      <link>http://snippets.dzone.com/posts/show/4302</link>
      <description>&lt;br /&gt;When using mysql for rails apps, you may need a &lt;code&gt;socket:&lt;/code&gt; value. I can never remember mine. So I added a TextMate snippet to find it. (read below for non-TextMate)&lt;br /&gt;&lt;br /&gt;Snippet text:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;socket: `mysql_config --socket`&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Activation: &lt;code&gt;socket:&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Scope selector: &lt;code&gt;source.yaml - string&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Usage:&lt;/h2&gt;&lt;br /&gt;&lt;br /&gt;In your database.yml, go to the line &lt;code&gt;socket: &lt;/code&gt;, delete any blank spaces til the cursor is at the colon, then press TAB and wait. The line will be updated with the socket location.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Non-TextMate&lt;/h3&gt;&lt;br /&gt;&lt;br /&gt;If you don't have textmate, you can get your socket location using:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;mysql_config --socket&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;And paste the result into your database.yml&lt;br /&gt;</description>
      <pubDate>Mon, 16 Jul 2007 07:30:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4302</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Dump postgres production data into development database</title>
      <link>http://snippets.dzone.com/posts/show/3958</link>
      <description>When bug requests come in, its great to grab a copy of the current production (or system test env) data and sync it into your development database.&lt;br /&gt;&lt;br /&gt;Here's a capistrano recipe for postgresql:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc "Dumps target database into development db"&lt;br /&gt;task :sync_db do&lt;br /&gt;  env   = ENV['RAILS_ENV'] || ENV['DB'] || 'production'&lt;br /&gt;  file  = "#{application}.sql.bz2"&lt;br /&gt;  remote_file = "#{shared}/log/#{file}"&lt;br /&gt;  run "pg_dump --clean --no-owner --no-privileges -U#{db_user} -h#{db_host} #{db_name}_#{env} | bzip2 &gt; #{file}" do |ch, stream, out|&lt;br /&gt;    ch.send_data "#{db_password}\n" if out =~ /^Password:/&lt;br /&gt;    puts out&lt;br /&gt;  end&lt;br /&gt;  puts rsync = "rsync #{user}@#{domain}:#{file} tmp"&lt;br /&gt;  `#{rsync}`&lt;br /&gt;  puts depackage = "bzcat tmp/#{file} | psql #{local_db_dev}"&lt;br /&gt;  `#{depackage}`&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 06 May 2007 09:00:17 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3958</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Reset user password based on another users</title>
      <link>http://snippets.dzone.com/posts/show/3955</link>
      <description>When you first create an app you might not have time *cough* to add special user password reset features. Especially if you don't have many users.&lt;br /&gt;&lt;br /&gt;Here's a quick script to set the password of a user to another user (say a manually created user 'tester' with a known password).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;namespace :user do&lt;br /&gt;  desc 'Reset user (USER=username) password to that of username "tester" (or FROM=username env)'&lt;br /&gt;  task :reset =&gt; :environment do&lt;br /&gt;    reset_username = ENV['USER']&lt;br /&gt;    unless ENV['USER']&lt;br /&gt;      puts 'Require "USER=username" for user to be reset'&lt;br /&gt;      next&lt;br /&gt;    end&lt;br /&gt;    reset_user     = User.find_by_username reset_username&lt;br /&gt;    unless reset_user&lt;br /&gt;      puts "Cannot find user: #{reset_username}"&lt;br /&gt;      next&lt;br /&gt;    end&lt;br /&gt;    from_username  = ENV['FROM'] || 'tester'&lt;br /&gt;    from_user      = User.find_by_username from_username&lt;br /&gt;    unless from_user&lt;br /&gt;      puts "Cannot find user: #{from_username}"&lt;br /&gt;      next&lt;br /&gt;    end&lt;br /&gt;    &lt;br /&gt;    reset_user.crypted_password = from_user.crypted_password&lt;br /&gt;    reset_user.salt             = from_user.salt&lt;br /&gt;    reset_user  .save&lt;br /&gt;    puts 'User password reset'&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 04 May 2007 13:45:54 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3955</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Capistrano logs</title>
      <link>http://snippets.dzone.com/posts/show/3794</link>
      <description>To get the last n lines from a remote log file, use this task&lt;br /&gt;&lt;br /&gt;Usage: cap log [-s lines=100] [-s rails_env=production]&lt;br /&gt;So, "cap log" returns 100 lines from production.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc "Returns last lines of log file. Usage: cap log [-s lines=100] [-s rails_env=production]"&lt;br /&gt;task :log do&lt;br /&gt;  lines     = configuration.variables[:lines] || 100&lt;br /&gt;  rails_env = configuration.variables[:rails_env] || 'production'&lt;br /&gt;  run "tail -n #{lines} #{app_dir}/log/#{rails_env}.log" do |ch, stream, out|&lt;br /&gt;    puts out&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Apr 2007 20:09:05 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3794</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Migrate rhtml views into erb views within subversion repository</title>
      <link>http://snippets.dzone.com/posts/show/3624</link>
      <description>From &lt;a href="http://opensoul.org/2007/3/4/renaming-rhtml-to-erb-using-ruby"&gt;Brandon Keepers&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;To migrate rhtml views into erb views within subversion repository:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Dir.glob('app/views/**/*.rhtml').each do |file|&lt;br /&gt;  puts `svn mv #{file} #{file.gsub(/\.rhtml$/, '.erb')}`&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 04 Mar 2007 07:57:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3624</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Create classes at runtime</title>
      <link>http://snippets.dzone.com/posts/show/2390</link>
      <description>&lt;a href="http://drnicwilliams.com/2006/08/07/ann-dr-nics-magic-models/"&gt;Dr Nic's Magic Models&lt;/a&gt; adds automatic ActiveRecord generation if you haven't defined a model, and for all ActiveRecords it can generate associations and validations on-the-fly based on the database schema (the table and column definitions). &lt;br /&gt;&lt;br /&gt;It does this with some &lt;a href="http://drnicwilliams.com/2006/08/10/bts-magic-models-class-creation/"&gt;interesting&lt;/a&gt; Ruby meta-programming.&lt;br /&gt;&lt;br /&gt;For example, if you want to create a new class at runtime, and assign it a superclass, try the following:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def create_class(class_name, superclass, &amp;block)&lt;br /&gt;    klass = Class.new superclass, &amp;block&lt;br /&gt;    Object.const_set class_name, klass&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;With this code, you can create a class as follows:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;create_class('Person', ActiveRecord::Base) do&lt;br /&gt;  set_table_name :people&lt;br /&gt;&lt;br /&gt;  def fullname&lt;br /&gt;    "#{firstname} #{lastname}" # assuming the people table has firstname,lastname columns&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;</description>
      <pubDate>Thu, 10 Aug 2006 22:49:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2390</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
  </channel>
</rss>
