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-10 of 38 total  RSS 

backuping up/restring pgsql

I always forget this, or get it mixed up with MySQL:

pg_dump dbname > dbname.sql
cat dbname.sql | psql dbname

deploy your rails apps on edge rails

lib/tasks/rails.rake
desc "Checks out rails"
task :init do
  ENV['SHARED_PATH']  = '../../shared' unless ENV['SHARED_PATH']
  ENV['RAILS_PATH'] ||= File.join(ENV['SHARED_PATH'], 'rails')
 
  checkout_path = File.join(ENV['RAILS_PATH'], 'trunk')
  export_path   = "#{ENV['RAILS_PATH']}/rev_#{ENV['REVISION']}"
  symlink_path  = 'vendor/rails'

  # do we need to checkout the file?
  unless File.exists?(checkout_path)
    puts 'setting up rails trunk'    
    get_framework_for checkout_path do |framework|
      system "svn co http://dev.rubyonrails.org/svn/rails/trunk/#{framework}/lib #{checkout_path}/#{framework}/lib --quiet"
    end
  end

  # do we need to export the revision?
  unless File.exists?(export_path)
    puts "setting up rails rev #{ENV['REVISION']}"
    get_framework_for export_path do |framework|
      system "svn up #{checkout_path}/#{framework}/lib -r #{ENV['REVISION']} --quiet"
      system "svn export #{checkout_path}/#{framework}/lib #{export_path}/#{framework}/lib"
    end
  end

  puts 'linking rails'
  rm_rf   symlink_path
  mkdir_p symlink_path

  get_framework_for symlink_path do |framework|
    ln_s File.expand_path("#{export_path}/#{framework}/lib"), "#{symlink_path}/#{framework}/lib"
  end
end

def get_framework_for(*paths)
  %w( railties actionpack activerecord actionmailer activesupport actionwebservice ).each do |framework|
    paths.each { |path| mkdir_p "#{path}/#{framework}" }
    yield framework
  end
end


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

regex matching in lighttpd configs

Match the host name and rewrite paths like /images to blah.com/images:

$HTTP["host"] =~ "^.*\.([^.]+\.[^.:]+)(:|^)" {
  url.rewrite = ( "^/images/(.*)" => "/%1/images/$1",
                 "^/files/(.*)" => "/%1/files/$1",
 "^/$" => "index.html", "^([^.]+)$" => "$1.html")
}


provided by Dreamer3 and annotated here so I can quit bugging him about it.

measure map tags for typo

See my associated blog post.

# /app/views/articles/_article.rhtml
<script type="text/javascript"><!--
if(!mmposts){var mmposts=[];}mmposts[mmposts.length]="<%= article.id %>";
//--></script>
<!-- mmp mmid:<%= article.id %> mmdate:<%= article.created_at.to_s(:db) %> mmurl:<%=h article_url(article, true) %> mmtitle:<%=h article.title %> -->

# /app/views/articles/_comment.rhtml
<script type="text/javascript"><!--
if(!mmcomments){var mmcomments=[];}mmcomments[mmcomments.length]="<%= comment.id %>";
//--></script>
<!-- mmc mmid:<%= comment.id %> mmdate:<%= comment.created_at.to_s(:db) %> mmauthor:<%=h comment.author %> -->

# /app/views/articles/read.rhtml
# This is the same as the _article.rhtml snippet, but using the @article instance var instead of the article local var
<script type="text/javascript"><!--
if(!mmposts){var mmposts=[];}mmposts[mmposts.length]="<%= @article.id %>";
//--></script>
<!-- mmp mmid:<%= @article.id %> mmdate:<%= @article.created_at.to_s(:db) %> mmurl:<%=h article_url(@article, true) %> mmtitle:<%=h @article.title %> -->
</div>


There's also a javascript include to add to the bottom of your typo theme, but there's no customization required...

freeze_edge for switchtower deployments

I run this rake task in the after_symlink event of switchtower to set up the newly deployed app's config files. I rewrote the freeze_edge task to be a little more efficient:

1. checks out rails trunk to shared/rails/trunk if needed
2. updates rails trunk to revision used by the app
3. exports that revision to shared/rails/rev_xxxx
4. links that revision to current/vendor/rails

This way, my app only has a single checkout of the rails trunk. Multiple deployments do not checkout fresh copies, but update to the desired revision and export. If you make multiple deployments using the same rails revision, then only the symlink action is performed. This speeds the task up considerably, and conserves disk space and network traffic.

Set rails version in deploy.rb:
set :rails_version, 2871


Create after_symlink task:
desc "Checks out rails rev ##{rails_version}"
task :after_symlink do
  run "cd #{current_release} && rake init SHARED_PATH=#{shared_path} CURRENT_RELEASE=#{current_release} REVISION=#{rails_version}"
end


init task:
desc "Performs an export of the rails trunk"
task :init do
  return unless ENV['SHARED_PATH'] and ENV['CURRENT_RELEASE']
  puts 'copying files...'
  cp "#{ENV['SHARED_PATH']}/database.yml",  "#{ENV['CURRENT_RELEASE']}/config"
  cp "#{ENV['SHARED_PATH']}/dispatch.fcgi", "#{ENV['CURRENT_RELEASE']}/public"

  puts 'setting permissions...'
  chmod 0600, "#{ENV['CURRENT_RELEASE']}/config/database.yml"
  chmod 0700, "#{ENV['CURRENT_RELEASE']}/public/dispatch.fcgi"
  
  checkout_path = "#{ENV['SHARED_PATH']}/rails/trunk"
  export_path = "#{ENV['SHARED_PATH']}/rails/rev_#{ENV['REVISION']}"
  symlink_path = "#{ENV['CURRENT_RELEASE']}/vendor/rails"

  # do we need to checkout the file?
  unless File.exists?(checkout_path)
    puts 'setting up rails trunk'    
    get_framework_for checkout_path do |framework|
      system "svn co http://dev.rubyonrails.org/svn/rails/trunk/#{framework}/lib #{checkout_path}/#{framework}/lib --quiet"
    end
  end

  # do we need to export the revision?
  unless File.exists?(export_path)
    puts "setting up rails rev #{ENV['REVISION']}"
    get_framework_for export_path, symlink_path do |framework|
      system "svn up #{checkout_path}/#{framework}/lib -r #{ENV['REVISION']} --quiet"
      system "svn export #{checkout_path}/#{framework}/lib #{export_path}/#{framework}/lib"
    end
  end

  puts 'linking rails'
  rm_rf   "vendor/rails"
  mkdir_p "vendor/rails"

  get_framework_for checkout_path, export_path, symlink_path do |framework|
    ln_s File.expand_path("#{export_path}/#{framework}/lib"), "#{ENV['CURRENT_RELEASE']}/vendor/rails/#{framework}/lib"
  end
end

def get_framework_for(*paths)
  %w( railties actionpack activerecord actionmailer activesupport actionwebservice ).each do |framework|
    paths.each { |path| mkdir_p "#{path}/#{framework}" }
    yield framework
  end
end

restart rails app in switchtower on textdrive

You'll have to override the standard Switchtower restart task if you don't have sudo access:

desc "Restart the FCGI processes on the app server."
task :restart, :roles => :app do
  run "ruby #{current_path}/script/process/reaper"
end

Set an ActionController's template root on each request

Set an ActionController's template root on each request for a domain acts as account key pattern. Careful with this code though. It relies on the behavior of a private rails method and could change 50 times in the next week. (see my blog post)

class MyController < ActionController::Base
  before_filter :set_site_template_root
  def set_site_template_root
    self.class.template_root = "#{RAILS_ROOT}/#{app}/#{views}/#{current_site.domain}")
    @template.base_path = template_root
  end
end


module ActionView
  class Base
    private
    alias_method :rails_assign_method_name, :assign_method_name

    # add a domain prefix at the end
    def assign_method_name(extension, template, file_name)
      rails_method_name = rails_assign_method_name(extension, template, file_name)
      @@method_names[file_name || template] = respond_to?(:current_site) ?
        "#{rails_method_name}_#{current_site.domain}".intern :
        rails_method_name
    end
  end
end

transparent pngs in IE

Keep in mind that the AlphaImageLoader takes a relative path to the HTML page, not the CSS file (which is in contrast to normal css behavior). Absolute positioning causes it to act funky, so I wouldn't recommend it.

* html #blah {
  filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/full/path/to/img.png', sizingMethod='crop');
  background:none;
  cursor:hand; /* only if this is a link */
  position:relative;
}

css opacity in IE

opacity:.7; /* css standard */
filter:alpha(opacity=70); /* IE patch */

using map on an array of elements

I wanted to use prototype's map function on a listing of elements like:

document.getElementsByTagName('div').map(function(el) {});


Object.extend(String.prototype, {
  tags_in: function(el) {
    nodes = [];
    elems = $(el).getElementsByTagName(this);
    for(var i = 0; i < elems.length; i++)
      nodes.push(elems[i]);
    return nodes;
  }
});


'div'.tags_in(document).map(function(el) {});
« Newer Snippets
Older Snippets »
Showing 1-10 of 38 total  RSS