<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Err's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 14:33:39 GMT</pubDate>
    <description>DZone Snippets: Err's Code Snippets</description>
    <item>
      <title>Run script/console Remotely with Capistrano</title>
      <link>http://snippets.dzone.com/posts/show/2485</link>
      <description>From &lt;a href="http://errtheblog.com/post/21"&gt;http://errtheblog.com/post/21&lt;/a&gt;.&lt;br /&gt;Allows you to execute script/console remotely through Capistrano.  Not for use on multi-machine :roles.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc "remotely console" &lt;br /&gt;task :console, :roles =&gt; :app do&lt;br /&gt;  input = ''&lt;br /&gt;  run "cd #{current_path} &amp;&amp; ./script/console #{ENV['RAILS_ENV']}" do |channel, stream, data|&lt;br /&gt;    next if data.chomp == input.chomp || data.chomp == ''&lt;br /&gt;    print data&lt;br /&gt;    channel.send_data(input = $stdin.gets) if data =~ /^(&gt;|\?)&gt;/&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 28 Aug 2006 05:52:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2485</guid>
      <author>err (Chris Wanstrath)</author>
    </item>
    <item>
      <title>Tail Multiple Log Files At Once With Capistrano</title>
      <link>http://snippets.dzone.com/posts/show/2484</link>
      <description>From &lt;a href="http://errtheblog.com/post/21"&gt;http://errtheblog.com/post/21&lt;/a&gt;&lt;br /&gt;Allows you to aggregate the tailing of multiple production log files with one Capistrano task&lt;br /&gt;&lt;br /&gt;Stick it in deploy.rb:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc "tail production log files" &lt;br /&gt;task :tail_logs, :roles =&gt; :app do&lt;br /&gt;  run "tail -f #{shared_path}/log/production.log" do |channel, stream, data|&lt;br /&gt;    puts  # for an extra line break before the host name&lt;br /&gt;    puts "#{channel[:host]}: #{data}" &lt;br /&gt;    break if stream == :err    &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 28 Aug 2006 05:50:52 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2484</guid>
      <author>err (Chris Wanstrath)</author>
    </item>
    <item>
      <title>Block to Partial Rails Helper</title>
      <link>http://snippets.dzone.com/posts/show/2483</link>
      <description>From &lt;a href="http://errtheblog.com/post/13"&gt;http://errtheblog.com/post/13&lt;/a&gt;.&lt;br /&gt;Create a helper which takes a block and renders that block within the context of a partial.&lt;br /&gt;&lt;br /&gt;Create this helper:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def block_to_partial(partial_name, options = {}, &amp;block)&lt;br /&gt;  options.merge!(:body =&gt; capture(&amp;block))&lt;br /&gt;  concat(render(:partial =&gt; partial_name, :locals =&gt; options), block.binding)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Create this helper, too:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def sidebar_module(title, options = {}, &amp;block)&lt;br /&gt;  block_to_partial('shared/sidebar_module', options.merge(:title =&gt; title), &amp;block)&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Create this partial (app/views/shared/_sidebar_module.rhtml):&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;div &lt;%= %[id="#{id}"] unless id.blank? %&gt; class="sidebar_module"&gt;&lt;br /&gt;  &lt;h2&gt;&lt;%= title %&gt;&lt;/h2&gt;&lt;br /&gt;  &lt;%= body %&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Use it in your views:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;% sidebar_module 'Recent Stories' do %&gt;&lt;br /&gt;  &lt;%= render :partial =&gt; 'recent_stories', :collection =&gt; @recent_stories %&gt;&lt;br /&gt;&lt;% end %&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 28 Aug 2006 05:49:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2483</guid>
      <author>err (Chris Wanstrath)</author>
    </item>
    <item>
      <title>Parse XML with Hpricot</title>
      <link>http://snippets.dzone.com/posts/show/2482</link>
      <description>From &lt;a href="http://errtheblog.com/post/8"&gt;http://errtheblog.com/post/8&lt;/a&gt;&lt;br /&gt;Simple XML is basically HTML with random tags, yeah?  Parse it with &lt;a href="http://redhanded.hobix.com/inspect/hpricot01.html"&gt;Hpricot&lt;/a&gt;!&lt;br /&gt;&lt;br /&gt;Your XML:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;Export&gt;&lt;br /&gt;  &lt;Product&gt;&lt;br /&gt;    &lt;SKU&gt;403276&lt;/SKU&gt;&lt;br /&gt;    &lt;ItemName&gt;Trivet&lt;/ItemName&gt;&lt;br /&gt;    &lt;CollectionNo&gt;0&lt;/CollectionNo&gt;&lt;br /&gt;    &lt;Pages&gt;0&lt;/Pages&gt;&lt;br /&gt;  &lt;/Product&gt;&lt;br /&gt;&lt;/Export&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;The code:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'hpricot'&lt;br /&gt;FIELDS = %w[SKU ItemName CollectionNo Pages]&lt;br /&gt;&lt;br /&gt;doc = Hpricot.parse(File.read("my.xml"))&lt;br /&gt;(doc/:product).each do |xml_product|&lt;br /&gt;  product = Product.new&lt;br /&gt;  for field in FIELDS&lt;br /&gt;    product[field] = (xml_product/field.intern).first.innerHTML&lt;br /&gt;  end&lt;br /&gt;  product.save&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 28 Aug 2006 05:45:47 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2482</guid>
      <author>err (Chris Wanstrath)</author>
    </item>
    <item>
      <title>rake remigrate</title>
      <link>http://snippets.dzone.com/posts/show/2481</link>
      <description>From &lt;a href="http://errtheblog.com/post/3"&gt;http://errtheblog.com/post/3&lt;/a&gt;&lt;br /&gt;Drops your database, recreates it, runs all migrations, then loads fixtures.  Heroic.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc "Drop then recreate the dev database, migrate up, and load fixtures" &lt;br /&gt;task :remigrate =&gt; :environment do&lt;br /&gt;  return unless %w[development test staging].include? RAILS_ENV&lt;br /&gt;  ActiveRecord::Base.connection.tables.each { |t| ActiveRecord::Base.connection.drop_table t }&lt;br /&gt;  Rake::Task[:migrate].invoke&lt;br /&gt;  Rake::Task["db:fixtures:load"].invoke&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 28 Aug 2006 05:42:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2481</guid>
      <author>err (Chris Wanstrath)</author>
    </item>
  </channel>
</rss>
