<?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>Sat, 26 Jul 2008 00:45:06 GMT</pubDate>
    <description>DZone Snippets: rails code</description>
    <item>
      <title>open_body_tag</title>
      <link>http://snippets.dzone.com/posts/show/4726</link>
      <description>&lt;code&gt;&lt;br /&gt;  #creates a body tag uniquely indentifying this page&lt;br /&gt;  #takes an options Hash with two keys:&lt;br /&gt;  #&lt;br /&gt;  #&lt;tt&gt;id&lt;/tt&gt;::        string that will be used as the body's ID. defaults to &lt;tt&gt;controller.controller_name.singularize&lt;/tt&gt;&lt;br /&gt;  #&lt;tt&gt;classes&lt;/tt&gt;::   an Array of class names. defaults to &lt;tt&gt;[params[:action]]&lt;/tt&gt;&lt;br /&gt;  #&lt;br /&gt;  #Examples:&lt;br /&gt;  #&lt;br /&gt;  # in HomeController#index:&lt;br /&gt;  #&lt;br /&gt;  # &lt;%= open_body_tag %&gt;&lt;br /&gt;  # =&gt; &lt;body id='home' class='index'&gt;&lt;br /&gt;  #&lt;br /&gt;  # &lt;%= open_body_tag(:id =&gt; 'foo') %&gt;&lt;br /&gt;  # =&gt; &lt;body id='foo' class='index'&gt;&lt;br /&gt;  #&lt;br /&gt;  # &lt;%= open_body_tag(:id =&gt; 'foo', :classes =&gt; %w(one two)) %&gt;&lt;br /&gt;  # =&gt; &lt;body id='foo' class='one two'&gt;&lt;br /&gt;  def open_body_tag(options = { :id =&gt; controller.controller_name.singularize, :classes =&gt; [params[:action]] })&lt;br /&gt;    "&lt;body id='#{options[:id]}' class='#{options[:classes].join(' ')}'&gt;"&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 01 Nov 2007 19:24:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4726</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Rails URL Validation</title>
      <link>http://snippets.dzone.com/posts/show/4532</link>
      <description>No regexes, allows URLs with ports or IPs. Inspiration from &lt;a href="http://actsasblog.wordpress.com/2006/10/16/url-validation-in-rubyrails/"&gt;here&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  validates_each :href, :on =&gt; :create do |record, attr, value|&lt;br /&gt;    begin&lt;br /&gt;      uri = URI.parse(value)&lt;br /&gt;      if uri.class != URI::HTTP&lt;br /&gt;        record.errors.add(attr, 'Only HTTP protocol addresses can be used')&lt;br /&gt;      end&lt;br /&gt;    rescue URI::InvalidURIError&lt;br /&gt;      record.errors.add(attr, 'The format of the url is not valid.')&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 12 Sep 2007 14:03:00 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4532</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Synchronizing Rails DB Contents via Fixtures</title>
      <link>http://snippets.dzone.com/posts/show/3393</link>
      <description>The following rake task will dump the contents of the current environment's database to YAML fixtures. Stick the following in lib/tasks/fixtures.rake:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;namespace :db do&lt;br /&gt;  namespace :fixtures do&lt;br /&gt;    &lt;br /&gt;    desc 'Create YAML test fixtures from data in an existing database.  &lt;br /&gt;    Defaults to development database.  Set RAILS_ENV to override.'&lt;br /&gt;    task :dump =&gt; :environment do&lt;br /&gt;      sql  = "SELECT * FROM %s"&lt;br /&gt;      skip_tables = ["schema_info"]&lt;br /&gt;      ActiveRecord::Base.establish_connection(:development)&lt;br /&gt;      (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|&lt;br /&gt;        i = "000"&lt;br /&gt;        File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|&lt;br /&gt;          data = ActiveRecord::Base.connection.select_all(sql % table_name)&lt;br /&gt;          file.write data.inject({}) { |hash, record|&lt;br /&gt;            hash["#{table_name}_#{i.succ!}"] = record&lt;br /&gt;            hash&lt;br /&gt;          }.to_yaml&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;After making changes to the database that you'd like to dump to fixtures:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rake db:fixtures:dump&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;After checking out updated fixtures from SVN:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rake db:migrate&lt;br /&gt;rake db:fixtures:load&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 31 Jan 2007 01:04:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3393</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Capistrano : apply local patches when deploying from an external source code repository</title>
      <link>http://snippets.dzone.com/posts/show/3147</link>
      <description>I've submitted patches to a couple rails apps, and want to run off of their SCM's trunk code, but with my local patches applied. These Capistrano tasks will take any files matching &lt;pre&gt;patches/*.diff&lt;/pre&gt; in your local directory, and apply them before restarting your app.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;task :after_setup do&lt;br /&gt;  patches_setup&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;task :after_update_code do&lt;br /&gt;  send_and_apply_patches&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;task :patches_setup do&lt;br /&gt;  run "mkdir -p #{deploy_to}/#{shared_dir}/patches" &lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;task :send_and_apply_patches do&lt;br /&gt;  Dir[File.join(File.dirname(__FILE__), '../patches/*.diff')].sort.each do |patch|&lt;br /&gt;    puts "sending #{File.basename(patch)}"&lt;br /&gt;    put(File.read(patch),&lt;br /&gt;       "#{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}",&lt;br /&gt;       :mode =&gt; 0777)&lt;br /&gt;    puts "applying #{File.basename(patch)}"&lt;br /&gt;    run "cd #{release_path}; patch -p0 &lt; #{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}"&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 13 Dec 2006 20:51:35 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3147</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>redirect or render</title>
      <link>http://snippets.dzone.com/posts/show/1623</link>
      <description>Quick method to help your XHR requests degrade gracefully - via the &lt;a href="http://blog.caboo.se/articles/2006/03/02/redirect-or-render"&gt;caboo.se&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def redirect_or_render( redirect_to_hash, render_page  )  &lt;br /&gt;  if @request.xhr?&lt;br /&gt;    render(render_page)&lt;br /&gt;  else&lt;br /&gt;    redirect_to(redirect_to_hash)&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Use like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;redirect_or_render(  &lt;br /&gt;  {:action=&gt;'foo'},&lt;br /&gt;  { :partial =&gt; 'monkey', :locals =&gt; { :obj = &gt; 'x' } }&lt;br /&gt;)&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 03 Mar 2006 08:04:07 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1623</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
  </channel>
</rss>
