Rails tip #1: Reloadable custom FormBuilder

Posted on April 21, 2008

August Lilleaas has a nice writeup on making your own FormBuilder. Towards the end, he describes wiring it up as the default:

Create a new file called config/initializers/setup.rb. Or, if you’re on pre-2.0, add it to the bottom of config/environment.rb:

ActionView::Base.default_form_builder = LabellingFormBuilder

Though “proper,” this approach requires restarting the server to see changes in LabellingFormBuilder.

You can avoid restarting the server by doing this instead:

class ActionView::Base
  def self.default_form_builder
    LabellingFormBuilder
  end
end

It’s a subtle difference, but it provides a nice window into understanding the Dependencies mechanism:

In August’s version, const_missing loads the LabellingFormBuilder class only once, when the initializer is run.

In our version, we’ve tricked const_missing into running on each request by not holding onto the class it loads. That is, instead of caching the class lookup in @@default_form_builder, we’re allowing the lookup to be performed on demand.

Sweet.

5 Rails tips

Each day this week, Joachim and I will post something we’ve learned in our time programming together. It’s fun to do, and we might just win something as well.

doh 0.2.1

Posted on April 09, 2008

Doh stops your version control commit if you’ve forgotten to add some files.

I last wrote about Doh in August. You may remember this part:


DDDDDDDDDDDDD                         hhhhhhh
 D::::::::::::DDD                      h:::::h
 D:::::::::::::::DD                    h:::::h
 DDD:::::DDDDD:::::D                   h:::::h
   D:::::D    D:::::D    ooooooooooo    h::::h hhhhh
   D:::::D     D:::::D oo:::::::::::oo  h::::hh:::::hhh
   D:::::D     D:::::Do:::::::::::::::o h::::::::::::::hh
   D:::::D     D:::::Do:::::ooooo:::::o h:::::::hhh::::::h
   D:::::D     D:::::Do::::o     o::::o h::::::h   h::::::h
   D:::::D     D:::::Do::::o     o::::o h:::::h     h:::::h
   D:::::D     D:::::Do::::o     o::::o h:::::h     h:::::h
   D:::::D    D:::::D o::::o     o::::o h:::::h     h:::::h
 DDD:::::DDDDD:::::D  o:::::ooooo:::::o h:::::h     h:::::h
 D:::::::::::::::DD   o:::::::::::::::o h:::::h     h:::::h ......
 D::::::::::::DDD      oo:::::::::::oo  h:::::h     h:::::h .::::.
 DDDDDDDDDDDDD           ooooooooooo    hhhhhhh     hhhhhhh ......

Since then, some nice things have happened:

  • It’s a (somewhat documented) gem:

    $ gem install doh --source http://gems.matthewtodd.org
  • It has git support!

    Doh tries to be smart about git commit -a, staying out of your way if you’ve only modified or deleted tracked files (exactly the things -a notices). See also the test case.

git st

Posted on April 09, 2008


[shoes:~/Code/wordpress] mtodd$ git st
git: 'st' is not a git-command. See 'git --help'.
[shoes:~/Code/wordpress] mtodd$ git config --global alias.st status
[shoes:~/Code/wordpress] mtodd$ git st
# On branch master
nothing to commit (working directory clean)
[shoes:~/Code/wordpress] mtodd$