Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Chris Wanstrath http://errtheblog.com

« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS 

Run script/console Remotely with Capistrano

From http://errtheblog.com/post/21.
Allows you to execute script/console remotely through Capistrano. Not for use on multi-machine :roles.

desc "remotely console" 
task :console, :roles => :app do
  input = ''
  run "cd #{current_path} && ./script/console #{ENV['RAILS_ENV']}" do |channel, stream, data|
    next if data.chomp == input.chomp || data.chomp == ''
    print data
    channel.send_data(input = $stdin.gets) if data =~ /^(>|\?)>/
  end
end

Tail Multiple Log Files At Once With Capistrano

From http://errtheblog.com/post/21
Allows you to aggregate the tailing of multiple production log files with one Capistrano task

Stick it in deploy.rb:
desc "tail production log files" 
task :tail_logs, :roles => :app do
  run "tail -f #{shared_path}/log/production.log" do |channel, stream, data|
    puts  # for an extra line break before the host name
    puts "#{channel[:host]}: #{data}" 
    break if stream == :err    
  end
end

Block to Partial Rails Helper

From http://errtheblog.com/post/13.
Create a helper which takes a block and renders that block within the context of a partial.

Create this helper:
def block_to_partial(partial_name, options = {}, &block)
  options.merge!(:body => capture(&block))
  concat(render(:partial => partial_name, :locals => options), block.binding)
end


Create this helper, too:
def sidebar_module(title, options = {}, &block)
  block_to_partial('shared/sidebar_module', options.merge(:title => title), &block)
end


Create this partial (app/views/shared/_sidebar_module.rhtml):
<div <%= %[id="#{id}"] unless id.blank? %> class="sidebar_module">
  <h2><%= title %></h2>
  <%= body %>
</div>


Use it in your views:
<% sidebar_module 'Recent Stories' do %>
  <%= render :partial => 'recent_stories', :collection => @recent_stories %>
<% end %>

Parse XML with Hpricot

From http://errtheblog.com/post/8
Simple XML is basically HTML with random tags, yeah? Parse it with Hpricot!

Your XML:
<Export>
  <Product>
    <SKU>403276</SKU>
    <ItemName>Trivet</ItemName>
    <CollectionNo>0</CollectionNo>
    <Pages>0</Pages>
  </Product>
</Export>


The code:
require 'hpricot'
FIELDS = %w[SKU ItemName CollectionNo Pages]

doc = Hpricot.parse(File.read("my.xml"))
(doc/:product).each do |xml_product|
  product = Product.new
  for field in FIELDS
    product[field] = (xml_product/field.intern).first.innerHTML
  end
  product.save
end

rake remigrate

From http://errtheblog.com/post/3
Drops your database, recreates it, runs all migrations, then loads fixtures. Heroic.

desc "Drop then recreate the dev database, migrate up, and load fixtures" 
task :remigrate => :environment do
  return unless %w[development test staging].include? RAILS_ENV
  ActiveRecord::Base.connection.tables.each { |t| ActiveRecord::Base.connection.drop_table t }
  Rake::Task[:migrate].invoke
  Rake::Task["db:fixtures:load"].invoke
end
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS