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

Jesse Newland http://jnewland.com

« Newer Snippets
Older Snippets »
Showing 11-15 of 15 total

Capistrano : apply local patches when deploying from an external source code repository

I've submitted patches to a couple rails apps, and want to run off of their SCM's trunk code, but with my local patches applied. These Capistrano tasks will take any files matching
patches/*.diff
in your local directory, and apply them before restarting your app.

task :after_setup do
  patches_setup
end

task :after_update_code do
  send_and_apply_patches
end

task :patches_setup do
  run "mkdir -p #{deploy_to}/#{shared_dir}/patches" 
end

task :send_and_apply_patches do
  Dir[File.join(File.dirname(__FILE__), '../patches/*.diff')].sort.each do |patch|
    puts "sending #{File.basename(patch)}"
    put(File.read(patch),
       "#{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}",
       :mode => 0777)
    puts "applying #{File.basename(patch)}"
    run "cd #{release_path}; patch -p0 < #{deploy_to}/#{shared_dir}/patches/#{File.basename(patch)}"
  end
end

redirect or render

Quick method to help your XHR requests degrade gracefully - via the caboo.se

def redirect_or_render( redirect_to_hash, render_page  )  
  if @request.xhr?
    render(render_page)
  else
    redirect_to(redirect_to_hash)
  end
end


Use like this:

redirect_or_render(  
  {:action=>'foo'},
  { :partial => 'monkey', :locals => { :obj = > 'x' } }
)

Movable Type Navigation

This will generate nested unordered lists of links to all category and sub-category archives that are descendants of the "Articles" category, but not a link to the "Articles" category itself. Requires the wonderful Compare plugin.

<MTSubCategories>
<MTIfIsDescendant parent="Articles">
<MTIfEqual a="[MTCategoryLabel]" b="Articles">
<MTSubCatsRecurse>
<MTElse>
<MTSubCatIsFirst><ul class="leftnav"></MTSubCatIsFirst>
<li><strong><a href="<MTCategoryArchiveLink$>" title="<$MTCategoryDescription$>"><$MTCategoryLabel$></a><MTSubCatsRecurse></strong></li>
<MTSubCatIsLast></ul></MTSubCatIsLast>
</MTElse>
</MTIfEqual>
</MTIfIsDescendant>
</MTSubCategories>

Lighty redirect

Redirect http://www.jnewland.com and http:///blog.jnewland.com, both still indexed by Google, to http://jnewland.com. 'www' is so 1995 :)

$HTTP["host"] =~ "^(www.|blog.)" {
    url.redirect = (
        "^/(.*)" => "http://jnewland.com/$1",
        "" => "http://jnewland.com/",
        "/" => "http://jnewland.com/"
    )
}

Feedburner Redirect

Redirect everyone except the feedburner spider to FeedBurner's cached version.

$HTTP["host"] =~ "^(www.|blog.)?jnewland.com" {
    $HTTP["useragent"] !~ "FeedBurner" {
        url.redirect = (
            "/xml/rss20/feed.xml" => "http://feeds.feedburner.com/jnewlandcom"
        )
    }
}
« Newer Snippets
Older Snippets »
Showing 11-15 of 15 total