hocus_pocus
December 21st, 2010
Let me talk about a gem library which casts a spell on your Rails app and enables you to build your Rails app “on the fly” just like editing a Wiki. This is named “hocus_pocus”, and developed as an “isolated engine” for Rails 3.1. The “isolated engine” feature has been developed by Piotr Sarnacki (@drogus) as the RubySOC project #14 and is currently available on Edge Rails. I really appreciate him for creating such a nicely extensible platform and kindly answering all of my questions when we firstly met at RubyConf 2010.
How to use:
1. create a Rails 3.1 app somehow
e.g.)
1
| % rails new hocus_pocus_demo --dev -j jquery |
Note that jQuery is required ATM (this will be improved someday).
2. add a line below to your Gemfile:
1
| gem 'hocus_pocus' |
3. bundle
1
| % bundle |
4. start up your server
1
| % rails s |
that’s all!
Features:
1. request driven generator
You, well grounded Railers, always start your development with URL designing, right? You know, that’s “the Rails Way”.
So, as you decide the URL, just put it in your browser’s URL window.
This engine catches the “routing missing” sort of request, then urges you to input the parameters for the scaffold generator for the “missing” resource.
When the params are submitted, the engine kicks the scaffold generator on the server, and redirects you back to the generated index action.
This is what I meant by “request driven”. This idea is strongly inspired by “Ymir” framework, which has been developed by YOKOTA Takehiko (@i_am_skirnir) in Java language. As I took a look at this, I thought, “hey, even Java can do live code generation through browser. Why can’t Rails do that?”
2. view editor
I guess you’re always frustrated that you have to switch between your text editor and browser again and again while developing web apps.
This engine magically enables you to edit your view file dynamically on your web browser just like a Wiki by clicking the “edit” button on the top right (this is why this product was previously called “wiki_mode” engine).
Of course you can edit the partials as well.

3. url_for missing driven generator
Now, as you edit your view, you probably want to link_to or build a form_for a resource that does not exist yet, because it’s a quite natural way for editing a Wiki.
OK, now you can do it that way. Simply add a link_to like this, and submit the form.
Then another magic happens here. This time, the engine catches “url_for missing” as well, and leads you to the generator again.
4. generating nested resources
As you put a link_to a nested resource like ‘post_comments_path(@post)’,
the engine suggests you to generate a nested resource.
This generates a perfectly working set of code with the following routes.
1 2 3 | resources :posts do resources :comments end |
This is done by another library named nested_scaffold generator.
5. Steak recorder
These days we’re getting familiar with writing end-to-end testing in Ruby code. In the test code, we visit a page, fill_in text fields, select value from drop downs, click buttons, and then assert values in the page body by operating a virtual browser by Ruby.
But, wait. Do you test only by code? Aren’t you doing exactly the same thing by hand anyway? And isn’t it easier and faster to manipulate the browser than writing Ruby code?
So, isn’t it nice if an integration test scenario can automatically be generated while you operate your browser?
This engine records what you did in your browser
and outputs a scenario for Steak.
Comparing to usual generator generated test cases, this scenario might be quite useful and practical since it uses the actual data you entered.
6. Speed
This is not actually a “feature”, but an unignorable benefit of this product. Every time we hit “rails” command or “rake” command on Rails 3, we’re forced to wait several seconds for the Rails process to start up. However, because the generator is directly executed inside the running Rails server process, the hocus_pocus generator runs incredibly fast. When you run the generator from browser, it actually invokes
1 2 | % rails generate scaffold % rake db:migrate |
and usually finishes within a second on my mac.
DEMO:
Finally, to see how it works, here’s my live demonstration video at Sapporo RubyKaigi 03
Though the talk were in japanese, I think you all can get the idea since the main part of the talk were live demo.
I know it’s super buggy ATM (thus still versioned as 0.0.0), needs tons of improvement, but has a great potential to change your development style. The source code is of course available on Github.
Have fun!
This entry is for Ruby Advent Calendar jp-en: 2010 : ATND
The previous post was from takahashim: “Japanese Ruby books in 2010” (thanks for introducing “Head First Rails” at the very top!)





This guide about hocus_pocus is pretty cool and will save a lot of my time.Thanks : )
Sighs of depression, Sighs of depression, Natural remidies for depression. http://flaberb.info/?Ambieninformation/Sighsofdepression - Depression doctor information http://flaberb.info/?Ambieninformation/Depressionmajorwoman - Farmer during the great depression http://flaberb.info/?Ambieninformation/Treatinganxietydisorderswomen - Vitamin supplement for depression
Lisinopril Nolvadex pharmacies accepting cod delivery SendTo Lasix overnight delivery WHOOOOOOO Lisinopril buy Acyclovir without a prescription Infrastructure Acyclovir free fedex shipping utyae Doxycycline fedex cod house buy cod Lamictal talker ultram prescription booklist Diflucan shipped cash on delivery shallow Prednisone with next day delivery without prescription with free shipping Collection Prednisone with next day delivery Ricky pharmacy Valtrex paperbacks Trazodone next day reduce online pharmaceutical Diflucan Manpower purchase Nolvadex cod believes cod overnight Trazodone Thanksfrom soma on line vargasbrooks purchase Trazodone COD ContentsGoals buy Accutane from online pharmacy with saturday delivery Guess