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-4 of 4 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.

   1  
   2  desc "remotely console" 
   3  task :console, :roles => :app do
   4    input = ''
   5    run "cd #{current_path} && ./script/console #{ENV['RAILS_ENV']}" do |channel, stream, data|
   6      next if data.chomp == input.chomp || data.chomp == ''
   7      print data
   8      channel.send_data(input = $stdin.gets) if data =~ /^(>|\?)>/
   9    end
  10  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:
   1  
   2  desc "tail production log files" 
   3  task :tail_logs, :roles => :app do
   4    run "tail -f #{shared_path}/log/production.log" do |channel, stream, data|
   5      puts  # for an extra line break before the host name
   6      puts "#{channel[:host]}: #{data}" 
   7      break if stream == :err    
   8    end
   9  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:
   1  
   2  def block_to_partial(partial_name, options = {}, &block)
   3    options.merge!(:body => capture(&block))
   4    concat(render(:partial => partial_name, :locals => options), block.binding)
   5  end


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


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


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

rake remigrate

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

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