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

Rick Olson http://techno-weenie.net

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

deploy your rails apps on edge rails

lib/tasks/rails.rake
   1  
   2  desc "Checks out rails"
   3  task :init do
   4    ENV['SHARED_PATH']  = '../../shared' unless ENV['SHARED_PATH']
   5    ENV['RAILS_PATH'] ||= File.join(ENV['SHARED_PATH'], 'rails')
   6   
   7    checkout_path = File.join(ENV['RAILS_PATH'], 'trunk')
   8    export_path   = "#{ENV['RAILS_PATH']}/rev_#{ENV['REVISION']}"
   9    symlink_path  = 'vendor/rails'
  10  
  11    # do we need to checkout the file?
  12    unless File.exists?(checkout_path)
  13      puts 'setting up rails trunk'    
  14      get_framework_for checkout_path do |framework|
  15        system "svn co http://dev.rubyonrails.org/svn/rails/trunk/#{framework}/lib #{checkout_path}/#{framework}/lib --quiet"
  16      end
  17    end
  18  
  19    # do we need to export the revision?
  20    unless File.exists?(export_path)
  21      puts "setting up rails rev #{ENV['REVISION']}"
  22      get_framework_for export_path do |framework|
  23        system "svn up #{checkout_path}/#{framework}/lib -r #{ENV['REVISION']} --quiet"
  24        system "svn export #{checkout_path}/#{framework}/lib #{export_path}/#{framework}/lib"
  25      end
  26    end
  27  
  28    puts 'linking rails'
  29    rm_rf   symlink_path
  30    mkdir_p symlink_path
  31  
  32    get_framework_for symlink_path do |framework|
  33      ln_s File.expand_path("#{export_path}/#{framework}/lib"), "#{symlink_path}/#{framework}/lib"
  34    end
  35  end
  36  
  37  def get_framework_for(*paths)
  38    %w( railties actionpack activerecord actionmailer activesupport actionwebservice ).each do |framework|
  39      paths.each { |path| mkdir_p "#{path}/#{framework}" }
  40      yield framework
  41    end
  42  end


Switchtower recipe code:
   1  
   2  set :rails_version, 3517
   3  desc "Checks out rails rev ##{rails_version}"
   4  task :after_symlink do
   5    run <<-CMD
   6      cd #{current_release} &&
   7      rake init REVISION=#{rails_version} RAILS_PATH=/home/username/projects/rails
   8    CMD
   9  end

simulating cookies in RoR functional tests

Okay, my first try didn't work. This seems to though:
   1  
   2  @request.cookies['key'] = CGI::Cookie.new( 
   3    'name'   => 'key',
   4    'value'   => my_value,
   5    'expires' => 14.days.from_now,
   6    'path'    => '/',
   7    'domain'  => 'example.com'
   8  )


When you're simulating a cookie, you have to go all the way. It expects it as a CGI::Cookie, set by a hash with strings for keys.

loading fixtures to development database

Fixed the previous example so it would load environment first:

   1  
   2  desc "Load fixtures data into the development database"
   3  task :load_fixtures_to_development => :environment do
   4    ActiveRecord::Base.establish_connection(:development)
   5    require 'active_record/fixtures'
   6    Fixtures.create_fixtures("test/fixtures", ActiveRecord::Base.configurations[:fixtures_load_order])
   7    puts "Loaded these fixtures: " + ActiveRecord::Base.configurations[:fixtures_load_order].collect { |f| f.to_s }.join(', ')
   8  end


This requires a fixture list in database.yml (or you can specify in environment.rb if you wish:

   1  
   2  :fixtures_load_order:
   3    - :fixture_1
   4    - :fixture_2
   5    - :fixture_3
   6    - :fixture_4
   7    - :fixture_5

Maintenance of Rails Programs

This is a maintenance script, yanked from Lucas Carlson's blog. Put this in your rails app to have a quick and dirty server maintenance cleanup app:

original at: http://tech.rufy.com/entry/15

   1  
   2  pid_file = '/tmp/running.pid'
   3  begin
   4    File.stat pid_file
   5  rescue
   6    f = File.new pid_file, 'w'
   7    f < < fork {
   8      loop {
   9        # do lots of maintenance
  10        sleep 15*60
  11      }
  12    }
  13    f.close
  14  end


Finally, if your maintenance code needs to be changed, a couple simple commands can restart the new maintenance code:

   1  
   2  kill `cat /tmp/running.pid`
   3  rm /tmp/running.pid

Themeable Views in Ruby

   1  
   2  class ApplicationController < ActionController::Base
   3    before_filter :choose_theme
   4    
   5    private
   6    def choose_theme
   7      ActionController::Base.template_root = File.join(RAILS_ROOT, 'app/views', @params['theme'])
   8    end
   9  end
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS