MetaSkills.net

The alias_method_chain of Rake - Override Rake Task

Posted On: May 25th, 2010 by kencollins

Rake is cool. It is built so that multiple tasks with the same name run in a reverse defined series. This is great, but sometimes you want to override a task with your own behavior and conditionally call the earlier task. Especially if that task is defined deep somewhere else, like in a rails gem. I have had to solve this problem before in Rake. Awhile back I hacked something up that would totally trump a predefined rake task and allow you to replace it with a new one. Lately while working on the SQL Server adapter, I had a need to method chain some core rails :db namespaced tasks. So once again I googled others work and again resorted to hacking Rake. Below is what I was left with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Rake::TaskManager.class_eval do
  def alias_task(fq_name)
    new_name = "#{fq_name}:original"
    @tasks[new_name] = @tasks.delete(fq_name)
  end
end

def alias_task(fq_name)
  Rake.application.alias_task(fq_name)
end

def override_task(*args, &block)
  name, params, deps = Rake.application.resolve_args(args.dup)
  fq_name = Rake.application.instance_variable_get(:@scope).dup.push(name).join(':')
  alias_task(fq_name)
  Rake::Task.define_task(*args, &block)
end

It's easy to use, just require it in your Rakefile. In the example below, I was able to programmatically method chain the core rails db:test:purge and only call the original if I needed to. Very cool!

1
2
3
4
5
6
7
8
9
10
11

namespace :db do
  namespace :test do
    override_task :purge => :environment do
      ...
      # To invoke the original task add ":original" to its name
      Rake::Task["db:test:purge:original"].execute
      ...
    end
  end
end

Lastly, thanks to Eugene Bolshakov, John Wood, and Mark Foster whom have tackled this rake problem before. My version above was based on their work, but correctly works with namespaces which was critical for my needs.

Tags:

RESTful AJAX with Forgery Protection (In Rails 3)

Posted On: April 12th, 2010 by kencollins

A while back ago I wrote an article about how to use Rails built-in forgery protection in your RESTful AJAX calls. Normally AJAX requests, those responding true to request.xhr? in rails, are forgery whitelisted. But sometimes, and under what conditions I am not sure, AJAX methods are subjected to forgery protection. Maybe you have the ActionDispatch::Request#forgery_whitelisted? overridden to not include AJAX requests? Either way and for whatever reason – if you like to use forgery protection in your RESTful AJAX calls to rails, then here is the new implementation under Rails 3 beta2.

TextMate Theme & Key Bindings For Xcode

Posted On: March 19th, 2010 by kencollins
Xcode TextMate Theme

I've been using Xcode for about 8 hard core months now and for all this time I have been using a theme I made that mimics the All Hallow's Eve TextMate theme with a bunch of custom key bindings that make Xcode more TextMate compatible. Here are links to download both below and a few notes I have put together. As I remember more of what I have actually done, this post will get updated. I do remember the first thing I did with Xcode for almost 3 days was just to customize the heck out of it and these files are the results.

Tags:

Git Init XCode Projects

Posted On: February 23rd, 2010 by kencollins

Here is a little ZSH function I have been using for quickly setting up new XCode apps I call tire kickers, little play and learn apps. Being able to track your learning as you go with git.

if [[ -x `which git` ]]; then
  
	function ginit_xcode () {
	  git init
	  echo "\n\n# XCode\nbuild\n*.mode1v3\n*.mode2v3\n*.nib\n*.swp\n\
*.pbxuser\n*.perspective\n*.perspectivev3\n\n# OSX\n.DS_Store\n\n\
# TextMate\n*.tm_build_errors\n\n\n" >> .gitignore
	  git add .gitignore
	  git commit -m "Ignore Xcode stuff."
	  git add .
	  git commit -m "Initial Xcode project."
	}
  
fi

The echo lines puts out a .gitignore file that will look something like this.

# XCode
build
*.mode1v3
*.mode2v3
*.nib
*.swp
*.pbxuser
*.perspective
*.perspectivev3

# OSX
.DS_Store

# TextMate
*.tm_build_errors
Tags: git, xcode

Synchronizing Core Data With Rails (3.0.0.pre)

Posted On: February 11th, 2010 by kencollins

This is my presentation to our local @757rb/@757objc users group this past Tuesday. Hope some find it useful. Lessons learned from building HomeMarks native iPhone application to synchronize Core Data with a RESTful backend built using rails 3.0.0.pre. This covers a previous design methodology called the AJAX head pattern which decouples rails applications from the views they present which allowed an easy API foundation for the iPhone application and data sync methods.

Resources

Simple Script/Console Function

Posted On: February 6th, 2010 by kencollins

This is something simple I worked up today for my ZSH profile that let's me keep my simple sc alias and have it work with all versions of rails. If you did not know, all the script files in rails 3 are gone and the new all-in-one rails executable does all the heavy lifting. This little function even passes down all the arguments too.

sc () {
  if [ -f ./script/rails ]; then 
    rails console $argv
  else
    ./script/console $argv
  fi
}

One other thing, the way rails uses IRB is different now. I had to change my ~/.irbrc file to look like this below to get my simple prompt and history back. IMPORTANT NOTE: In order for this to work, you have to apply this 2 line patch to your save-history.rb file. Worked like a champ for me.

1
2
3
4
5
6
7
8
9
10

# IRB history patch <http://redmine.ruby-lang.org/issues/show/1556>

require 'irb/completion'
require 'irb/ext/save-history'

IRB.conf[:USE_READLINE] = true
IRB.conf[:SAVE_HISTORY] = 500
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb.hist"
IRB.conf[:PROMPT_MODE] = :SIMPLE
Tags:

Unobtrusive JS In Rails 3 With Prototype

Posted On: January 29th, 2010 by kencollins

Are you bleeding on the edge of rails 3 and need to shim up some unobtrusive JavaScript to work with your link_to code that uses a destructive :method option? I did today and here is what I did to solve it. If you are unfamiliar with the problem, and what has been happening in rails 3 with UJS, check out Piotr Solnica's blog for a good run down. Or you can check out the simple code block below.

Tags: