Thor – add jQuery example

I create new Rails3 apps quite frequently. As you know, Rails is packaged with Prototype- the Javascript library. I prefer to use the jQuery library. So, I need to manually add the library file. I can copy it from another project but that requires me to type a long command such as
cp ../../source.../file ./public/javascripts

There are other problems. Rails projects have a ‘rails.js’ file which needs to be swapped out with a jQuery compatible file. Also, we need to include the Javascript files inside our html head tag. To save some time I decided to create a new Thor task to accomplish all this work. Once the task is created I can simply run the following command inside the root directory of any new Rails app:

thor fbp_tools:jQuerify

fbp_tools is the name of my custom collection of thor tasks. Here is what happens when the jQuerify task runs:

[code]
…$ thor fbp_tools:jQuerify
jQuery
create public/javascripts/jquery-1.4.3.min.js
create public/javascripts/rails.js
inject app/views/layouts/application.html.haml
NOTICE: please open and verify your modified layout
[/code]

We can see that 2 files were created in correct Rails directories. Also, the application.html.haml layout template file was opened and modified (the term for this is “inject”). Finally a notice to the user suggesting the verification of the modified layout file due to the fact that .haml template files require proper white-space alignment. (I’ll post something about the amazing haml system in a future article.)

What happens if we run it again?

[code]
…$ thor fbp_tools:jQuerify
jQuery
identical public/javascripts/jquery-1.4.3.min.js
identical public/javascripts/rails.js
inject app/views/layouts/application.html.haml
NOTICE: please open and verify your modified layout
[/code]

Since the files are identical, nothing happens. Also, the inject procedure doesn’t need to do anything because it detects the previous edit.

Leave a Reply

Your email address will not be published. Required fields are marked *