<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">
 
 <title>Piotr Sarnacki</title>
 
 <updated>2012-03-25T17:21:57-07:00</updated>
 <id>http://piotrsarnacki.com/</id>
 <author>
   <name>Piotr Sarnacki</name>
   <email>drogus@gmail.com</email>
 </author>

 
 <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/piotrsarnacki" /><feedburner:info uri="piotrsarnacki" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:browserFriendly></feedburner:browserFriendly><entry>
   <title>Mountable apps tutorial</title>
   <link href="http://piotrsarnacki.com/2010/12/21/mountable-apps-tutorial" />
   <published>2010-12-21T00:00:00-08:00</published>
   <updated>2010-12-21T00:00:00-08:00</updated>
   <id>http://piotrsarnacki.com/2010/12/21/mountable-apps-tutorial</id>
   <content type="html">&lt;p&gt;I finished my work on RubySOC a few months ago and I wanted to write this post quickly after that, but life, as usual, verified my plans. Unfortunately, attending a few great conferences left me with no money, so I needed some time of hard work to pay all the debts (nevertheless, all those trips were really worth it, especially rubyconf – I really felt in love with New Orleans!). I want to apologize to everyone who really waited for that, especially those who heard that this blog post will be written &amp;#8220;very soon&amp;#8221; ;) Now&amp;#8230; let&amp;#8217;s cut the emo talk and get to serious business!&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ll start with a short introduction for those, who aren&amp;#8217;t familiar with the concept of mountable apps in Rails 3 (edge) yet. The goal for implementing mountable apps was to allow running 2 rails applications in one process. Unfortunately, that goal was not achieved and we settled on a simpler approach. If you want to create a mountable application, you can use rails engines, which will be much more powerful in Rails 3.1. For the list of major changes, please check my &lt;a href="http://piotrsarnacki.com/2010/09/14/mountable-engines"&gt;last post on that topic&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Although almost all of the changes made to engines were usable shortly after summer, the process of setting up a new engine, suitable for mounting, was really rough. Of course, there is a great gem, &lt;a href="https://github.com/josevalim/enginex"&gt;enginex&lt;/a&gt;, by José, but at that time it only generated bare structure for any kind of rails extension and there were no things specific to mountable apps (as mountable apps haven&amp;#8217;t existed during the time of writing enginex). That&amp;#8217;s why I ported enginex to rails, with some changes that were needed to merge it to the core, to make that task simpler.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s start with the tutorial. First things first. As the new APIs work only on rails edge, you need to get rails from github:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;git clone https://github.com/rails/rails.git
&lt;span class="nb"&gt;cd &lt;/span&gt;rails
bundle install
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;To generate any rails extension in rails 3.1 you can use &lt;code&gt;rails plugin new&lt;/code&gt; command. It generates a directory with lib directory, gemspec, tests and dummy application for testing. There are 2 really important things here:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;gemspec is automatically generated, unless you use &lt;code&gt;--skip-gemspec&lt;/code&gt; option &amp;#8211; I hope that this will help to move more people to gem plugins&lt;/li&gt;
	&lt;li&gt;dummy application, which by default lives in test/dummy &amp;#8211; this is standard rails application that you can test your plugin&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;To generate a mountable engine, we can use:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;bundle &lt;span class="nb"&gt;exec&lt;/span&gt; ./bin/rails plugin new ../blog --edge --mountable
&lt;span class="nb"&gt;cd&lt;/span&gt; ../blog
bundle install
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We need to add &lt;code&gt;--dev&lt;/code&gt; or &lt;code&gt;--edge&lt;/code&gt; option to ensure that Gemfile will point to rails edge version, either from local clone or github. I used edge here, cause it&amp;#8217;s better for most of you &amp;#8211; you can get newest rails version with simple &lt;code&gt;bundle update&lt;/code&gt;. Mountable option will add files that will come in handy for developing a mountable engine, such as: integration tests, &lt;code&gt;Blog::ApplicationController&lt;/code&gt;, &lt;code&gt;config/routes.rb&lt;/code&gt; and so on.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s see how the key files look like.&lt;/p&gt;
&lt;p&gt;Probably the most important file is &lt;code&gt;lib/blog/engine.rb&lt;/code&gt;, which keeps definition of the engine:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Blog&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;
    &lt;span class="n"&gt;isolate_namespace&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The thing that differs from what we could see in Rails 3.0 engine, is &lt;code&gt;isolate_namespace&lt;/code&gt; which makes engine isolated from host application. If you don&amp;#8217;t remember how it works exactly, please check &lt;a href="https://github.com/rails/rails/blob/master/railties/lib/rails/engine.rb#L210"&gt;the documentation&lt;/a&gt; or &lt;a href="http://piotrsarnacki.com/2010/09/14/mountable-engines"&gt;my last post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Next important thing is &lt;code&gt;config/routes.rb&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;These are empty routes, but as you can see, they belong to the engine, not to the host application.&lt;/p&gt;
&lt;p&gt;The last thing that you may not be familiar with is the dummy application located in &lt;code&gt;test/dummy&lt;/code&gt;. This is a standard rails application that will be used to test engine, both with automated tests and manually during development. The nice thing about the new plugin generator is that with &lt;code&gt;--mountable&lt;/code&gt; option, it automatically mounts engine in &lt;code&gt;test/dummy/config/routes.rb&lt;/code&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Ok, let&amp;#8217;s write some code! Or&amp;#8230; actually don&amp;#8217;t write code, I&amp;#8217;m too lazy, I&amp;#8217;m gonna use scaffold generator:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;rails g scaffold post title:string body:text
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;While the scaffold should behave exactly the same as the one generated in regular app, there are some differences. Notice that migration for that is called &lt;code&gt;create_blog_posts&lt;/code&gt; instead of &lt;code&gt;create_posts&lt;/code&gt;. Also almost all of the files are places in &lt;code&gt;blog/&lt;/code&gt; subdirectory. When you open &lt;code&gt;app/models/blog/post.rb&lt;/code&gt;, you will see:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Blog&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Post&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Everything is namespaced for a good reason: we want to avoid conflicts between engine and host application.&lt;/p&gt;
&lt;p&gt;Ok, now we can migrate our database (sqlite3 by default):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;rake db:migrate
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Engine&amp;#8217;s Rakefile includes all the tasks that are needed to manage database. Migrations are run from both engine&amp;#8217;s and dummy&amp;#8217;s application directories to make development and testing easier. Finally, it&amp;#8217;s time to check it!&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;rails s
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now point your browser to &lt;a href="http://localhost:3000/blog/posts"&gt;http://localhost:3000/blog/posts&lt;/a&gt;. You should see the standard rails scaffold working – the only difference is that it&amp;#8217;s namespaced with &lt;code&gt;/blog&lt;/code&gt; path. It was easy, wasn&amp;#8217;t it?&lt;/p&gt;
&lt;p&gt;As you can see, creating a mountable application using engines is pretty straightforward. I haven&amp;#8217;t shown any way to better integrate a mountable application with the host application (e.g. using User model from the host application or adding some configuration options to the engine). It&amp;#8217;s on purpose ;) I haven&amp;#8217;t got time to develop anything good yet. You can start playing with it yourself! Be sure to let me know if you come up with some good patterns.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Lightweight controllers with rails 3</title>
   <link href="http://piotrsarnacki.com/2010/12/12/lightweight-controllers-with-rails3" />
   <published>2010-12-12T00:00:00-08:00</published>
   <updated>2010-12-12T00:00:00-08:00</updated>
   <id>http://piotrsarnacki.com/2010/12/12/lightweight-controllers-with-rails3</id>
   <content type="html">&lt;p&gt;Some time ago &lt;a href="http://piotrsarnacki.com/2010/07/31/rails3-modularity/"&gt;I showed you&lt;/a&gt;, how easily you can reuse rails 3 modules. Today I want to present lightweight controllers, which is also really really easy in rails 3.&lt;/p&gt;
&lt;p&gt;Why would you want controller to be more lightweight? Speed of course. We all know that rails can&amp;#8217;t scale and ruby is slow, let&amp;#8217;s make it a little bit faster ;)&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s start with getting familiar with new controller architecture. The base for all the controllers in rails is &lt;a href="https://github.com/rails/rails/blob/v3.0.3/actionpack/lib/abstract_controller/base.rb"&gt;&lt;code&gt;AbstractController::Base&lt;/code&gt;&lt;/a&gt;. If you want to build controllers without support for handling requests, you can start with that as a base. What if we want to create something looking more like standard rails controllers? We can check how does implementation of &lt;code&gt;ActionController::Base&lt;/code&gt; look like: &lt;a href="https://github.com/rails/rails/blob/v3.0.3/actionpack/lib/action_controller/base.rb#L169"&gt;&lt;code&gt;ActionController::Base&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;As you can see, it inherits from &lt;code&gt;ActionController::Metal&lt;/code&gt; and &lt;a href="https://github.com/rails/rails/blob/v3.0.3/actionpack/lib/action_controller/base.rb#L180-221"&gt;includes bunch of modules&lt;/a&gt;. Looks like Metal will be a good start for our purpose. Let&amp;#8217;s say that we want to render a simple &lt;span class="caps"&gt;JSON&lt;/span&gt; response generated from a model:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# app/controllers/api_controller.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ApiController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Metal&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rendering&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
    &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:text&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Good morning!&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see I created controller that inherits from &lt;code&gt;ActionController::Metal&lt;/code&gt; and included &lt;code&gt;ActionController::Rendering&lt;/code&gt;. To render simple &lt;span class="caps"&gt;JSON&lt;/span&gt; response, we will not need anything more.&lt;/p&gt;
&lt;p&gt;How fast is it comparing to &lt;code&gt;ActionController::Base&lt;/code&gt;? For comparison I&amp;#8217;ve created controller with exactly the same action:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# app/controllers/home_controller.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;HomeController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
    &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;:text&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Good morning!&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
I&amp;#8217;ve done a few benchmarks, but don&amp;#8217;t take it too seriously, these are not scientific, I just wanted to show you how approximately can it differ. Here are ab results: &lt;a href="https://gist.github.com/738168"&gt;https://gist.github.com/738168&lt;/a&gt;. As you can see, &lt;code&gt;ActionController::Metal&lt;/code&gt; is almost 40% faster. This can of course vary, depending on version of ruby, concurrency (I used 1) and other factors, but clearly it can give you nice boost.
&lt;p&gt;Of course trained eye can easily spot an issue here. Do you think that someone will actually return a response like that? Hardcoded string?Srsly? I guess not. But there certainly are situations, when you can profit from using that kind of controllers. If all you need to do, is to return text response, that most of the time is fetched from cache and is hit frequently, this should work pretty well. Some of you may also ask, &lt;del&gt;why can&amp;#8217;t we use &lt;a href="http://weblog.rubyonrails.org/2008/12/17/introducing-rails-metal"&gt;Rails Metal&lt;/a&gt;, which is available for quite some time now&lt;/del&gt; &lt;ins&gt;rack middleware&lt;/ins&gt; (rails metal was removed in rails 3). We could, but then we loose possibility to use router and include other modules to &lt;code&gt;ActionController::Metal&lt;/code&gt; (like &lt;code&gt;ActionController::Helpers&lt;/code&gt; or other useful things).&lt;/p&gt;
&lt;p&gt;I hope that this post will help you to experiment with new Rails 3 cool features! If you want more of that stuff, in better form and with much better explanation and depth, I highly recommend reading José Valim&amp;#8217;s book &lt;a href="http://pragprog.com/titles/jvrails/crafting-rails-applications"&gt;Crafting Rails Applications&lt;/a&gt;!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Mountable engines - RSoC Wrap Up</title>
   <link href="http://piotrsarnacki.com/2010/09/14/mountable-engines" />
   <published>2010-09-14T00:00:00-07:00</published>
   <updated>2010-09-14T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2010/09/14/mountable-engines</id>
   <content type="html">&lt;p&gt;As some of you probably know, during the summer I was working on &amp;#8220;Rails mountable applications&amp;#8221; project thanks to Ruby Summer of Code. It was great experience, I learned a lot new things and I hope that I delivered something useful for the community. I tweeted that the other day, but I must emphasize: it would not be possible without great support from my mentors: Carl Lerche, Yehuda Katz and José Valim! They helped me a lot with both ideas and implementation. The biggest internet hug should go to José, who spent enormous amount of his time on discussions, reviewing my commits and helping me to set the goals. I also want to thank all the sponsors and people that helped to organize Ruby Summer of Code.&lt;/p&gt;
&lt;p&gt;Getting back to my work. My main task was to extend capabilities of rails engines. Although at first I wanted to get straight to mountable full rails applications, after discussions with Carl and José, I knew that starting with the smaller target will be more sane way to go. The biggest problem with implementing mountable applications (that is running more than one rails application in same process) is configuration and application initialization in general. Right now config values are shared between all the railties classes, because they&amp;#8217;re kept in class variables. The other problem with mountable apps is that they&amp;#8217;re pretty new concept in rails community and we will need some time to settle standards. With those problems in mind, it is much better to test the concept with engines and implement truly mountable applications later, based on results and feedback. Right now, engines are almost as powerful as applications. The main difference is that engine can&amp;#8217;t be run without an application. The best thing about such strategy is that &lt;span class="caps"&gt;API&lt;/span&gt; for using more than one application is already here, so if it is needed, converting engine to application will be as easy as changing a bunch of configuration files.&lt;/p&gt;
&lt;p&gt;In that post, I would like to briefly describe my changes.&lt;/p&gt;
&lt;h2&gt;Extending engines&lt;/h2&gt;
&lt;p&gt;First thing needed to allow build entire applications on engines, was to add some of the application&amp;#8217;s features to engines. That&amp;#8217;s why engines got:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;its own middleware stack&lt;/li&gt;
	&lt;li&gt;routes&lt;/li&gt;
	&lt;li&gt;plugins support&lt;/li&gt;
	&lt;li&gt;config/environment.rb&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With all the capabilities that engines had before, engine is now almost as powerful as application.&lt;/p&gt;
&lt;h2&gt;Mounting engine&lt;/h2&gt;
&lt;p&gt;Since engine is now a rack app, you can simply mount it in your application&amp;#8217;s routes:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This will mount &lt;code&gt;Blog::Engine&lt;/code&gt; at &lt;code&gt;/blog&lt;/code&gt; path. There are 2 huge benefits of such method:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;you can change the engine&amp;#8217;s path, which was much harder before&lt;/li&gt;
	&lt;li&gt;you can use all of the routes magic&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Want to mount your engine with dynamic scope? No problem:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/:username&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;default&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also use devise to force authentication for the engine:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;authenticate&lt;/span&gt; &lt;span class="ss"&gt;:admin&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Tolk&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/tolk&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;It&amp;#8217;s easy, isn&amp;#8217;t it?&lt;/p&gt;
&lt;h2&gt;Cross applications routes &lt;span class="caps"&gt;API&lt;/span&gt;&lt;/h2&gt;
&lt;p&gt;Since you can mount engine with its own router inside application, there is a possibility of having more than one router in your app. To handle all the routers easily, there are new helpers providing access for each router. Application&amp;#8217;s router is always available as &lt;code&gt;main_app&lt;/code&gt;. That said, you can call any application route from engine just like that:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;main_app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;logout_path&lt;/span&gt;
&lt;span class="n"&gt;main_app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;root_path&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Helpers for other routers are available after mounting an engine:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;# default helper for such engine is &amp;quot;blog&amp;quot;:&lt;/span&gt;
&lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;posts_path&lt;/span&gt;
&lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;root_path&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If you need to change the helper name, just pass the &lt;code&gt;:as&lt;/code&gt; attribute:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:as&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;my_blog&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;# now helper is called my_blog:&lt;/span&gt;
&lt;span class="n"&gt;my_blog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;posts_path&lt;/span&gt;
&lt;span class="n"&gt;my_blog&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;root_path&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;You can also use those helpers in polymorphic url (which is used, among the other places, in &lt;code&gt;form_for&lt;/code&gt;):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;polymorphic_url&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vi"&gt;@post&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;form_for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="n"&gt;blog&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="vi"&gt;@post&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Note that you have to explicitly add those helpers to &lt;code&gt;polymorphic_url&lt;/code&gt; only when you need to call an engine route from your application or from another engine.&lt;/p&gt;
&lt;h2&gt;Namespacing&lt;/h2&gt;
&lt;p&gt;Having more than one source of controllers, models and helpers can cause conflicts. Imagine you have &lt;code&gt;Post&lt;/code&gt; model in your application and you would like to install blog engine having model with the same name. To avoid that, you can put your engine inside the namespace. Although it will help you with conflicts, it&amp;#8217;s not enough level of separation for some engines. There are basically two possible scenarios here. One of them is shared engine, when you want to share helpers between engine and application. A good example of such engine is &lt;a href="http://github.com/plataformatec/devise"&gt;Devise&lt;/a&gt;. The other use case is when you want to make isolated engine, which will not likely share anything. It could be an engine that provides tools for application like &lt;a href="http://github.com/dhh/tolk"&gt;Tolk&lt;/a&gt; or the other app like blog or &lt;span class="caps"&gt;CMS&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;Engines are shared by default, therefore you must explicitly mark it as isolated with &lt;code&gt;isolate_namespace&lt;/code&gt; method:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Blog&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;
    &lt;span class="n"&gt;isolate_namespace&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With such engine definition, only the engine&amp;#8217;s helpers and routes will be included inside engine&amp;#8217;s controllers and views.&lt;/p&gt;
&lt;h2&gt;Migrations&lt;/h2&gt;
&lt;p&gt;If you are one of NoSQL (or rather schema less) database users, you can probably skip that point. In other cases you will probably need migrations. We decided that the easiest way to handle engine&amp;#8217;s migrations is to copy them to application&amp;#8217;s db/migrate directory and change their timestamps for not breaking the migrations timeline. It can be done by simply calling rake task:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;rake railties:copy_migrations
&lt;span class="c"&gt;# or to copy only selected engines&lt;/span&gt;
rake railties:copy_migrations &lt;span class="nv"&gt;RAILTIES&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;foo,bar
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The nice side effect of copying migrations is that you can easily review them before applying.&lt;/p&gt;
&lt;h2&gt;Assets&lt;/h2&gt;
&lt;p&gt;The chances are that you will have some assets in your engine&amp;#8217;s public directory. The default way of serving assets in development mode in Rails is &lt;code&gt;ActionDispatch::Static&lt;/code&gt; middleware. If you have mounted any engine, it will automagically serve their assets. In production you have 2 options:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;stay with &lt;code&gt;ActionDispatch::Static&lt;/code&gt; by turning it on with: &lt;code&gt;config.serve_static_assets = true&lt;/code&gt; in your &lt;code&gt;environment.rb&lt;/code&gt;&lt;/li&gt;
	&lt;li&gt;create symlinks to engine&amp;#8217;s public directories&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can automatically create symlinks with a rake task:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;rake ralties:create_symlinks
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;If you want to change the default asset path, you can set it in &lt;code&gt;Engine&lt;/code&gt; definition:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Blog&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;
    &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;asset_path&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/my_blog_assets%s&amp;quot;&lt;/span&gt; &lt;span class="c1"&gt;# note %s at the end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This will change both &lt;code&gt;ActionDispatch::Static&lt;/code&gt;&amp;#8217;s and create_symlinks rake behavior.&lt;/p&gt;
&lt;h2&gt;Why does it matter?&lt;/h2&gt;
&lt;p&gt;Although engines are available since Rails 2.3 and even ealier as a plugin, building isolated applications using them can be hard. With new &lt;span class="caps"&gt;API&lt;/span&gt;, building entire apps (like forum, blog or &lt;span class="caps"&gt;CMS&lt;/span&gt;) that can be reused, will be much easier. But hey, big components are evil, right? I would say that it depends on your needs. There is huge space for engines as tools to help develop your application. If you use i18n, there is &lt;a href="http://github.com/dhh/tolk"&gt;Tolk&lt;/a&gt;. Most of applications could probably benefit from &lt;a href="http://github.com/sferik/rails_admin"&gt;Rails Admin&lt;/a&gt; (it&amp;#8217;s also one of the RSoC projects). Also the new Carlhuda&amp;#8217;s ;-) work on &lt;a href="http://github.com/wycats/rails_assets"&gt;Rails 3.1 assets&lt;/a&gt; is based on engines.&lt;/p&gt;
&lt;p&gt;Another topic are applications. You can argue that making flexible application that would suit needs of many developers will be hard and inefficient&amp;#8230; and you are probably right. However there are many situations when you need to quickly add some &lt;span class="caps"&gt;CMS&lt;/span&gt; or small forum to your application and you pretty much do not care about features. It&amp;#8217;s not your core product, you just need something simple. Of course you can always do it by yourself, but what are the benefits? If you&amp;#8217;re not trying to revolutionize &lt;span class="caps"&gt;CMS&lt;/span&gt; systems and just want an easy way to add a few articles to your site, you can use something generic. If it&amp;#8217;s really needed, you can always write your own better suited engine later on and replace the previous one.&lt;/p&gt;
&lt;h2&gt;What&amp;#8217;s next?&lt;/h2&gt;
&lt;p&gt;My work is already merged to rails master and most of the things are finished, but there are still some places that need more work. If you have any suggestions on engines, feel free to comment, add a ticket on lighthouse or send a pull request with changes.&lt;/p&gt;
&lt;p&gt;There is also need for engines creators that will battle test the new &lt;span class="caps"&gt;API&lt;/span&gt; and new concepts. If you have any questions feel free to contact me on &lt;a href="http://twitter.com/drogus"&gt;twitter&lt;/a&gt;, &lt;a href="http://github.com/drogus"&gt;github&lt;/a&gt; or &lt;a href="mailto:drogus@gmail.com"&gt;by email&lt;/a&gt;. I will also prepare a guide shortly, so stay tuned!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>RSoC status: Namespacing engines</title>
   <link href="http://piotrsarnacki.com/2010/09/06/rsoc-status-namespacing-engines" />
   <published>2010-09-06T00:00:00-07:00</published>
   <updated>2010-09-06T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2010/09/06/rsoc-status-namespacing-engines</id>
   <content type="html">&lt;p&gt;It&amp;#8217;s been a while since my last post. After finalizing my work on routes and few other things I went to Croatia for 2 weeks, to rest from the computer and programming. But fear not, I will not count that 2 weeks into RSoC time and I will work for 2 weeks longer to not shorten actual development time.&lt;/p&gt;
&lt;p&gt;The big news is: my work has been merged to rails official repository recently! That means that you can start using the things that I described right away (you just need to use edge version of rails).&lt;/p&gt;
&lt;p&gt;The next thing that needed to be done to allow mounting engines easily is namespacing. Namespacing is crucial to avoid conflicts between engines and application itself. Imagine that you have application with Article and Comment models and you want to mount blog engine into it. The chances are, blog engine will also have Comment model and corresponding controller and helper. Without namespacing such configuration would end up with conflicts. To avoid that, we can put all of the things in an engine into namespace:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# ENGINE/app/models/blog/comment.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Blog&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Comment&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActiveRecord&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# ENGINE/app/controllers/blog/comments_controller.rb&lt;/span&gt;
&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Blog&lt;/span&gt;
  &lt;span class="c1"&gt;# note that ApplicationController here is in fact Blog::ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CommentsController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;At first, it may seem that this will not require any changes to Rails, it&amp;#8217;s standard Ruby technique, right? To some degree it&amp;#8217;s true, but there are some places that made it hard to use namespaced engines before.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s look at controllers as an example. In rails 3.0, when you create any class that inherits from &lt;code&gt;ActionController::Base&lt;/code&gt;, it will get all of the application helpers. It&amp;#8217;s default behavior and it&amp;#8217;s perfectly ok when you have only one application. Problems appear when you want to mount engine with its own controllers and helpers. With Rails 3.0 it will be a bit hard. Even if you namespace your controllers and helpers, you will get all application&amp;#8217;s helpers instead of engine&amp;#8217;s ones. The simple solution would be to load only helpers within current namespace, but that would break some of the existing engines.&lt;/p&gt;
&lt;p&gt;There are basically 2 types of engines. Some of the engines are considered to be a part of application, they provide helpers and controllers that will be used within application and there is no need to isolate them (take &lt;a href="http://github.com/plataformatec/devise"&gt;Devise&lt;/a&gt; as example of such engine). I will call it &amp;#8220;shared engine&amp;#8221;. The second use case is engine that should not share anything, like blog engine. I will call such constructs &amp;#8220;isolated engines&amp;#8221;.&lt;/p&gt;
&lt;p&gt;The problem with having shared and isolated engines is the fact that in both situations you can namespace stuff inside, but the behavior should be slightly different. While you would probably like to share helpers from shared engine, even if they&amp;#8217;re namespaced, you would not want that in case of isolated engine. To distinguish this situations you need to explicitly say that engine is isolated. Otherwise it will be shared. To make engine isolated, just use &lt;code&gt;isolate_namespace()&lt;/code&gt; method in Engine definition:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Blog&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;
    &lt;span class="n"&gt;isolate_namespace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With such code, all of the stuff namespaced with &lt;code&gt;Blog&lt;/code&gt; will be isolated. There are quite a few effects of that. As I mentioned before, one of the problems are controllers. With engine explicitly marked as namesapaced, controllers will include only helpers from within the same namespace and they will include only an engine&amp;#8217;s routes.&lt;/p&gt;
&lt;p&gt;The next thing is router itself. When you have every controller inside the module, normally you would have to reflect that in routes:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;scope&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:module&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;:blog&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:posts&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This code will ensure that &lt;code&gt;posts&lt;/code&gt; resource points to &lt;code&gt;Blog::PostsController&lt;/code&gt;. With isolated engine this is not needed at all. You can ommit that scope, cause it will be applied automagically behind the scene. With engine marked as isolated you can just do:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="c1"&gt;# everything here is namespaced with :blog namespace&lt;/span&gt;
  &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:posts&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The next thing to change was &lt;code&gt;ActiveModel::Naming&lt;/code&gt;. It is used in many places in rails and by default it leaves namespace, e.g. it would convert &lt;code&gt;Blog::Post&lt;/code&gt; to &lt;code&gt;blog_post&lt;/code&gt;. While in some places it&amp;#8217;s ok, it can be a major &lt;span class="caps"&gt;PITA&lt;/span&gt; in other ones. The first pain point is &lt;code&gt;polymorphic_url&lt;/code&gt;, which is used among the other things in &lt;code&gt;form_for&lt;/code&gt;. Let&amp;#8217;s consider example of &lt;code&gt;Blog::Post&lt;/code&gt; model:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# @post = Blog::Post.new&lt;/span&gt;

&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;%= form_for(@post) do |f|%&amp;gt;&lt;/span&gt;
&lt;span class="sx"&gt;  &amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;f&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text_field&lt;/span&gt; &lt;span class="ss"&gt;:title&lt;/span&gt; &lt;span class="sx"&gt;%&amp;gt;&lt;/span&gt;
&lt;span class="sx"&gt;  &amp;lt;%= f.submit %&amp;gt;&lt;/span&gt;
&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sx"&gt;% end &lt;/span&gt;&lt;span class="o"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Without any changes in &lt;code&gt;ActiveModel::Naming&lt;/code&gt;, such code would try to use &lt;code&gt;blog_posts_path&lt;/code&gt; helper. Using namespace here is not needed at all, as you can&amp;#8217;t have a conflict with helpers from other routers. That&amp;#8217;s why in isolated engine &lt;code&gt;form_for(@post)&lt;/code&gt; will use &lt;code&gt;posts_path&lt;/code&gt; and thanks to that, we didn&amp;#8217;t need to add that prefix to all the routes.&lt;/p&gt;
&lt;p&gt;The next thing connected with forms and model name are param names. Normally, the last piece of code would generate field &lt;code&gt;&amp;lt;input type="text" name="blog_post[title]" id="blog_post_title" /&amp;gt;&lt;/code&gt;. That kind of prefixes are also not needed as it&amp;#8217;s highly improbable that you would need &lt;code&gt;params[:post]&lt;/code&gt; for some other thing (and even if it is the case for you, you can use &lt;code&gt;:as =&amp;gt; :some_other_name&lt;/code&gt; to change the default). The &lt;code&gt;blog_&lt;/code&gt; is also omitted here and in isolated engine the field would look just like that: &lt;code&gt;&amp;lt;input type="text" name="post[title]" id="post_title"/&amp;gt;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;All of the other places where &lt;code&gt;ActiveModel::Naming&lt;/code&gt; is used, like partials, generating dom elements or i18n, take the namespace into account.&lt;/p&gt;
&lt;p&gt;As you can see, there was quite a few changes in Rails code to handle namespaces, but in the end they&amp;#8217;re pretty transparent to for the developer.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Rails3 modularity</title>
   <link href="http://piotrsarnacki.com/2010/07/31/rails3-modularity" />
   <published>2010-07-31T00:00:00-07:00</published>
   <updated>2010-07-31T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2010/07/31/rails3-modularity</id>
   <content type="html">&lt;p&gt;Since I spend a lot more time with Rails 3 codebase, I am more and more amazed by its modularity. Rails 3 is much more extensible than I would have ever thought. As a part of my rubysoc project I wanted to port merb-parts to Rails 3. Some of you may never heard of merb parts and ask what the hell are parts? Do I need it? Parts are a bit like controllers, but much more lightweight and without overhead of handling requests. I like to think about parts as powerful partials. If you have a partial and you would like to add a bit more logic to it or fetch something from database, you could probably benefit from using parts. Let&amp;#8217;s go with a simple example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# app/parts/articles_part.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ArticlesPart&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Parts&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
    &lt;span class="vi"&gt;@articles&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Article&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;limit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:limit&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;created_at DESC&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="erb"&gt;&lt;span class="x"&gt;# app/parts/views/articles_part/index.html.erb&lt;/span&gt;
&lt;span class="x"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
&lt;span class="x"&gt;  &lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="vi"&gt;@articles&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="x"&gt;&lt;/span&gt;
&lt;span class="x"&gt;    &amp;lt;li&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;article&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="x"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="x"&gt;  &lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="x"&gt;&lt;/span&gt;
&lt;span class="x"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This simple part will fetch last &lt;code&gt;params[:limit]&lt;/code&gt; articles or 10 if limit is not provided. Let&amp;#8217;s use it in our view:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="erb"&gt;&lt;span class="x"&gt;  &lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;part&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ArticlesPart&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;:index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:limit&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="x"&gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Such call will render list of last 5 articles.&lt;/p&gt;
&lt;p&gt;The question is: how much lines of code does it take to implement something like that with ability to render views, layouts, &lt;code&gt;:inline&lt;/code&gt;, use helpers, filters and much more? Over 100 lines of code including &lt;code&gt;part()&lt;/code&gt; helper and a railtie (railtie is used to plug it into rails) &amp;#8211; if you don&amp;#8217;t believe me grab the &lt;a href="http://github.com/drogus/rails-parts"&gt;repo&lt;/a&gt; and check it yourself.&lt;/p&gt;
&lt;p&gt;How is it possible? Let&amp;#8217;s look at the implementation of &lt;code&gt;Parts::Base&lt;/code&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;parts/default_layout&amp;#39;&lt;/span&gt;

&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Parts&lt;/span&gt;
  &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Base&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;AbstractController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
    &lt;span class="kp"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:params&lt;/span&gt;

    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;AbstractController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Layouts&lt;/span&gt;
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;AbstractController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Translation&lt;/span&gt;
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Helpers&lt;/span&gt;
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;AbstractController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Rendering&lt;/span&gt;
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ImplicitRender&lt;/span&gt;
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;DefaultLayout&lt;/span&gt;
    &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;AbstractController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Callbacks&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="vi"&gt;@params&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dup&lt;/span&gt;
      &lt;span class="vi"&gt;@params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;merge!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;empty?&lt;/span&gt;
      &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;formats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;controller&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;formats&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;

    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;inherited&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;klass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="k"&gt;super&lt;/span&gt;
      &lt;span class="n"&gt;klass&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;helper&lt;/span&gt; &lt;span class="ss"&gt;:all&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That&amp;#8217;s all&amp;#8230; ? Yes!&lt;/p&gt;
&lt;p&gt;As you cans see &lt;code&gt;Parts::Base&lt;/code&gt; inherits from &lt;code&gt;AbstractController::Base&lt;/code&gt;, which gives it really basic functionality. Additionaly a few helpers are included to add a bit more behavior. The only mixin that I needed to create myself is &lt;code&gt;Parts::DefaultLayouts&lt;/code&gt; which ensures that layout with the name of the part is rendered by default, unless &lt;code&gt;:layout =&amp;gt; false&lt;/code&gt; or there is no such layout in layouts directory.&lt;/p&gt;
&lt;p&gt;Ok, that all is nice and dandy, but how does it work internally?&lt;/p&gt;
&lt;p&gt;The implementation of such pattern can be demonstrated with such code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;foo&amp;#39;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Bar&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;bar&amp;#39;&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Baz&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;baz&amp;#39;&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Omg&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Foo&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Bar&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Baz&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;omg&amp;#39;&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Omg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;foo&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; omg&lt;/span&gt;
            &lt;span class="c1"&gt;#   baz&lt;/span&gt;
            &lt;span class="c1"&gt;#   bar&lt;/span&gt;
            &lt;span class="c1"&gt;#   foo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;There are 2 modules (Bar and Baz) and 2 classes (Foo and Omg) implementing foo method here. Omg class inherits from Foo and additionaly modules Bar and Baz are included in Omg. This code takes advantage of ruby object model. How does it work?&lt;/p&gt;
&lt;p&gt;When you instantinate Omg object with &lt;code&gt;Omg.new&lt;/code&gt; and call method foo on it, it looks for foo method in current class and its superclasses. So first method that will be actually called is Omg#foo. This method is calling super, so ruby will look for method foo also in Omg&amp;#8217;s superclass. At first you could think that it&amp;#8217;s Foo, but internally Ruby treats modules as superclasses. That said, the next superclass will be the last included module, which is Baz. After that Bar&amp;#8217;s and Foo&amp;#8217;s methods will be invoked.&lt;/p&gt;
&lt;p&gt;This one of the best patterns I have seen in Ruby so far. Not only it allows to extend objects easily, but also to reuse small chunks of code making the whole thing more modular. If Rails would stop at standard inheritance that we know from Java and other similar OO languages, creating something like parts would be much harder. In &lt;code&gt;Parts::Base&lt;/code&gt; I used a few modules from AbstractController, but also a module from ActionController. With implementation without mixins, I would need to take all or nothing approach.&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;re a Ruby developer, please think about that pattern while writing your next library. It can really help to make your code easier to extend and reuse. I was amazed how easy it was to implement all that stuff and I love the way that Rails is going!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>RSoC status: routes (a.k.a OMG it's hard)</title>
   <link href="http://piotrsarnacki.com/2010/07/20/rsoc-status-routes-aka-omg-it-s-hard" />
   <published>2010-07-20T00:00:00-07:00</published>
   <updated>2010-07-20T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2010/07/20/rsoc-status-routes-aka-omg-it-s-hard</id>
   <content type="html">&lt;p&gt;In my last post I briefly described some of the RSoC changes and plans. One of the things that I left is router. The topic is much harder and I think it deserves separate blog post.&lt;/p&gt;
&lt;p&gt;Basically, the problem with engines and routes is that now you can have more than one router. That leads to many issues that need to be solved. As with my last post, things described here are not in Rails core yet, so you will have to wait a bit to actually use it.&lt;/p&gt;
&lt;p&gt;For the good start, let&amp;#8217;s identify things to consider:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;recognition&lt;/li&gt;
	&lt;li&gt;generation&lt;/li&gt;
	&lt;li&gt;named routes from more than one router (for example &lt;code&gt;posts_path&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Recognition&lt;/h2&gt;
&lt;p&gt;At the beginning I would like to explain how does recognition work. Here is a simple example of engine mounted in application:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# APP/config/routes.rb&lt;/span&gt;
&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:users&lt;/span&gt;
  &lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# ENGINE/config/routes.rb&lt;/span&gt;
&lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:posts&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you can see engine provides :posts resource and it&amp;#8217;s mounted at /blog path. Let&amp;#8217;s study simple request to our blog:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;GET&lt;/span&gt;&lt;span class="sr"&gt; /blog/&lt;/span&gt;&lt;span class="n"&gt;posts&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Application&amp;#8217;s router will match the first part of the path, which is &lt;code&gt;/blog&lt;/code&gt;, with &lt;code&gt;/blog&lt;/code&gt; mount point and pass the request to rack app mounted there (which in that case is &lt;code&gt;Blog::Engine&lt;/code&gt;). To allow engine recognizing path properly it needs to pass &lt;code&gt;/posts&lt;/code&gt; as a path. The &lt;code&gt;/blog&lt;/code&gt; (which I will call prefix later on) is not a part of mounted engine, but we don&amp;#8217;t want to loose that information by simply removing it from &lt;span class="caps"&gt;PATH&lt;/span&gt;. In that case &lt;code&gt;/blog&lt;/code&gt; prefix is attached to &lt;code&gt;env["SCRIPT_NAME"]&lt;/code&gt; (&lt;code&gt;SCRIPT_NAME&lt;/code&gt; is part of &lt;a href="http://rack.rubyforge.org/doc/files/SPEC.html"&gt;rack&amp;#8217;s spec&lt;/a&gt;). That way, engine will get only &lt;code&gt;/posts&lt;/code&gt; part as &lt;span class="caps"&gt;PATH&lt;/span&gt;, which will allow to properly recognize it.&lt;/p&gt;
&lt;p&gt;The only one problem with recognition is connected with rack middlewares. Consider such example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# APP/config/routes.rb&lt;/span&gt;
&lt;span class="no"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;
  &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog/omg&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;omg#index&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# APP/app/controllers/omg_controller.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OmgController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;SomeMiddleware&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# ENGINE/lib/blog/engine.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;
  &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;SomeMiddleware&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;What will happen when you will request &lt;code&gt;/blog/omg&lt;/code&gt; path? It will call the &lt;code&gt;Blog::Engine&lt;/code&gt; first, as it has higher priority than &lt;code&gt;/blog/omg&lt;/code&gt;. Request will pass through the Engine&amp;#8217;s middleware stack firing SomeMiddleware and hit the Engine&amp;#8217;s router. If &lt;code&gt;/omg&lt;/code&gt; is not a valid route for &lt;code&gt;Blog::Engine&lt;/code&gt; it will return 404 and Application&amp;#8217;s router will try next route, that is &lt;code&gt;/blog/omg&lt;/code&gt; pointing to &lt;code&gt;OmgController&lt;/code&gt;. Middleware stack for that controller also includes &lt;code&gt;SomeMiddleware&lt;/code&gt; so it will be fired again, which is probably not something that we want.&lt;/p&gt;
&lt;p&gt;The solution is to mount apps with lower priority than other routes.&lt;/p&gt;
&lt;h2&gt;Generation&lt;/h2&gt;
&lt;p&gt;The problem with generation is related to mount point, the place where engine is mounted. Consider such example:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# APP/config/routes.rb&lt;/span&gt;
&lt;span class="no"&gt;MyRailsApp&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;match&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/&amp;quot;&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;users#index&amp;quot;&lt;/span&gt;

  &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/:user&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;drogus&amp;quot;&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# ENGINE/lib/blog/engine.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# ENGINE/config/routes.rb&lt;/span&gt;
&lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:posts&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;Blog::Engine&lt;/code&gt; is mounted at &lt;code&gt;"/:user/blog"&lt;/code&gt; (let&amp;#8217;s just suppose that blog implements multi user setup) and it provides &lt;code&gt;posts&lt;/code&gt; resources. As you can remember from recognition part, &lt;code&gt;/:user/blog&lt;/code&gt; part is engine&amp;#8217;s prefix. Additionally, default user is set to &amp;#8220;drogus&amp;#8221;, so when :user is not specified it will be set to drogus. Imagine that you need to generate path to posts. Something that you would normally achieve by calling &lt;code&gt;posts_path&lt;/code&gt; method. With more than one router the situation is not so simple. We can&amp;#8217;t simply use named url helpers (like &lt;code&gt;posts_path&lt;/code&gt;) inapplication&amp;#8217;s controllers, so we need some other way to handle that (if you&amp;#8217;re wondering why can&amp;#8217;t we just use &lt;code&gt;posts_path&lt;/code&gt;, it&amp;#8217;s explained in named routes section of this post).&lt;/p&gt;
&lt;p&gt;First problem connected with that is &lt;span class="caps"&gt;API&lt;/span&gt;. Jeremy Kemper came up with helper that will allow using mounted engine&amp;#8217;s url helpers by simply calling &lt;code&gt;some_engine.posts_path&lt;/code&gt;. The name of that helper is taken from Engine&amp;#8217;s name or from &lt;code&gt;:as&lt;/code&gt; option used in mount method. Because &lt;code&gt;:as&lt;/code&gt; option is not provided in my example, helper&amp;#8217;s name will be &lt;code&gt;blog_engine&lt;/code&gt; (based on &lt;code&gt;Blog::Engine&lt;/code&gt;). Using that helper we can generate paths for Engine:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;blog_engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;posts_path&lt;/span&gt;
&lt;span class="n"&gt;blog_engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url_for&lt;/span&gt; &lt;span class="vi"&gt;@post&lt;/span&gt;
&lt;span class="n"&gt;blog_engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;polymorphic_path&lt;/span&gt; &lt;span class="vi"&gt;@post&lt;/span&gt;
&lt;span class="n"&gt;blog_engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url_for&lt;/span&gt; &lt;span class="ss"&gt;:controller&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;posts&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:action&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;index&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We can also generate application&amp;#8217;s urls with &lt;code&gt;app&lt;/code&gt; helper:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;root_url&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The next problem to solve is behavior of such helpers. At first, it seems that it&amp;#8217;s fairly easy, but in reality there is quite a few cases to handle. The thing that is most important while considering url generation is context. We can generate urls in application&amp;#8217;s controllers, engine&amp;#8217;s controller&amp;#8217;s, ActionMailer or regular classes not related to Rails itself. Let&amp;#8217;s go through each case.&lt;/p&gt;
&lt;h4&gt;Generating engine&amp;#8217;s url inside application&amp;#8217;s controller&lt;/h4&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# APP/app/controllers/foo_controller.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FooController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationController&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
    &lt;span class="n"&gt;blog_engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;posts_path&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; &amp;quot;/drogus/blog/posts&amp;quot;&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Generating posts path inside one of application controllers (and consequently views and helpers) should generate prefix according to mount point. In that particular situation, default user will be inserted in place of :user, so the url will be &lt;code&gt;"/drogus/blog/posts"&lt;/code&gt;. You could also do &lt;code&gt; blog_engine.posts_path(:user =&amp;gt; "john")&lt;/code&gt;, which would generate &lt;code&gt;"/john/blog/posts"&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;Generating engine&amp;#8217;s url inside engine&amp;#8217;s controller&lt;/h4&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# ENGINE/app/controllers/posts_controller.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PostsController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url_helpers&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
    &lt;span class="n"&gt;posts_path&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; ??&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This situation is a bit different. The first thing you will probably notice is that I used posts_path instead of blog_engine.posts_path. This is possible because I included url_helpers from engine&amp;#8217;s routes. From various reasons it can&amp;#8217;t be included automagically for engine&amp;#8217;s controllers, but the plan is to make it work without any explicit includes. Why can&amp;#8217;t we just use &lt;code&gt;blog_engine.posts_path&lt;/code&gt;? In that example it would work, but note that engine can be mounted with different &lt;code&gt;:as&lt;/code&gt; option. With mount looking like:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:as&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;blog&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;you would have to use &lt;code&gt;blog.posts_path&lt;/code&gt; instead of &lt;code&gt;blog_engine.posts_path&lt;/code&gt;. Basically engine should not need to know how it&amp;#8217;s mounted. That said, we don&amp;#8217;t have any information about options used to mount it, all that we know is what&amp;#8217;s the request that was used to reach the engine.&lt;/p&gt;
&lt;p&gt;But what about the generated path? Someone could say that it should also generate prefix, but that would not work as expected. Imagine that someone requested one of your users blog with path &lt;code&gt;"/dhh/blog/posts/1"&lt;/code&gt;. When you click on link with url generated by &lt;code&gt;posts_path&lt;/code&gt;, you should stay in the same scope, so url should depend on you current path. This is achieved by using &lt;code&gt;env["SCRIPT_NAME"]&lt;/code&gt; value. In request to &lt;code&gt;"/dhh/blog/posts/1"&lt;/code&gt;, the script name would be set to &lt;code&gt;"/dhh/blog"&lt;/code&gt;, as this is the part of path that does not belong to engine. It should be clear now, that example above will generate &lt;code&gt;"/dhh/blog/posts"&lt;/code&gt; path for such request.&lt;/p&gt;
&lt;p&gt;The next thing that is worth mentioning is &lt;code&gt;_routes&lt;/code&gt; method. When you call posts_path directly, it must use routes object. This object is available through &lt;code&gt;_routes&lt;/code&gt; method. This method is defined when you include &lt;code&gt;url_helpers&lt;/code&gt;, so it points to application&amp;#8217;s routes by default. When &lt;code&gt;Blog::Engine.routes.url_helpers&lt;/code&gt; are included in PostsController, &lt;code&gt;_routes&lt;/code&gt; is changed to use engine&amp;#8217;s routes, and because of that we can use posts_path safely.&lt;/p&gt;
&lt;h4&gt;Generating application&amp;#8217;s url inside engine&amp;#8217;s controller&lt;/h4&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# ENGINE/app/controllers/posts_controller.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PostsController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
    &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;root_path&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;We would like to generate root_path from our application&amp;#8217;s router. Obviously it should generate &lt;code&gt;"/"&lt;/code&gt; path, without Engine&amp;#8217;s prefix. The only exception is situation when app is hosted in a sub path (eg /myapp). This can be done with Phusion Passenger, using RailsBaseURI option. &lt;code&gt;/myapp&lt;/code&gt; part would be passed as &lt;code&gt;SCRIPT_NAME&lt;/code&gt; in such case. Nothing complicated, we already know how to use &lt;code&gt;SCRIPT_NAME&lt;/code&gt;, right? Not really (wouldn&amp;#8217;t it be to simple? ;-). The problem is, &lt;code&gt;SCRIPT_NAME&lt;/code&gt; is kept as a string. Let&amp;#8217;s see how the request to &lt;code&gt;"/myapp/user/blog/posts"&lt;/code&gt; looks like.&lt;/p&gt;
&lt;p&gt;At first it hits the application. &lt;code&gt;SCRIPT_NAME&lt;/code&gt; is set to &lt;code&gt;/myapp&lt;/code&gt; by Passenger and the path is &lt;code&gt;/user/blog/posts&lt;/code&gt;. Now, application&amp;#8217;s router recognizes that this request should get to engine, so &lt;code&gt;Blog::Engine&lt;/code&gt; is called. As engine needs only &lt;code&gt;/posts&lt;/code&gt; as path, the prefix (&lt;code&gt;/user/blog&lt;/code&gt;) will be attached to &lt;code&gt;env["SCRIPT_NAME"]&lt;/code&gt; resulting in &lt;code&gt;/myapp/user/blog&lt;/code&gt;. As this is one string and we need to get just application&amp;#8217;s script name, solution is not obvious. How do we get the original script name? Right now our approach is to use whatever is set in &lt;code&gt;Rails.application.routes.default_url_options[:script_name]&lt;/code&gt;, so it should be set to &amp;#8220;/myapp&amp;#8221; in that case.&lt;/p&gt;
&lt;h4&gt;Generating engine&amp;#8217;s url in any other class (including ActionMailer)&lt;/h4&gt;
&lt;p&gt;In that case, url should be generated with prefix (which would be &lt;code&gt;/user/blog&lt;/code&gt; in my example). In ActionMailer we are not inside request, so &lt;code&gt;script_name&lt;/code&gt; is not available and with that in mind we need to generate the full path with &lt;code&gt;"/user/blog/"&lt;/code&gt; at the beginning.&lt;/p&gt;
&lt;h4&gt;Solution&lt;/h4&gt;
&lt;p&gt;Although it may look complicated to handle all those use cases, solution is relatively easy. The first thing to do is to assume that prefix should be always generated. This gets all cases with prefix out of our way. What&amp;#8217;s not so obvious, it also makes generating application&amp;#8217;s url in engine work. As application&amp;#8217;s routes are not mounted, prefix is nil, so we can safely add it to &amp;#8220;generate prefix&amp;#8221; case. What&amp;#8217;s left? Generating engine&amp;#8217;s url inside engine. We need to use &lt;code&gt;env["SCRIPT_NAME"]&lt;/code&gt; here. How to check if we should attach script name? We need to check if routes used to generate url are the same as routes connected with current request.&lt;/p&gt;
&lt;p&gt;Right now, to make it possible, router object is passed via &lt;code&gt;env["action_dispatch.routes"]&lt;/code&gt;. When application is called, it sets it to &lt;code&gt;Rails.application.routes&lt;/code&gt; and then when engine is called, it sets its own router there. That way, we always now in which controller are we. If &lt;code&gt;_routes&lt;/code&gt; method points to the same routes as the &lt;code&gt;env["action_dispatch.routes"]&lt;/code&gt; it means that we try to generate engine&amp;#8217;s url inside engine and we should use the &lt;code&gt;SCRIPT_NAME&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Named routes&lt;/h2&gt;
&lt;p&gt;The initial idea was to allow using named routes from 2 routers in one scope, just like that:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# APP/app/controllers/foo_controller.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FooController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionController&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="c1"&gt;# Rails.application.routes_url_helpers are included by default&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;url_helpers&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
    &lt;span class="n"&gt;posts_path&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; &amp;quot;/blog/posts&amp;quot; - path from engine&amp;#39;s router&lt;/span&gt;
    &lt;span class="n"&gt;root_path&lt;/span&gt; &lt;span class="c1"&gt;#=&amp;gt; &amp;quot;/&amp;quot; - path from application&amp;#39;s router&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Although it would be handy if you will have to use paths from mounted engine a lot, it is the cause of many issues:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;named route method must know the router that it was generated by, as now we could have multiple routers&lt;/li&gt;
	&lt;li&gt;there is a collision problem if 2 routers have the same helpers (situations when both routers will have for example posts_path is not so common, but problem will exist for root_path for sure)&lt;/li&gt;
	&lt;li&gt;currently, after including url_helpers twice &lt;code&gt;_routes&lt;/code&gt; method will point to second routes (&lt;code&gt;Blog::Engine.routes&lt;/code&gt; in last example), which will cause problems with using url helpers directly&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;There are some solutions for all of those problems, but as they&amp;#8217;re all a bit hacky and complicated, we decided to leave that topic and not allow to use helpers from 2 routers in one scope. It means that you will have to explicitly say that you want to use router other then the default one (for example by using &lt;code&gt;blog_engine.posts_path&lt;/code&gt; helper). It also means that including url_helpers from engine will overwrite current routes, so in case above, you can&amp;#8217;t directly use application routes any more. In such case you should use &lt;code&gt;app&lt;/code&gt; helper.&lt;/p&gt;
&lt;p&gt;I hope that this post is good introduction to current status of router usage in mountable apps. As usual: if you have any ideas, feature requests, critique or any other thoughts that could help bringing mountable apps to life, you&amp;#8217;re more than welcome ;-)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>RSoC status: bringing engines closer to application</title>
   <link href="http://piotrsarnacki.com/2010/07/06/rsoc-status-briging-engine-closer-to-application" />
   <published>2010-07-06T00:00:00-07:00</published>
   <updated>2010-07-06T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2010/07/06/rsoc-status-briging-engine-closer-to-application</id>
   <content type="html">&lt;p&gt;I&amp;#8217;ve been working for Ruby Summer of Code for last 2 weeks and so far it&amp;#8217;s great! In this post I will try to sum up the work on engines and outline a couple of problems that are still not solved.&lt;/p&gt;
&lt;p&gt;The first idea for RSoC was to bring Rails::Engine closer to Rails::Application. One of the long term targets is to allow to run more than one Application instance in one process. &lt;a href="http://piotrsarnacki.com/2010/06/18/rails-internals-railties/"&gt;As I described in my last post&lt;/a&gt;, application is a bit more specialized engine, so while moving most of the functionalities from Application to Engine, I could identify and solve most of the problems with running several apps in one process.&lt;/p&gt;
&lt;p&gt;First things first. What can Engine do right now and where is it used in Rails? When you drop anything in vendor/plugins directory, it will implicitly be declared as Engine. The features of engine are:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;everything in app/* works as in application&lt;/li&gt;
	&lt;li&gt;it can load config/routes.rb&lt;/li&gt;
	&lt;li&gt;config/locales/* are automatically picked by I18n&lt;/li&gt;
	&lt;li&gt;config/initializers works as in application&lt;/li&gt;
	&lt;li&gt;engines also have initializers blocks inside the Rails::Engine class to customize rails booting&lt;/li&gt;
	&lt;li&gt;you can customize paths like in application (for example change where controllers are)&lt;/li&gt;
	&lt;li&gt;it can define custom generators&lt;/li&gt;
	&lt;li&gt;rake tasks are loaded from lib/tasks&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All these features are great, but we can take it even further. Here is the plan for bringing Rails::Engine closer to Rails::Application. Rails::Engine should:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;be a Rack app&lt;/li&gt;
	&lt;li&gt;have a middleware stack&lt;/li&gt;
	&lt;li&gt;have its own routes&lt;/li&gt;
	&lt;li&gt;allow to store assets in public/&lt;/li&gt;
	&lt;li&gt;be able to run its own migrations&lt;/li&gt;
	&lt;li&gt;be able to load plugins&lt;/li&gt;
	&lt;li&gt;allow to do more configuration&lt;/li&gt;
	&lt;li&gt;allow to namespace models and controller without problems&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A few things from that list are already finished (not in rails master yet, on &lt;a href="http://github.com/drogus/rails/tree/engines"&gt;my fork&lt;/a&gt; for now). I will describe my changes, but beware, this is code that&amp;#8217;s not currently a part of rails and it can be changed before merging it to rails. Its here for getting feedback mainly.&lt;/p&gt;
&lt;p&gt;Engine can be now a rack application by providing rack endpoint:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;
  &lt;span class="n"&gt;endpoint&lt;/span&gt; &lt;span class="no"&gt;AnyRackApp&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;That code would create engine with AnyRackApp as endpoint. Now you can mount it with:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;MyRailsApp&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Mount method will tell application router that &lt;code&gt;Blog::Engine&lt;/code&gt; is located at &amp;#8220;/blog&amp;#8221; path. Let&amp;#8217;s investigate a request to &amp;#8220;/blog/posts&amp;#8221;. At first, it will hit the application and it will be passed through entire application&amp;#8217;s middleware stack. The last middleware in application is the router. Router will recognize that &amp;#8220;/blog&amp;#8221; should point to &lt;code&gt;Blog::Engine&lt;/code&gt; app, so it will pass the request to &lt;code&gt;Blog::Engine&lt;/code&gt;. Then it will be passed through Engine&amp;#8217;s middleware stack and finally it will hit Engine&amp;#8217;s rack endpoint.&lt;/p&gt;
&lt;p&gt;What’s the point of providing rack app inside engine? Probably this will not be a common scenario, but it allows you to add all functionalities provided by Engines, like initializers, generators, to your rack app. Besides, Engine has its own middleware stack now, so you can easily add some specific middlewares that will be fired only on requests for engine:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;
  &lt;span class="n"&gt;middleware&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;use&lt;/span&gt; &lt;span class="no"&gt;Rack&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Subdomain&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;By default endpoint is set to &lt;code&gt;routes&lt;/code&gt;, probably a more common scenario.&lt;/p&gt;
&lt;p&gt;Currently if you want to add your own routes, you have to hardcode it in application&amp;#8217;s routes. It&amp;#8217;s a bit limited, as you cannot easily change the place where the engine is mounted. With Engine being able to use its own router, you can mount it anywhere you want, even using dynamic scopes.&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="c1"&gt;# APP/config/routes.rb&lt;/span&gt;
&lt;span class="no"&gt;MyRailsApp&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Application&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;mount&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/blog&amp;quot;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# ENGINE/lib/blog/engine.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Rails&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;# ENGINE/config/routes.rb&lt;/span&gt;
&lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Engine&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;routes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;draw&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:posts&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;With such setup, &lt;code&gt;Blog::Engine&lt;/code&gt; is mounted at &amp;#8220;/blog&amp;#8221;. A request to &amp;#8220;/blog/posts&amp;#8221; will fire posts controller inside the engine. It’s simple as that! However, as we are going to see in another blog post, if you want to mount an engine using a dynamic route, as &amp;#8220;/:company/blog&amp;#8221;, we need to consider different scenarios on &lt;span class="caps"&gt;URL&lt;/span&gt; Recognition and Generation. Currently if you use &lt;code&gt;posts_path&lt;/code&gt;, it will generate &lt;code&gt;/posts&lt;/code&gt;. The problem is, if you&amp;#8217;re in application you should prepend prefix for mounted app, so it would be &amp;#8220;/company/blog/posts&amp;#8221;.&lt;/p&gt;
&lt;p&gt;Next important thing for engines working as full rails apps is public folder. Currently you can for example symlink engine&amp;#8217;s public folder in your application. It&amp;#8217;s ok, but there is much cleaner way to do it: use ActionDispatch::Static, which will serve static files from given directory. At first when I heard that idea, I thought that it will much slower than serving those files with web server, but according to Yehuda Katz, it should be really fast in practice and many server setups could actually cache it (for example with Varnish). If it&amp;#8217;s still not fast enough you can just not enable serving static files, make a symlink and serve it with webserver.&lt;/p&gt;
&lt;p&gt;In addition to all that features, engines can load its own set of plugins now. Right now, it&amp;#8217;s really simple implementation without any check for duplicating plugins, but I think we will consider such thing. Personally I hope that more and more people will just start using gem plugins with its own dependencies, but there are still many apps with &lt;code&gt;vendor/plugins&lt;/code&gt; full of plugins.&lt;/p&gt;
&lt;p&gt;One of the things that has not been implemented yet is migration support. There is quite long &lt;a href="https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/2058"&gt;discussion&lt;/a&gt; on it on lighthouse and a &lt;a href="http://interblah.net/plugin-migrations"&gt;blog post with a few solutions described&lt;/a&gt;, but there is sill no consensus on that one. If you have any thoughts on that topic, you can add a comment on lighthouseapp.&lt;/p&gt;
&lt;p&gt;The last problem that I will talk about (but certainly not last problem that will appear) is namespacing. For better encapsulation all of the controllers, helpers and models should be namespaced. The problem is, currently if you would namespace a controller or model it will not work as intended. Given:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;PostsController&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;Blog&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;ApplicationController&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;it will affect application in many places. Using that controller in routes will also require namespacing it with: &amp;#8220;blog/posts#index&amp;#8221;, which is probably not something that we want for all the routes. In case of models, most of ORMs will name table for &lt;code&gt;class Blog::Post&lt;/code&gt; as &amp;#8220;blog_posts&amp;#8221;. It could be actually ok, as it will help to avoid name conflicts, but it&amp;#8217;s not always desired behavior.&lt;/p&gt;
&lt;p&gt;I will appreciate any ideas and thoughts on that topic. Anything, like &lt;span class="caps"&gt;API&lt;/span&gt; ideas, feature requests or solutions to some problems will be welcome and will help me with delivering better mountable apps. Stay tuned for the next post on RSoC status: routes. I promise you will not have to wait for it for the next 2 weeks ;-)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Rails internals: Railties</title>
   <link href="http://piotrsarnacki.com/2010/06/18/rails-internals-railties" />
   <published>2010-06-18T00:00:00-07:00</published>
   <updated>2010-06-18T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2010/06/18/rails-internals-railties</id>
   <content type="html">&lt;p&gt;I&amp;#8217;m going to start my RSoC work next week, but I as a preparation for real work I started reading through Rails codebase. I will try to document my findings on this blog, I hope it will be helpful for people that would like to know Rails internals.&lt;/p&gt;
&lt;p&gt;One of the first findings of reading Rails 3 code is that it&amp;#8217;s really simple compared to previous Rails versions. Boot process doesn&amp;#8217;t need rubygems magic anymore. Ruby features are used to make Rails flexible without need of some crazy hacks. alias_method_chain is not used anymore, which makes it much easier to understand code. These are really great news for all Rails devs, especially plugins maintainers. But hey, enough already known facts, let&amp;#8217;s get to the point!&lt;/p&gt;
&lt;p&gt;The core class of Rails is Railtie. If you are a plugin developer, you will probably want to take a closer look at that. Although Railtie is not required in every plugin, it&amp;#8217;s handy if you want to hook into Rails boot process, create intializers or add a rake tasks. For more information you can read about &lt;a href="http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html"&gt;Railtie in docs&lt;/a&gt; So what&amp;#8217;s Railtie? It&amp;#8217;s a simple class that is capable of loading generators, tasks, middlewares and adding subscribers. One of the things that I&amp;#8217;ve noticed in Railtie (and almost any other class in Rails) is usage of autoload method. I&amp;#8217;ve never heard of autoload before and it seems that&amp;#8217;s very handy method. From rubydocs:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Registers filename to be loaded (using Kernel::require) the first time that module (which may be a String or a symbol) is accessed.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;  &lt;span class="nb"&gt;autoload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kp"&gt;nil&lt;/span&gt;
  &lt;span class="nb"&gt;autoload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:MyModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;/usr/local/lib/modules/my_module.rb&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The great thing in autoload is its laziness. It will not load given file, if module is not accessed. How does Rails use autoload in Railtie? It autoloads Configurable and Configuration modules. As Railtie doesn&amp;#8217;t need configuration on its own, it includes it in classes that inherit from Railtie (&lt;a href="http://github.com/rails/rails/blob/v3.0.0.beta4/railties/lib/rails/railtie.rb#L170"&gt;code&lt;/a&gt;). That way, those 2 files will not be loaded unless Railtie subclass is created. Someone could argue that Railtie will not be probably used alone, without any subclasses, as Railtie is much more like abstract class, but it&amp;#8217;s really good pattern and it&amp;#8217;s consistently used across other classes in Rails.&lt;/p&gt;
&lt;p&gt;&lt;ins&gt;I need to rectify one thing. Although autoload is very nice, it&amp;#8217;s also known to be thread unsafe and according to Carl Lerche it will be removed from Rails. Use with consideration ;-) &lt;/ins&gt;&lt;/p&gt;
&lt;p&gt;Getting back to Rails itself. The next class in hierarchy is &lt;a href="http://edgeapi.rubyonrails.org/classes/Rails/Engine.html"&gt;Engine&lt;/a&gt;. Engine is much closer to full stack Rails app. In fact application is a bit more specialized Engine. That makes sharing an application as easy as converting it to engine. So, what&amp;#8217;s Engine? Basically it&amp;#8217;s Railtie with ability to set load paths for views, controllers, helpers, locales etc. That&amp;#8217;s it? Nothing more? That simple? Yep, that&amp;#8217;s right.&lt;/p&gt;
&lt;p&gt;Now it&amp;#8217;s time for Application. Application is a subclass of Engine and it&amp;#8217;s capable of booting the Rails app. As you can &lt;a href="http://edgeapi.rubyonrails.org/classes/Rails/Application.html"&gt;read in the docs&lt;/a&gt;, Application is singletone and that&amp;#8217;s why 2 Rails apps can&amp;#8217;t be run in a single process. So what does exactly Rails application do?&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;it loads default middleware stack (it&amp;#8217;s amazing how much things can be handled by rack middlewares, allowing to keep Rails code simpler)&lt;/li&gt;
	&lt;li&gt;it loads plugins&lt;/li&gt;
	&lt;li&gt;it sets bunch of other things: database, logging, sessions, environment config, cache&lt;/li&gt;
	&lt;li&gt;it loads activesupport (interesting thing, you can set config.active_support.bare to not load &amp;#8216;active_support/all&amp;#8217;)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;It&amp;#8217;s also interesting how does Rails use OO model to make things simpler. After generating Rails 3 apps you can notice that frameworks are loaded by just requiring their railties:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;action_mailer/railtie&amp;quot;&lt;/span&gt;
&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;active_resource/railtie&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The same goes for engines. Ok&amp;#8230; but how will Rails know all the railties and engines loaded? As railties and engines are subclasses of Rails::Railtie and Rails::Engine, it&amp;#8217;s as easy as getting their subclasses: ::Rails::Railtie.subclasses or  ::Rails::Engine.subclasses. Quick note on &lt;code&gt;subclasses&lt;/code&gt;: it&amp;#8217;s not ruby method, subclasses are gathered using &lt;code&gt;inherited&lt;/code&gt; method (&lt;a href="http://github.com/rails/rails/blob/v3.0.0.beta4/railties/lib/rails/railtie.rb#L171"&gt;code&lt;/a&gt;).&lt;/p&gt;
&lt;p&gt;Now let&amp;#8217;s look how is that all used to boot rails application. As you can see in Rails 3 application, config.ru file looks like:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;File&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;expand_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;../config/environment&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="bp"&gt;__FILE__&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="no"&gt;MyApplication&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Application&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;As you probably know, object passed to &amp;#8220;run&amp;#8221; method must respond to :call. Application class does not have &amp;#8220;call&amp;#8221; method defined, though. How does it work? When you send &amp;#8220;call&amp;#8221; message to MyApplication::Application, method_missing will be fired and message will be send to application instance. In fact all of the missing methods on MyApplication::Application are delegated to instance:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;method_missing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;instance&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;What is instance? As Application is singleton, call to instance method returns instantinated application or instantinates it and then returns the instance. Application instance is built on initialization, such behavior is triggered in &lt;a href="http://github.com/rails/rails/blob/master/railties/lib/rails/application/finisher.rb"&gt;Finisher&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;The next question is: how does request&amp;#8217;s path look like? call() method runs call() on @app object, which is basically the middleware stack. Then, request goes through all the middlewares up to ActionDispatch::Routing::RouteSet, which is responsible for routing request to one of the controllers or other rack apps.&lt;/p&gt;
&lt;p&gt;After reading this blog post you should have a pretty good overview of core classes of Rails. I strongly encourage you to dive into Rails code yourself, it&amp;#8217;s a good way to learn new things about design, object oriented programming in ruby and to pick some cool patterns.&lt;/p&gt;
&lt;p&gt;If anyone is interested, I plan to continue Rails internals posts with action pack and routing system. Feedback highly appreciated! Please let me know if you find this kind of information useful or if you think that I missed something or should give more details on any of the parts.&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>New address</title>
   <link href="http://piotrsarnacki.com/2010/05/15/new-address" />
   <published>2010-05-15T00:00:00-07:00</published>
   <updated>2010-05-15T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2010/05/15/new-address</id>
   <content type="html">&lt;p&gt;I haven&amp;#8217;t written anything here for a while now. It was partially caused by a little mess in my blogs. I had 2 blogs with addresses blog.drogomir.com (for Polish blog) and drogomir.com/blog (for English blog)&amp;#8230; I didn&amp;#8217;t quite like addresses (hey? which one is in which language?) and keeping them on separate engines (typo and mephisto). Each time I wanted to start new post, I felt that I should change that right away. The problem is, I never had that much will to actually do something and after few minutes of checking solutions I usually stopped without any effects.&lt;/p&gt;
&lt;p&gt;Finally I&amp;#8217;ve merged my blogs and moved it to current address: &lt;a href="http://piotrsarnacki.com"&gt;piotrsarnacki.com&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;The blog is &lt;a href="http://github.com/drogus/drogus.github.com"&gt;hosted on github&lt;/a&gt; with help of &lt;a href="http://github.com/mojombo/jekyll"&gt;jekyll&lt;/a&gt;. Jekyll is great so far. Its simplicity is impressive and I don&amp;#8217;t have to worry about any server related issues. Layout is borrowed from &lt;a href="http://tom.preston-werner.com"&gt;Tom Preston-Werner&amp;#8217;s site&lt;/a&gt; (layout is &lt;span class="caps"&gt;MIT&lt;/span&gt; licensed). It helped me start within a few minutes, but I will try to change it in a few days.&lt;/p&gt;
&lt;p&gt;Although old &lt;span class="caps"&gt;RSS&lt;/span&gt; feed still works, I recommend to use new one: &lt;a href="http://feeds.feedburner.com/piotrsarnacki"&gt;feeds.feedburner.com/piotrsarnacki&lt;/a&gt;. All the feeds available for this blog are:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://feeds.feedburner.com/piotrsarnacki"&gt;feeds.feedburner.com/piotrsarnacki&lt;/a&gt; &amp;#8211; English content&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://feeds.feedburner.com/piotrsarnacki-pl"&gt;feeds.feedburner.com/piotrsarnacki-pl&lt;/a&gt; &amp;#8211; Polish content (available on &lt;a href="http://piotrsarnacki.com/pl"&gt;piotrsarnacki.com/pl&lt;/a&gt;)&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://feeds.feedburner.com/piotrsarnacki-all"&gt;feeds.feedburner.com/piotrsarnacki-all&lt;/a&gt; &amp;#8211; All posts&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I deleted some of the posts and marked some &lt;a href="http://piotrsarnacki.com/2009/06/16/cucumber-and-celerity-testing-unobtrusive-javascript/"&gt;as deprecated&lt;/a&gt;. I try to not abandon that blog this time and post some new stuff.&lt;/p&gt;
&lt;p&gt;On the side note. I have managed to be one of 20 students working on &lt;a href="http://rubysoc.org"&gt;Ruby Summer of Code&lt;/a&gt; projects. I will work in the team with &lt;a href="http://github.com/hurrycane"&gt;Bogdan Gaza&lt;/a&gt; on Rails 3 mountable apps and Rails admin panel (I will focus more on mountable apps part and Bogdan will work on rails admin panel). &lt;a href="http://github.com/carllerche"&gt;Carl Lerche&lt;/a&gt; and &lt;a href="http://github.com/sferik"&gt;Erik Michaels-Ober&lt;/a&gt; will be our primary mentors.&lt;/p&gt;
&lt;p&gt;I will try to post progress updates on this blog. Stay tuned!&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Cucumber and Celerity - testing unobtrusive javascript</title>
   <link href="http://piotrsarnacki.com/2009/06/16/cucumber-and-celerity-testing-unobtrusive-javascript" />
   <published>2009-06-16T00:00:00-07:00</published>
   <updated>2009-06-16T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2009/06/16/cucumber-and-celerity-testing-unobtrusive-javascript</id>
   <content type="html">&lt;p&gt;In the world of javascript and ajax web apps I often find that writing applications supporting both javascript enabled and disabled is hard. For the sake of simplicity and due to deadlines I often write only javascript version of some of the features. But there are many situations where both scenarios should be supported.&lt;/p&gt;
&lt;p&gt;The most important parts of app, especially the ones that must be crawled by google, should be written with unobtrusive style. And here comes the problem&amp;#8230; I rarely see javascript testing and till today I haven&amp;#8217;t done it myself. Celerity and cucumber (with help of culerity) can solve this problem. Here is short guide introducing this technique.&lt;/p&gt;
&lt;p&gt;I will use &lt;a href="http://cukes.info/"&gt;Cucumber&lt;/a&gt;, &lt;a href="http://celerity.rubyforge.org/"&gt;celerity&lt;/a&gt; (which is &lt;a href="http://htmlunit.sourceforge.net/"&gt;HtmlUnit&lt;/a&gt; wrapper with &lt;span class="caps"&gt;API&lt;/span&gt; compatible with watir) and &lt;a href="http://github.com/langalex/culerity/tree/master"&gt;culerity&lt;/a&gt;. Culerity is a proxy between celerity and your app. Celerity requires JRuby and probably your app need &lt;span class="caps"&gt;MRI&lt;/span&gt; or &lt;span class="caps"&gt;REE&lt;/span&gt; &amp;#8211; culerity resolves this problem.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s get started.&lt;/p&gt;
&lt;p&gt;You need to &lt;a href="http://kenai.com/projects/jruby/pages/GettingStarted#Installing_a_JRuby_Binary_Fille"&gt;install JRuby&lt;/a&gt; in order to run celerity. After installing and adding jruby to your &lt;span class="caps"&gt;PATH&lt;/span&gt; install celerity gem (probably as a root):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;jruby&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;S&lt;/span&gt; &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;celerity&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now you can create rails app and configure the environment:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="n"&gt;culerity&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt;
&lt;span class="n"&gt;sudo&lt;/span&gt; &lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;cucumber&lt;/span&gt; &lt;span class="n"&gt;rspec&lt;/span&gt; &lt;span class="n"&gt;rspec&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;rails&lt;/span&gt; &lt;span class="n"&gt;haml&lt;/span&gt;
&lt;span class="c1"&gt;# add config.gem &amp;quot;haml&amp;quot; to environment.rb&lt;/span&gt;
&lt;span class="n"&gt;gem&lt;/span&gt; &lt;span class="n"&gt;install&lt;/span&gt; &lt;span class="n"&gt;langalex&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;culerity&lt;/span&gt; &lt;span class="o"&gt;--&lt;/span&gt;&lt;span class="n"&gt;source&lt;/span&gt; &lt;span class="n"&gt;http&lt;/span&gt;&lt;span class="ss"&gt;:/&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;gems&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;github&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;com&lt;/span&gt;
&lt;span class="n"&gt;cd&lt;/span&gt; &lt;span class="n"&gt;culerity&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;example&lt;/span&gt;
&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;/script&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;generate&lt;/span&gt; &lt;span class="n"&gt;cucumber&lt;/span&gt;
&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;/script&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;generate&lt;/span&gt; &lt;span class="n"&gt;rspec&lt;/span&gt;
&lt;span class="c1"&gt;# now edit database.yml and set database options&lt;/span&gt;
&lt;span class="n"&gt;rake&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="ss"&gt;:migrate&lt;/span&gt;
&lt;span class="n"&gt;rake&lt;/span&gt; &lt;span class="n"&gt;features&lt;/span&gt;
&lt;span class="n"&gt;rm&lt;/span&gt; &lt;span class="n"&gt;features&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;step_definitions&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;webrat_steps&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rb&lt;/span&gt; &lt;span class="c1"&gt;# cause we will be using celerity&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;At this point you should have cucumber configured and you should be able to run &amp;#8220;rake features&amp;#8221; with output similar to:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;scenarios&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="n"&gt;steps&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="n"&gt;m0&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="mo"&gt;000&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Let&amp;#8217;s add some tests! You will need step definitions and hooks. Culerity provides some basic step definitions and hooks which you can generate with &amp;#8220;./script/generate culerity&amp;#8221; but I&amp;#8217;ve changed them a bit for my needs, so you can find them on &lt;a href="http://github.com/drogus/culerity-javascript-example"&gt;this example repository&lt;/a&gt;.&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://github.com/drogus/culerity-javascript-example/blob/master/features/step_definitions/common_celerity.rb"&gt;features/step_definitions/common_celerity.rb&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/drogus/culerity-javascript-example/blob/master/features/support/hooks.rb"&gt;features/support/hooks.rb&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Copy those files to your app.&lt;/p&gt;
&lt;p&gt;The first file is just rewrite of webrat steps and the second file adds hooks for firing celerity server and browser. Let me explain the hooks.rb file:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;culerity&amp;#39;&lt;/span&gt;
&lt;span class="sb"&gt;`mongrel_rails start -e cucumber -p 3001 -d`&lt;/span&gt;

&lt;span class="no"&gt;Before&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="vg"&gt;$server&lt;/span&gt; &lt;span class="o"&gt;||=&lt;/span&gt; &lt;span class="no"&gt;Culerity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;run_server&lt;/span&gt;
  &lt;span class="vg"&gt;$browser&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Culerity&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;RemoteBrowserProxy&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;new&lt;/span&gt; &lt;span class="vg"&gt;$server&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="ss"&gt;:browser&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="ss"&gt;:firefox&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="vg"&gt;$browser&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;webclient&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setJavaScriptEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
  &lt;span class="vi"&gt;@host&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;http://localhost:3001&amp;#39;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="no"&gt;Before&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;@js&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;scenario&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
  &lt;span class="vg"&gt;$browser&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;webclient&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;setJavaScriptEnabled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="nb"&gt;at_exit&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="vg"&gt;$browser&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exit&lt;/span&gt;
  &lt;span class="vg"&gt;$server&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;
  &lt;span class="sb"&gt;`mongrel_rails stop`&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&amp;#8216;Before&amp;#8217; hooks are run before each scenerio. In first &amp;#8216;before&amp;#8217; hook server and browser are set up and host is set to &amp;#8220;http://localhost:3001&amp;#8221; (change it if you want to run app on other address or port). Notice the line: $browser.webclient.setJavaScriptEnabled(false) &amp;#8211; it disables javascript by default.&lt;/p&gt;
&lt;p&gt;Second Before hook is fired only for scenarios tagged with @js tag. It will be useful for explicitly saying which scenarios should be tested with  javascript.&lt;/p&gt;
&lt;p&gt;&lt;ins&gt;I&amp;#8217;ve also added lines to start mongrel before the tests and stop it at exit. It&amp;#8217;s handy if you don&amp;#8217;t want to run and restart mongrel manually&lt;/ins&gt;&lt;/p&gt;
&lt;p&gt;Now it is time to write some scenarios. File &lt;a href="http://github.com/drogus/culerity-javascript-example/blob/master/features/javascript.feature"&gt;features/javascript.feature&lt;/a&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="no"&gt;Feature&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Javascript&lt;/span&gt;
  &lt;span class="no"&gt;In&lt;/span&gt; &lt;span class="n"&gt;order&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="n"&gt;javascript&lt;/span&gt;
  &lt;span class="no"&gt;As&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;developer&lt;/span&gt;
  &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;need&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="n"&gt;way&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="n"&gt;scenarios&lt;/span&gt; &lt;span class="n"&gt;with&lt;/span&gt; &lt;span class="n"&gt;javascript&lt;/span&gt; &lt;span class="n"&gt;enabled&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="n"&gt;disabled&lt;/span&gt;

  &lt;span class="vi"&gt;@js&lt;/span&gt;
  &lt;span class="no"&gt;Scenario&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;With&lt;/span&gt; &lt;span class="n"&gt;javascript&lt;/span&gt;
    &lt;span class="no"&gt;Given&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;homepage&lt;/span&gt;
    &lt;span class="no"&gt;And&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;follow&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Click me!&amp;quot;&lt;/span&gt;
    &lt;span class="no"&gt;Then&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;see&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Javascript rocks!&amp;quot;&lt;/span&gt;

  &lt;span class="no"&gt;Scenario&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;Without&lt;/span&gt; &lt;span class="n"&gt;javascript&lt;/span&gt;
    &lt;span class="no"&gt;Given&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="n"&gt;on&lt;/span&gt; &lt;span class="n"&gt;the&lt;/span&gt; &lt;span class="n"&gt;homepage&lt;/span&gt;
    &lt;span class="no"&gt;And&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;follow&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Click me!&amp;quot;&lt;/span&gt;
    &lt;span class="no"&gt;Then&lt;/span&gt; &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;should&lt;/span&gt; &lt;span class="n"&gt;see&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;I am also working without javascript!&amp;quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Both scenerios rely on &amp;#8220;Click me!&amp;#8221; link but have different expectations. &lt;del&gt;To run those tests start mongrel (or any other web server):&lt;/del&gt;&lt;br /&gt;
&lt;del&gt;&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;mongrel_rails&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="n"&gt;cucumber&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nb"&gt;p&lt;/span&gt; &lt;span class="mi"&gt;3001&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;&lt;/del&gt;&lt;br /&gt;
&lt;del&gt;This will fire mongrel in background on port 3001.&lt;/del&gt;&lt;br /&gt;
&lt;ins&gt;It is not necessary as mongrel control commands are in hooks.rb file&lt;/ins&gt;&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s best to operate on 2 console tabs while using celerity. One of the big drawbacks of using it, is lack of displaying rails exceptions. Because of that I run cucumber on one tab and &amp;#8220;tail -f log/cucumber.log&amp;#8221; on the other &amp;#8211; I can see errors in the log without opening the browser.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s run tests: &lt;pre&gt;rake features&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Of course both tests should fail with &lt;pre&gt;Unable to locate Link, using :text and /Click me!/&lt;/pre&gt; and rails error in log file: &lt;pre&gt;ActionController::RoutingError (No route matches &amp;#8220;/&amp;#8221; with {:method=&amp;gt;:get}):&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Now we can fix it. We need some controller to show the link: &lt;br /&gt;
&lt;pre&gt;script/generate rspec_controller home&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Add &lt;pre&gt;map.root :action =&amp;gt; &amp;#8220;show&amp;#8221;, :controller =&amp;gt; &amp;#8220;home&amp;#8221;&lt;/pre&gt; to routes file.&lt;br /&gt;
Next copy &lt;a href="http://github.com/drogus/culerity-javascript-example/raw/master/app/views/layouts/main.html.haml"&gt;app/views/layouts/main.html.haml&lt;/a&gt; (it just yields action and includes jquery.js and application.js) and &lt;a href="http://github.com/drogus/culerity-javascript-example/raw/master/public/javascripts/jquery.js"&gt;jquery.js&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;You need to also set layout for home controller: &lt;pre&gt;layout &amp;#8216;main&amp;#8217;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;And here comes html and javascript.&lt;br /&gt;
app/views/home/show.html.haml&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;link_to&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;Click me!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;?clicked=1&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:id&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s2"&gt;&amp;quot;click_me&amp;quot;&lt;/span&gt;

&lt;span class="c1"&gt;#text&lt;/span&gt;
  &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:clicked&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;I&lt;/span&gt; &lt;span class="n"&gt;am&lt;/span&gt; &lt;span class="n"&gt;also&lt;/span&gt; &lt;span class="n"&gt;working&lt;/span&gt; &lt;span class="n"&gt;without&lt;/span&gt; &lt;span class="n"&gt;javascript!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;It displays a link and a text if params[:clicked] is present. So after clicking on that link page will be reloaded with parameter clicked=1 and the text will be displayed.&lt;/p&gt;
&lt;p&gt;Let&amp;#8217;s check if it&amp;#8217;s passing:&lt;br /&gt;
&lt;pre&gt;mongrel_rails restart; rake features&lt;/pre&gt;&lt;br /&gt;
We need to restart mongrel before &amp;#8220;rake features&amp;#8221; in order to load changes because of &amp;#8220;cache_classes = true&amp;#8221;. It&amp;#8217;s one of the drawbacks of using this method, but I&amp;#8217;m sure that someone will find out better way to do that.&lt;/p&gt;
&lt;p&gt;If you did everything properly the second scenario should pass now. We&amp;#8217;re green! :D&lt;/p&gt;
&lt;p&gt;Add javascript code to your application in order to make the second scenario pass:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="ruby"&gt;&lt;span class="n"&gt;jQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;#click_me&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;click&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;#text&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;html&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;&amp;quot;Javascript rocks!&amp;quot;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kp"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now both scenarios should pass.&lt;/p&gt;
&lt;p&gt;To sum up, now you should be able to:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;configure rails app with cucumber and celerity&lt;/li&gt;
	&lt;li&gt;specify how your tests should be run by placing @js tag on top of javascript scenarios&lt;/li&gt;
	&lt;li&gt;test unobtrusive javascript with ease&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;TODOs:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;del&gt;figure out better way to reload rails app while testing&lt;/del&gt;&lt;/li&gt;
	&lt;li&gt;provide better error explanations in cucumber with celurity to test without tailing logs&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Cheers!&lt;/p&gt;
&lt;h3&gt;Additional resources&lt;/h3&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://github.com/alvinschur/celerity-examples"&gt;Celerity ajax examples by Alvin Schur&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;</content>
 </entry>
 
 <entry>
   <title>Fixing safe_erb with memcached improved</title>
   <link href="http://piotrsarnacki.com/2009/05/12/fixing-safe_erb-with-memcached-improved" />
   <published>2009-05-12T00:00:00-07:00</published>
   <updated>2009-05-12T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2009/05/12/fixing-safe_erb-with-memcached-improved</id>
   <content type="html">&lt;p&gt;Ruby has many structures that are weird and dangerous to some programmers that works with Java or other more &amp;#8220;traditional&amp;#8221; OO languages. For me it&amp;#8217;s great and metaprogramming capabilities makes ruby killer in case of DSLs, nice looking APIs and avoiding repeating yourself.&lt;/p&gt;
&lt;p&gt;But many problems that are solved with alias_method_chain or method_missing can be solved easier without the code smell. I sometimes find myself designing some weird complex solutions and then, after looking at it saying: &amp;#8220;Hey! Why not just use OO features!&amp;#8221;. I encourage everybody to look critically at every piece of your code that seems to be overkill and can be replaced with standard OO design.&lt;/p&gt;
&lt;p&gt;One of examples is &lt;a href="http://www.nfjsone.com/blog/david_bock/2009/03/fixing_safe_erb_with_memcached_fragments.html"&gt;fixing memcached and safe_erb duet with alias_method_chain&lt;/a&gt; I don&amp;#8217;t know if this solution worked in older versions of rails, but in 2.3 I got stack level too deep error. After some thinking I&amp;#8217;ve just created subclass of MemCacheStore:&lt;/p&gt;
&lt;script src="http://gist.github.com/110488.js"&gt;&lt;/script&gt;&lt;p&gt;And then you can simply set it as cache store: config.cache_store = :my_mem_cache_store&lt;/p&gt;
&lt;p&gt;And that&amp;#8217;s it, no alias method chain :)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>My git workflow</title>
   <link href="http://piotrsarnacki.com/2009/04/26/my-git-workflow" />
   <published>2009-04-26T00:00:00-07:00</published>
   <updated>2009-04-26T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2009/04/26/my-git-workflow</id>
   <content type="html">&lt;p&gt;I know that this post may be obvious for experienced git users, but it may be useful for some of you.&lt;/p&gt;
&lt;p&gt;Github has &lt;a href="http://github.com/blog/421-pick-your-default-branch"&gt;added default branch picking&lt;/a&gt; recently. It&amp;#8217;s great, as you don&amp;#8217;t have to use master branch as your main branch. I usually have three &amp;#8220;main&amp;#8221; branches in my repo &amp;#8211; production, staging and development.&lt;/p&gt;
&lt;p&gt;The main point of it is to have separate branch for each environment (sometimes I skip staging, but it&amp;#8217;s often wise to have some point between development and production). My main problem with git (or maybe rather with my lack of knowledge about git) in the very beginning of using git was to learn how to use branches efficiently. After some time with git I&amp;#8217;ve noticed that it&amp;#8217;s best to keep each fix or idea in separate branch.&lt;/p&gt;
&lt;p&gt;One crucial thing is to inherit from production when it&amp;#8217;s possible. Let&amp;#8217;s say you have those main branches I mentioned before. There is a bug #345 in bug tracker and you want to fix it and merge into production after showing results to the client. The best way is to begin with production:&lt;/p&gt;
&lt;pre&gt;&lt;code class="console"&gt;
git checkout -b bug345 production
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It will create new branch bug345 with production as a parent branch. Now:&lt;/p&gt;
&lt;pre&gt;&lt;code class="console"&gt;
# fix the bug
git commit -m "Fixed the nasty bug #345"
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sometimes client doesn&amp;#8217;t need reviewing the changes but when he does you should have ability to show it and merge it to the production branch later.&lt;/p&gt;
&lt;pre&gt;&lt;code class="console"&gt;
git checkout staging
git merge bug345
# deploy staging
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After the client approval of your solution you can merge the bugfix to the production branch:&lt;/p&gt;
&lt;pre&gt;&lt;code class="console"&gt;
git checkout production
git merge bug345
# deploy production
# you can remove bug345 branch now
git branch -d bug345
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The most important thing in this process is to always inherit from production branch, unless you need changes from other branches. Otherwise you could merge some needless junk from other branches or have to cherry-pick individual commits. With above solution everything is clean and you are sure that that you will merge only what you need.&lt;/p&gt;
&lt;p&gt;Hope it will help :)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>JSON-P support for Apache upload progress</title>
   <link href="http://piotrsarnacki.com/2009/02/10/json-p-support-for-apache-upload-progress" />
   <published>2009-02-10T00:00:00-08:00</published>
   <updated>2009-02-10T00:00:00-08:00</updated>
   <id>http://piotrsarnacki.com/2009/02/10/json-p-support-for-apache-upload-progress</id>
   <content type="html">&lt;p&gt;Just a quick note about new feature added to apache upload progress and jquery upload progress libs.&lt;/p&gt;
&lt;p&gt;&lt;a href="http://deadprogrammersociety.blogspot.com/"&gt;Ron Evans aka deadprogrammer&lt;/a&gt; has added support for &lt;span class="caps"&gt;JSON&lt;/span&gt;-P in &lt;a href="http://github.com/drogus/apache-upload-progress-module/commit/20fed47119d3ea70b91ccb6a91a9338c8f127304"&gt;this commit&lt;/a&gt;. What does it mean? Cross domain requests are now possible, so if you need such a functionality pull the newest changes.&lt;/p&gt;
&lt;p&gt;Here is Ron&amp;#8217;s description from &lt;span class="caps"&gt;README&lt;/span&gt;:&lt;br /&gt;
&lt;blockquote&gt;&lt;br /&gt;
- &lt;span class="caps"&gt;JSON&lt;/span&gt;-P Support&lt;/p&gt;
&lt;p&gt;You can also request progress updates by using &lt;span class="caps"&gt;JSON&lt;/span&gt;-P, if you are uploading the file from a different domain or  subdomain than the web server that is handling your original request. Adding a &amp;#8220;callback=yourCallbackFunction&amp;#8221; parameter  to your request to the progress server will activate this functionality.&lt;/p&gt;
&lt;p&gt;For example, a request like:&lt;br /&gt;
  http://uploads.yourdomain.com/progress?callback=jsonp123&amp;amp;X-Progress-ID=1234&lt;/p&gt;
&lt;p&gt;Would return the &lt;span class="caps"&gt;JSON&lt;/span&gt;-P function: &lt;br /&gt;
  jsonp123(new Object({ &amp;#8216;state&amp;#8217; : &amp;#8216;uploading&amp;#8217;, &amp;#8216;received&amp;#8217; : 35587, &amp;#8216;size&amp;#8217; : 716595, &amp;#8216;speed&amp;#8217; : 35587 }));&lt;/p&gt;
&lt;p&gt;The normal &lt;span class="caps"&gt;JSON&lt;/span&gt; request:&lt;br /&gt;
  http://www.yourdomain.com/progress?X-Progress-ID=1234&lt;/p&gt;
&lt;p&gt;Would return the &lt;span class="caps"&gt;JSON&lt;/span&gt; data: &lt;br /&gt;
  new Object({ &amp;#8216;state&amp;#8217; : &amp;#8216;uploading&amp;#8217;, &amp;#8216;received&amp;#8217; : 35587, &amp;#8216;size&amp;#8217; : 716595, &amp;#8216;speed&amp;#8217; : 35587 })&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Remember to update &lt;a href="http://github.com/drogus/jquery-upload-progress/tree/master"&gt;jquery upload progress&lt;/a&gt; also, to use jsonp.&lt;/p&gt;
&lt;p&gt;Enjoy :)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Tweaking Rails app with jQuery, part I</title>
   <link href="http://piotrsarnacki.com/2008/07/03/tweaking-rails-app-with-jquery-part-i" />
   <published>2008-07-03T00:00:00-07:00</published>
   <updated>2008-07-03T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2008/07/03/tweaking-rails-app-with-jquery-part-i</id>
   <content type="html">&lt;div class="quick-links"&gt;
&lt;p&gt;Quick links:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://mugshots.drogomir.com/js/step2/mugshots"&gt;completed demo&lt;/a&gt; (demo is reseted every few hours)&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/drogus/mugshots/tree/master"&gt;sources of application on github&lt;/a&gt; (&lt;a href="http://github.com/drogus/mugshots/tarball/master"&gt;or download tarball&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;p&gt;I&amp;#8217;m in the train from &lt;a href="http://maps.google.com/maps?f=d&amp;amp;hl=en&amp;amp;geocode=&amp;amp;saddr=Zgorzelec,+Poland&amp;amp;daddr=warsaw,+poland&amp;amp;sll=37.0625,-95.677068&amp;amp;sspn=58.470251,108.984375&amp;amp;ie=UTF8&amp;amp;ll=51.604372,17.578125&amp;amp;spn=5.815072,13.623047&amp;amp;z=6"&gt;Zgorzelec to Warsaw&lt;/a&gt; returning from my girlfriend&amp;#8217;s place. Polish trains are like turtles, so I will have pretty much time for writing ;-)&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve wrote (or maybe it&amp;#8217;s better to say copy&amp;amp;paste) little rails app like in &lt;a href="http://clarkware.com/cgi/blosxom/2007/02/24"&gt;Mike Clark&amp;#8217;s tutorial for attachment_fu&lt;/a&gt;. A few months ago there was &lt;a href="http://www.yoursgallery.pl/exhibitions.php?action=details&amp;amp;exh_id=44&amp;amp;setlang=en"&gt;Mugshots exhibition in Yours Gallery&lt;/a&gt; in Warsaw based on &lt;a href="http://www.amazon.com/City-Shadows-Sydney-Photographs-1912-1948/dp/1876991208/ref=pd_bbs_sr_2?ie=UTF8&amp;amp;s=books&amp;amp;qid=1215106900&amp;amp;sr=8-2"&gt;work of Peter Doyle&lt;/a&gt;. I saw it with Kathleene, she took some pictures. Great! I have material to fill my new app, what else could I possibly dream of?! (yeah&amp;#8230; macbook, but it&amp;#8217;s obvious ;-).&lt;/p&gt;
&lt;p&gt;Now you can admire my hard work: &lt;a href="http://mugshots.drogomir.com/js/no-javascript/mugshots/"&gt;mugshots.drogomir.com/js/no-javascript/mugshots/&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;But wait&amp;#8230; It&amp;#8217;s not so cool&amp;#8230; where are all those shiny javascript effects? Don&amp;#8217;t worry. I will show you how to spice this dish.&lt;/p&gt;
&lt;p&gt;We will need:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://www.malsup.com/jquery/form/"&gt;jQuery form plugin&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://brandonaaron.net/docs/livequery/"&gt;jQuery livequery plugin&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/drogus/jquery-upload-progress/"&gt;jQuery upload progress&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://malsup.com/jquery/block/" title="used by multifile"&gt;jquery blockUI&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://www.fyneworks.com/jquery/multiple-file-upload/"&gt;jquery mutli file&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://github.com/drogus/lightbox-fu/tree/master"&gt;jquery lightBoxFu&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://leandrovieira.com/projects/jquery/lightbox/"&gt;lightbox&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I&amp;#8217;ve pushed application to github, so you can see entire code. Clone it or &lt;a href="http://cdsc"&gt;grab the tarball&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;There is one thing that is not straight forward. @main_js variable in app/views/layouts/main.rhtml:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
&amp;lt;%= javascript_include_tag @main_js %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&amp;#8217;s there for changing javascript file loaded. When url is app.com/js/some_javascript_file/mugshots, @main_js should be &amp;#8220;some_javascript_file.js&amp;#8221;. I&amp;#8217;ve done this to have possibility to show you app with different javascript files without changing the code. See routes and mugshots_controller.rb to find out how it was done (or run &amp;#8220;rake routes&amp;#8221; in app dir to see routes).&lt;/p&gt;
&lt;p&gt;Lets begin.&lt;/p&gt;
&lt;p&gt;What to do first? It&amp;#8217;s all about uploading files, so I would add upload progress bar to form in &lt;a href="http://mugshots.drogomir.com/mugshots/new"&gt;mugshots.drogomir.com/mugshots/new&lt;/a&gt;. To implement it you will need some kind of server module:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;&lt;a href="http://drogomir.com/blog/2008/6/18/upload-progress-bar-with-mod_passenger-and-apache"&gt;Apache upload progress module&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://wiki.codemongers.com/NginxHttpUploadProgressModule"&gt;Nginx upload progress module&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://upload.lighttpd.net/"&gt;Lighttpd upload progress&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You have to install and enable one of the above modules to make progress bar work.&lt;/p&gt;
&lt;p&gt;Then add some javascript to applications.js. This example is using &amp;#8220;LightBoxFu&amp;#8221;: &amp;#8211; little script that I wrote to show progress bar as an overlay. It&amp;#8217;s based on &lt;a href="http://riddle.pl/"&gt;Riddle&amp;#8217;s work&lt;/a&gt; &amp;#8211; all positioning is in &lt;span class="caps"&gt;CSS&lt;/span&gt; (except expressions for IE) so it&amp;#8217;s really light and fast. Ideal for such a task. If you don&amp;#8217;t like lightBoxFu you can use any other form of displaying message (you can use some other lightbox with displaying code function or even blockUI plugin).&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;
// handy trick, if we can't use $ beaceuse jQuery.noConflict
// was used, jQuery is passed as argument in document ready
// so we can name it $
jQuery(function($) {
  // add upload progress to our form
  $('form.progress').uploadProgress({
    start:function(){
      // after starting upload open lightBoxFu with our bar as html
      $.lightBoxFu.open({
        html: '&amp;lt;div id="uploading"&amp;gt;&amp;lt;span id="received"&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;span id="size"&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;br/&amp;gt;&amp;lt;div id="progress" class="bar"&amp;gt;&amp;lt;div id="progressbar"&amp;gt;&amp;amp;nbsp;&amp;lt;/div&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;span id="percent"&amp;gt;&amp;lt;/span&amp;gt;&amp;lt;/div&amp;gt;',
        width: "250px",
        closeOnClick: false
      });
      jQuery('#received').html("Upload starting.");
      jQuery('#percent').html("0%");
    },
    uploading: function(upload) {
      // update upload info on each /progress response
      jQuery('#received').html("Uploading: "+parseInt(upload.received/1024)+"/");
      jQuery('#size').html(parseInt(upload.size/1024)+" kB");
      jQuery('#percent').html(upload.percents+"%");
    },
    interval: 2000,
    /* if we are using images it's good to preload them, safari has problems with
       downloading anything after hitting submit button. these are images for lightBoxFu
       and progress bar */
    preloadImages: ["/images/overlay.png", "/images/ajax-loader.gif"]
  });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And some styling for progress bar:&lt;br /&gt;
&lt;pre&gt;&lt;code class="css"&gt;
  #progress {
  margin: 8px;
  width: 220px;
  height: 19px;
}&lt;/p&gt;
&lt;p&gt;#progressbar {&lt;br /&gt;
  background: url(&amp;#8216;/images/ajax-loader.gif&amp;#8217;) no-repeat;&lt;br /&gt;
  width: 0px;&lt;br /&gt;
  height: 19px;&lt;br /&gt;
}&lt;br /&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;That&amp;#8217;s it, just add &amp;#8220;progress&amp;#8221; class to your form and progress bar is working:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
&amp;lt;% form_for(:mugshot, :url =&amp;gt; mugshots_path, 
                      :html =&amp;gt; { :multipart =&amp;gt; true, :class =&amp;gt; "progress" }) do |f| -%&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Uploading files looks much better right now, check it here: &lt;a href="http://mugshots/drogomir.com/js/progress/mugshots/new"&gt;http://mugshots.drogomir.com/js/progress/mugshots/new&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So what now? I find the &amp;#8220;add photo, click New mugshot, add photo&amp;#8221; scenerio annoying. We could add more than one file on each submit. For that we will use jquery.MultiFile.js. This one is a bit tricky, cause we will need to tweak code handling uploads also.&lt;/p&gt;
&lt;p&gt;Javascript enabling mutlifile:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;
jQuery(function($) {
  $('.multi-file').each(function() {
    // change name of element before applying MultiFile
    // so array of files can be send to server with mugshot[uploaded_data][]
    $(this).attr('name', $(this).attr('name') + '[]');
  }).MultiFile();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;We must also add &amp;#8220;multi-file&amp;#8221; class to file field:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
&amp;lt;%= f.file_field :uploaded_data, :class =&amp;gt; 'multi-file' %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;From javascript point of view that&amp;#8217;s all. Let&amp;#8217;s see how uploaded photos are handled by rails app:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
@mugshot = Mugshot.new(params[:mugshot])
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So mugshot.uploaded_data is filled with data from &lt;code&gt;params[:mugshot][:uploaded_data]&lt;/code&gt;. Good for one file. But with array of files we should create Mugshot for each file. I would add a method in model:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
  def self.handle_upload(mugshot_params)
    # array for not saved mugshots
    mugshots = []
    if mugshot_params[:uploaded_data].kind_of?(Array)
      mugshot_params[:uploaded_data].each do |p| 
        unless p.blank?
          mugshot = Mugshot.new(:uploaded_data =&amp;gt; p)
          mugshots &amp;lt;&amp;lt; mugshot unless mugshot.save
        end
      end
    else
      mugshot = Mugshot.new(mugshot_params)
      mugshots &amp;lt;&amp;lt; mugshot unless mugshot.save
    end
    mugshots
  end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;and slightly change controller code:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
  def create
    @mugshots = Mugshot.handle_upload(params[:mugshot])
      
    # if @mugshots is empty there are no errors
    if @mugshots.blank?
      flash[:notice] = 'Mugshot was successfully created.'
      redirect_to mugshots_url
    else
      render :action =&amp;gt; :new
    end
  end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Only one problem left. Validation.&lt;/p&gt;
&lt;p&gt;Easiest way is to change error_messages_for:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
&amp;lt;%= error_messages_for :object =&amp;gt; @mugshots %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It works. But suppose you are uploading 3 files and 2 of them are too big. You will end with:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Size is not included in the list&lt;/li&gt;
	&lt;li&gt;Size is not included in the list&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Which one was added? Some lottery here&amp;#8230;&lt;/p&gt;
&lt;p&gt;I would tweak attachment_fu error messages a bit. By default it uses validates_as_attachment method which simply adds:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
  validates_presence_of :size, :content_type, :filename
  validate  :attachment_attributes_valid?
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead validates_as_attachment we can isert our new code:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
  validates_presence_of :size, :content_type, :filename, :message =&amp;gt; Proc.new { |mugshot| "can't be blank (#{mugshot.filename})" }
  validate  :attachment_attributes_valid?
  
  def attachment_attributes_valid?
    [:size, :content_type].each do |attr_name|
      enum = attachment_options[attr_name]
      errors.add attr_name, "#{ActiveRecord::Errors.default_error_messages[:inclusion]} (#{self.filename})" unless enum.nil? || enum.include?(send(attr_name))
    end
  end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now it&amp;#8217;s a lot more readable:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Size is not included in the list (filename.jpg)&lt;/li&gt;
	&lt;li&gt;Size is not included in the list (filename1.jpg)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Submit form looks better now, but viewing files is still ugly. Maybe we could add some lightbox? No problem:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;
$('#mugshots li a').lightBox(); 
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I used &lt;a href="http://leandrovieira.com/projects/jquery/lightbox/"&gt;that lightbox&lt;/a&gt; cause I had it configured for my previous rails apps, but pick your favourite one, as there are gazilions of them.&lt;/p&gt;
&lt;p&gt;This is first step of tweaking our app. Javascript is in step1.js file: &lt;a href="http://mugshots.drogomir.com/js/step1/mugshots/new"&gt;mugshots.drogomir.com/js/step1/mugshots/new&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;What now? User can upload many files at one submit and see progress bar. What else do we need? Ajax! My preciousssss&amp;#8230;&lt;/p&gt;
&lt;p&gt;As all children know, XMLHttpRequest can&amp;#8217;t upload files. What a shame&amp;#8230; our new tweaked mugshots app is all about uploading files. Although you can&amp;#8217;t do it with &lt;span class="caps"&gt;XHR&lt;/span&gt;, there is a way to imitate it. It is obtained by creating an iframe and uploading files to it.&lt;/p&gt;
&lt;p&gt;Luckily Mike Malsup has done hard work for us writing &lt;a href="http://www.malsup.com/jquery/form/"&gt;jQuery form plugin&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;First, we need our form. I would place it instead &amp;#8220;New mugshot&amp;#8221; link. Link has id=&amp;#8220;new_mugshot_link&amp;#8221;, so this piece of code will replace it with form:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;
  /* create upload form with multifile instead of new mugshot link */
  var form = $('&amp;lt;form method="post" enctype="multipart/form-data" class="progress ajax" action="/mugshots"&amp;gt;');
  var label = $('&amp;lt;p&amp;gt;&amp;lt;label for="mugshot_uploaded_data"&amp;gt;Upload mugshot: &amp;lt;/label&amp;gt;&amp;lt;/p&amp;gt;');
  var input = $('&amp;lt;input type="file" class="multi-file" id="mugshot_uploaded_data" size="30" name="mugshot[uploaded_data]"/&amp;gt;');
  label.append(input).appendTo(form);
  form.append('&amp;lt;p&amp;gt;&amp;lt;input type="submit" value="Create" name="commit"/&amp;gt;&amp;lt;/p&amp;gt;');
  if (typeof(AUTH_TOKEN) != "undefined") form.append('&amp;lt;input type="hidden" value="'+AUTH_TOKEN+'" name="authenticity_token"/&amp;gt;'); 
  $('#new_mugshot_link').replaceWith(form);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Our form has to be send to an iframe, so we have to apply ajaxForm to it. After replacing link with form we can&amp;#8217;t figure out when form is actually appended to &lt;span class="caps"&gt;DOM&lt;/span&gt;. To be sure that form is there, we can use livequery. It will fire callback function when &amp;#8216;form.ajax&amp;#8217; will be available:&lt;/p&gt;
&lt;pre&gt;&lt;code class="javascript"&gt;
  $('form.ajax').livequery(function() {
    $(this).ajaxForm({iframe: true, success: function (responseText, statusText, form) {
      var url = $(form).attr('action');
      /* get new files */
      $.ajax({
          url: url,
          dataType: "script",
          beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");},
	  /* we need to update lightbox array to include new files */
          complete: function() { $('#mugshots li a').lightBox(); }
      });
    }});
  });
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When new form tag with class &amp;#8220;ajax&amp;#8221; will be available callback function will be run. iframe option tells form plugin to add hidden iframe (it will handle file upload).&lt;/p&gt;
&lt;p&gt;The above code has ajax call to &amp;#8220;/mugshots&amp;#8221; url which will run index.js.erb (&lt;span class="caps"&gt;RJS&lt;/span&gt;), so we will need one:&lt;/p&gt;
&lt;p&gt;app/views/mugshots/index.js.erb&lt;br /&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
  jQuery('#mugshots').html(&amp;lt;%= js render(:partial =&amp;gt; 'mugshot', :collection =&amp;gt; @mugshots) %&amp;gt;);
&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;to handle it we need to use respond_to:&lt;/p&gt;
&lt;pre&gt;&lt;code class="ruby"&gt;
  respond_to do |format|
    format.html
    # layout =&amp;gt; false is here beaceuse without it rails are looking
    # for layouts/index.js.erb
    format.js { render :layout =&amp;gt; false }
  end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Normally I try not to use &lt;span class="caps"&gt;RJS&lt;/span&gt; to keep all my javascript (and ajax) logic in javascript files, but in case of images it isn&amp;#8217;t so esay. I will write about it and about javascript templating systems in one of the next posts.&lt;/p&gt;
&lt;p&gt;Take a look at: &lt;a href="http://mugshots.drogomir.com/js/step2/mugshots"&gt;mugshots.drogomir.com/js/step2/mugshots&lt;/a&gt; Doesn&amp;#8217;t it look nice?&lt;/p&gt;
&lt;p&gt;There is only one problem :) No ajax validation. After submitting files, javascript can&amp;#8217;t get any info about errors or uploaded files beaceuse it is treated like normal html request and response is loaded in an iframe. How to fix it? I&amp;#8217;ll write about it in the next post. :)&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Upload progress script with safari support</title>
   <link href="http://piotrsarnacki.com/2008/06/30/upload-progress-script-with-safari-support" />
   <published>2008-06-30T00:00:00-07:00</published>
   <updated>2008-06-30T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2008/06/30/upload-progress-script-with-safari-support</id>
   <content type="html">&lt;p&gt;Quick links:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Source on github for jquery version: &lt;a href="http://github.com/drogus/jquery-upload-progress"&gt;http://github.com/drogus/jquery-upload-progress&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Source on github for prototype version: &lt;a href="http://github.com/drogus/prototype-upload-progress"&gt;http://github.com/drogus/prototype-upload-progress&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;jQuery demo: &lt;a href="http://drogomir.com/files/blog/jquery-upload-progress/example"&gt;http://drogomir.com/files/blog/jquery-upload-progress/example&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;Prototype demo: &lt;a href="http://drogomir.com/files/blog/prototype-upload-progress/example"&gt;http://drogomir.com/files/blog/prototype-upload-progress/example&lt;/a&gt;&lt;/li&gt;
	&lt;li&gt;&lt;a href="http://drogomir.com/blog/2008/6/18/upload-progress-bar-with-mod_passenger-and-apache"&gt;Installing apache upload progress&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Recently I&amp;#8217;ve wrote about &lt;a href="http://drogomir.com/blog/2008/6/18/upload-progress-bar-with-mod_passenger-and-apache"&gt;apache upload progress module&lt;/a&gt;. I work mainly on linux and I haven&amp;#8217;t check my scripts on safari. It was working even on IE, what possibly could be harder to obtain? ;-) Some people reported that demo is not working on safari and Michele &lt;a href="http://drogomir.com/blog/2008/6/18/upload-progress-bar-with-mod_passenger-and-apache#comment-13"&gt;resolved the problem&lt;/a&gt; (thanks Michele :).&lt;/p&gt;
&lt;p&gt;Solved! The only thing to do was to open WinXP on &lt;a href="http://www.virtualbox.org/"&gt;VirtualBox&lt;/a&gt; and check it on Safari 3. Michele&amp;#8217;s solution worked well, but to make it work there must be html page with given structure and javascript. I like &amp;#8220;one file&amp;#8221; easy to use scripts without any issues with static files :) So what? Create an iframe dynamically, load scripts dynamically, one file, the only thing that user will have to set is path to scripts.&lt;/p&gt;
&lt;p&gt;With Safari? No, not really. I&amp;#8217;ve wrote it in a few minuttes and checked in firefox. It worked, great, now safari. Nope&amp;#8230;.&lt;/p&gt;
&lt;p&gt;Although Safari have great &lt;span class="caps"&gt;CSS&lt;/span&gt; support, it is really terrible with Javascript. &lt;span class="caps"&gt;WYSIWYG&lt;/span&gt;, javascript history, ajax issues, now the upload progress and iframes. In edge case libraries I often see hacks for IE and safari mainly.&lt;/p&gt;
&lt;p&gt;Luckily Apple released Windows version of safari and I can check my scripts and pages on safari. Good&amp;#8230; developer tools don&amp;#8217;t work, though&amp;#8230; I had to do some alert-like IE style debugging ;-) After many hours of trying new more and more insane ways to create iframe and load scripts into it, code with document.write() worked!&lt;/p&gt;
&lt;p&gt;&lt;a href="http://github.com/drogus/jquery-upload-progress/commit/cc8ebce8acbb54febb3d24c7a77118c1501a922f"&gt;Check the commit on github&lt;/a&gt;. Lines 18-22 especially. And 28-line issue with safari not waiting to load previous script.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s really sad that after working on firefox with firebug or opera with developer tools I have to fight with Safari which is supposed to be modern browser. I know that on Mac developing scripts for safari is easier, thanks to working debug tools, but hey! I work on Linux. Why do I have to run Safari on Wine or VirtualBox, lacking debug tools? It&amp;#8217;s a pain. I feel like I&amp;#8217;m working on explorer&amp;#8230;&lt;/p&gt;
&lt;p&gt;&lt;del&gt;I will add prototype version&lt;/del&gt; and possibly some usage page shortly (for know look at the &lt;a href="http://drogomir.com/files/blog/jquery-upload-progress/example"&gt;demo code&lt;/a&gt;).&lt;/p&gt;</content>
 </entry>
 
 <entry>
   <title>Upload progress bar with mod_passenger and apache</title>
   <link href="http://piotrsarnacki.com/2008/06/18/upload-progress-bar-with-mod_passenger-and-apache" />
   <published>2008-06-18T00:00:00-07:00</published>
   <updated>2008-06-18T00:00:00-07:00</updated>
   <id>http://piotrsarnacki.com/2008/06/18/upload-progress-bar-with-mod_passenger-and-apache</id>
   <content type="html">&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;&lt;/strong&gt;: I found 2 bugs in upload progress module. If you have already installed. update to at least 0.1 version: &lt;a href="http://github.com/drogus/apache-upload-progress-module/commits/0.1"&gt;http://github.com/drogus/apache-upload-progress-module/commits/0.1&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I&amp;#8217;ve installed &lt;a href="http://www.modrails.com/"&gt;mod passenger&lt;/a&gt; on my server recently. It&amp;#8217;s really great software. Now I don&amp;#8217;t have to worry about monitoring, nginx proxy, load balancing, big file uploads&amp;#8230; and it&amp;#8217;s fast! With &lt;a href="http://www.rubyenterpriseedition.com/"&gt;Ruby Enterprise Edition&lt;/a&gt; it&amp;#8217;s even faster.&lt;/p&gt;
&lt;p&gt;Personally I don&amp;#8217;t care about people saying that phusion wants to &lt;a href="http://judofyr.net/posts/ruby-fishy-edition.html"&gt;promote themselves on &lt;span class="caps"&gt;REE&lt;/span&gt;&lt;/a&gt; as long as it gives faster ruby with lower memory use (but yes, I know, &lt;span class="caps"&gt;REE&lt;/span&gt; is not best choice for a name :).&lt;/p&gt;
&lt;p&gt;After installing I&amp;#8217;ve realised that my shiny upload progress bar (thanks to &lt;a href="http://wiki.codemongers.com/NginxHttpUploadProgressModule"&gt;Upload Progress Module for nginx&lt;/a&gt;) was not working. Oh gods! What a tragedy!&lt;/p&gt;
&lt;p&gt;But fear not. I&amp;#8217;ve written &lt;a href="http://github.com/drogus/apache-upload-progress-module/tree/master"&gt;apache upload progress module&lt;/a&gt; to have my lovely progress bar back again. As a lazy developer I&amp;#8217;ve implemented reports in the same way as in nginx upload progress, so my applications are working without changing any signle line of code. If you were using nginx upload progress just drop the module, change your config and you&amp;#8217;re good to go :)&lt;/p&gt;
&lt;p&gt;I&amp;#8217;m testing it in one of my production servers, but be carefull &amp;#8211; it&amp;#8217;s not well tested in other enviroments (i have gentoo with apache 2.2.8). Any feedback will be helpfull. Give me a note in comments if you encounter any problems.&lt;/p&gt;
&lt;p&gt;So you want to be cool and have your own sexy progress bar in your app? Keep reading ;)&lt;/p&gt;
&lt;p&gt;To install module you must download it using git:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="bash"&gt;git clone git://github.com/drogus/apache-upload-progress-module.git
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;or get the package: &lt;a href="http://github.com/drogus/apache-upload-progress-module/tarball/master"&gt;http://github.com/drogus/apache-upload-progress-module/tarball/master&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;To compile/install/activate you have to use apxs2:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="apache"&gt;&lt;span class="nb"&gt;apxs2&lt;/span&gt; -c -i -a mod_upload_progress.c
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;ul&gt;
	&lt;li&gt;-c is for compiling&lt;/li&gt;
	&lt;li&gt;-i is for installing (copy mod_upload_progress.so to apache library dir)&lt;/li&gt;
	&lt;li&gt;-a is for activating (add LoadModule option into your apache conf file)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;If you want to install and activate run this command as a root. Otherwise you can just compile and add LoadModule to apache conf:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="apache"&gt;&lt;span class="nb"&gt;LoadModule&lt;/span&gt; upload_progress_module path/to/apache-upload-progress-module/.libs/mod_upload_progress.so
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Currently there is only one global option:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="apache"&gt;&lt;span class="nb"&gt;UploadProgressSharedMemorySize&lt;/span&gt; &lt;span class="m"&gt;1024000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This sets shared memory size to  1M. By default it&amp;#8217;s 100kB.&lt;/p&gt;
&lt;p&gt;To add tracking and reporting upload for a virtual host in apache you will need to add:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;code class="apache"&gt;&lt;span class="nt"&gt;&amp;lt;Location&lt;/span&gt; &lt;span class="s"&gt;/&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;# enable tracking uploads in /&lt;/span&gt;
    &lt;span class="nb"&gt;TrackUploads&lt;/span&gt; &lt;span class="k"&gt;On&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Location&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;Location&lt;/span&gt; &lt;span class="s"&gt;/progress&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="c"&gt;# enable upload progress reports in /progress&lt;/span&gt;
    &lt;span class="nb"&gt;ReportUploads&lt;/span&gt; &lt;span class="k"&gt;On&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/Location&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now all uploads will be tracked and reports are under /progress&lt;/p&gt;
&lt;p&gt;Format of the report is &lt;span class="caps"&gt;JSON&lt;/span&gt;. From nginx wiki:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The returned document is a &lt;span class="caps"&gt;JSON&lt;/span&gt; text with the possible 4 results:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;the upload request hasn&amp;#8217;t been registered yet or is unknown:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;new Object({ &amp;#8216;state&amp;#8217; : &amp;#8216;starting&amp;#8217; })&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;the upload request has ended:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;new Object({ &amp;#8216;state&amp;#8217; : &amp;#8216;done&amp;#8217; })&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;the upload request generated an &lt;span class="caps"&gt;HTTP&lt;/span&gt; error:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;new Object({ &amp;#8216;state&amp;#8217; : &amp;#8216;error&amp;#8217;, &amp;#8216;status&amp;#8217; : &lt;error code&gt; })&lt;/p&gt;
&lt;p&gt;One error code that is interesting to track for clients is &lt;span class="caps"&gt;HTTP&lt;/span&gt; error 413 (Request entity too large)&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;the upload request is in progress:&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;new Object({ &amp;#8216;state&amp;#8217; : &amp;#8216;uploading&amp;#8217;, &amp;#8216;received&amp;#8217; : &lt;size_received&gt;, &amp;#8216;size&amp;#8217; : &lt;total_size&gt;})&lt;/p&gt;
&lt;p&gt;The &lt;span class="caps"&gt;HTTP&lt;/span&gt; request to this location must have either an X-Progress-ID parameter or X-Progress-ID &lt;span class="caps"&gt;HTTP&lt;/span&gt; header containing the unique identifier as specified in your upload/&lt;span class="caps"&gt;POST&lt;/span&gt; request to the relevant tracked zone. If you are using the X-Progress-ID as a query-string parameter, ensure it is the &lt;span class="caps"&gt;LAST&lt;/span&gt; argument in the &lt;span class="caps"&gt;URL&lt;/span&gt;.&lt;/blockquote&gt;&lt;/p&gt;
&lt;p&gt;Now the last thing to do is to implement progress bar. I don&amp;#8217;t like repeating others and &lt;a href="http://blog.new-bamboo.co.uk/2007/11/23/upload-progress-with-nginx"&gt;there is great tutorial on setting up upload progress bar with nginx and merb&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;&lt;span class="caps"&gt;UPDATE&lt;/span&gt;&lt;/strong&gt;: I released jquery upload progress library with Safari 3 support. More info &lt;a href="http://drogomir.com/blog/2008/6/30/upload-progress-script-with-safari-support"&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;strong&gt;UPDATE2&lt;/strong&gt;: I&amp;#8217;ve upgraded prototype version to work in Safari.&lt;/p&gt;
&lt;p&gt;It&amp;#8217;s for merb and nginx but if you drop the scripts in your rails app and with apache-upload-progress-module it will work. :) Basically if you have your own code handling uploads (for example using attachment_fu) you can just add javascript and css &amp;#8211; it&amp;#8217;s unobtrusive.&lt;/p&gt;
&lt;p&gt;If you&amp;#8217;re using prototype I&amp;#8217;ve rewritten script and made &lt;a href="http://drogomir.com/files/blog/prototype-upload-progress/example"&gt;a demo&lt;/a&gt;. &lt;a href="http://drogomir.com/files/blog/prototype-upload-progress/"&gt;You can also grab files&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I hope you enjoy this article. Progress bar is in my opinion one of the most useful technics &amp;#8211; there is nothing more annoying than large file uploading without any info on state of an upload.&lt;/p&gt;</content>
 </entry>
 
 
</feed>
