EdgeRails : rack router, protect_from_forgery & modules dependencies



Written by Anthony Heukmes on Fri Apr 17 16:16:01 UTC 2009

0 comment



The way towards Rails 3 is going on and you can find a lot of activity on GitHub these days.

First good news, Carl Lerche & Josh Peek are working on a new router (a Rack middleware, so it will not be only useful for Rails). According to Carl, performances are 2x faster than Merb router (and so, 4 times faster than Rails).

More information available on Rack GG group.

Another good news, we will not have to add authenticity tokens to our hand-written Ajax requests anymore!
You know, when you create a Javascript function that sends a POST request to your server and you get a nice "Invalid Authenticity Token" error message in return.
The solution is to add the token to your parameters or to disable CSRF attacks check in your controller using protect_from_forgery :except => :my_action.
This will not be required anymore with Rails 3!

The touch method has also been added to ActiveRecord models.
When called, it will save the record with the updated_at attribute set to the current time.

It can also be used as an option with the belongs_to association. So when the current instance is saved/destroyed, the associated object will be touched too.

belongs_to :company, :touch => true


If you follow the evolution of the Rails codebase you've probably been surprised by setup, depends_on and use keywords. You can see an example in this commit.

The source code for can be found in ActiveSupport (activesupport/ lib/active_support/core_ext/module/setup.rb) :


class Module
attr_accessor :_setup_block
attr_accessor :_dependencies

def setup(&blk)
@_setup_block = blk
end

def use(mod)
return if self < mod

(mod._dependencies || []).each do |dep|
use dep
end
# raise "Circular dependencies" if self < mod
include mod
extend mod.const_get("ClassMethods") if mod.const_defined?("ClassMethods")
class_eval(&mod._setup_block) if mod._setup_block
end

def depends_on(mod)
return if self < mod
@_dependencies ||= []
@_dependencies << mod
end
end


A cleaner way to organize modules inclusion and dependencies. Yehuda Katz wrote an article about that.
Bookmark and Share

Add a comment



0 comment for this article