Build Joomla 1.5 MVC The Ruby on Rails Style

This articles explains the a ruby on rails plug-in I developed that utilizes some features of Ruby on Rails such as Plug-ins, Rake, Generators, Migration and ActiveRecord to speed up some development time for Joomla projects.

This is not a first time that someone used Ruby related tools for other projects than ruby. Capistrano, the web application deployment utility written in Ruby, has been used for Joomla sites deployments. You can read about it at http://www.adaruby.com/2007/08/14/deploying-joomla-using-capistrano-20/

Why I made this tool


I am a ruby on rails developer who has started developing in Joomla 1.5 Content Management System. Joomla 1.5 has a MVC-based architecture very similar to one in Ruby on Rails. It’s very clean and understandable. The only problem is that much of the setting up and packing procedures requires manual labor. It is possible to use ant to do some packaging but the level of automation acquired using ant is limited compared to rake (make in ruby) and in addition in ant you must write long XML scripts. Yak.Moreover Ruby On Rails comes with pre-packaged generating tools to create empty skeletons for controllers, models and views which can be used for Joomla projects with some modifications. In addition, it is possible to use Rails Migration class to create the install and uninstall SQL files. That means instead of writing syntax-error prone SQL statements, you can write them in plain ruby and it gets translated to SQL statements.Who can read this


I made the following assumptions about the your level of knowledge in Ruby on Rails and Joomla.

  1. You are a rails developers who has recently turned to Joomla 1.5
  2. You know how to set up a rails environment and make it work with MySQL
  3. You know the ruby scripting language and Rails and have good understanding about some rails tools and elements such as Plug-ins, Rake, Generators, Migration and ActiveRecord
  4. You know about Joomla 1.5 and know how to set it up with Apache, MySQL
  5. You have a good understanding about Joomla extensions like components, modules and plug-ins and you are familiar with new Joomla 1.5 MVC framework

Lets start

Download Joomla 1.5. and Install and set it up. Checkout the Joomla site for installation instruction.
Download rails and set it up. check out http://rubyonrails.org for instructions.

First lets create a rails project and call it joomrails. In shell we type

rails joomrails

This creates a rails project named joomrails. Then let’s install the joomrails plugin the googlecode repository

cd joomrails
./script/plugin
install http://joomrails.googlecode.com/svn/trunk

The joomrails rails plugin gets installed. As the result some of the rails default directory like public, app gets removed. It’s okay since we are not working on a rails project. Now Lets create a Joomla Wrapper within our rails project.

./script/generate joomla /path/to/your/joomla/project

This creates three directories
japp -> soft link to your joomla project directory
jextensions -> holds your joomla extensions
jpkg -> hold the packaged extensions

Everything is set. Let’s create a component named web_realtor

./script/generate jcomponent web_realtor

this creates the following joomla component skeleton

As an example let’s create a controller for the admin site and name it property_manager.

./script/generate jcontroller admin web_realtor poperty_manager

This creates a component controller and view skeleton named property_manager with all the necessary files and folders

Same way you can create models. For example to create a property model we type

./script/generate jmodel admin property

Now lets build and package the component, we type

rake com:webrealtor:build

this builds and packages the components and places it in the pkg folder. This also iterates through the component folder and creates a manifest and places within webraltor.xml file which is the component install file.

There is more to joomrails than just generators. There is a use of ActiveRecord and Migration files to create SQL statements using the ruby languages instead of writing it in plain SQL. Ruby on Rails developers really appreciate this feature. The db folder within the component webrealtor, contains a file called 01_install.rb. The structure of the file is as follow

The method self.up is to create install.sql file and self.down will generate the uninstall.sql file. So lets create a table properties for our model property

 

Now if we build this component the install.rb will generate two files install.sql and uninstall.sql in the admin folder that will look like this.

install.sql file:

CREATE TABLE `#__properties` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `user_id` int(11) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `address` varchar(255) DEFAULT NULL) ENGINE=InnoDB;

uninstall.sql:

DROP TABLE `#__properties`;

Another feature that I added is a way to be able to continue developing on a component after installing it. The problem is that after a component is installed the source is copied to the joomla project directory under joomla/administrator/components/com_adminyourcomponent and joomla/components/com_siteyourcomponent folders. by typing rake com:componentname:link the installed component folders in the joomla project joomla/administrator/components/com_adminyourcomponent and joomla/components/com_siteyourcomponent are deleted and instead soft links to your component site and admin folders in the jextension directory are created. This allows us to use the existing project of our component to develop.

So in our example we type

rake com:webrealtor:link

This instruction ends here. Leave me a comment if you need some help using this. Or let me know if there are bugs which is for sure. I am hosting the project home page at goolge at joomrails.googlecode.com.

7 Comments

  1. Generate Joomla! 1.5 MVC the Ruby on Rails style at rmd Studio Blog Says:

    […] My friend and business associate Arash from PeerGlobe technology has written an awesome tutorial to help you Build Joomla! MVCs the Ruby on Rails Style. […]

  2. Arash Says:

    For later, I will remove the dependency to the rails by packaging the generator as a ruby gem. And now since Ruby comes with most of the linux distribution it woudn’t be too much trouble to install it.

  3. Build Joomla 1.5 MVC The Ruby on Rails Style Says:

    […] auction wrote an interesting post today onHere’s a quick excerptThis articles explains the a ruby on rails plug-in I developed that utilizes some features of Ruby on Rails such as Plug-ins, Rake, Generators, Migration and ActiveRecord to speed up some development time for Joomla projects. … […]

  4. Shmuel Says:

    Looks pretty promising.
    Until I tried to compile with rake :(

    I’m using windows (without cygwin) by the way.
    Had to make some changes and skip creation of the symbolic link.

    First I did
    rake com:webrealtor:build
    Then got error like this:
    Please specifiy a component name in the format of COM=component

    Then I tried:
    rake com=webrealtor

    The output is:
    E:/programs/Ruby/bin/ruby -Ilib;test “E:/programs/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb”
    E:/programs/Ruby/bin/ruby -Ilib;test “E:/programs/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb”
    Errors running test:units!

    with –trace:

    ** Invoke default (first_time)
    ** Invoke test (first_time)
    ** Execute test
    ** Invoke test:units (first_time)
    ** Invoke db:test:prepare (first_time)
    ** Invoke environment (first_time)
    ** Execute environment
    ** Invoke db:abort_if_pending_migrations (first_time)
    ** Invoke environment
    ** Execute db:abort_if_pending_migrations
    ** Execute db:test:prepare
    ** Invoke db:test:clone (first_time)
    ** Invoke db:schema:dump (first_time)
    ** Invoke environment
    ** Execute db:schema:dump
    ** Invoke test:functionals (first_time)
    ** Invoke db:test:prepare
    ** Execute test:functionals
    E:/programs/Ruby/bin/ruby -Ilib;test “E:/Path/To/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb”
    ** Invoke test:integration (first_time)
    ** Invoke db:test:prepare
    ** Execute test:integration
    E:/programs/Ruby/bin/ruby -Ilib;test “E:/Path/To/Ruby/lib/ruby/gems/1.8/gems/rake-0.8.1/lib/rake/rake_test_loader.rb”
    Errors running test:units!

    So the unit test fails or something.

    This is what I have in the logs:

    # Logfile created on Wed Apr 02 09:42:35 +0300 2008 SQL (0.000000) SET NAMES ‘utf8′
    SQL (0.000000) SET SQL_AUTO_IS_NULL=0
    SQL (0.110000) CREATE TABLE `schema_info` (version int(11))
    SQL (0.031000) INSERT INTO `schema_info` (version) VALUES(0)
    SQL (0.000000) SET NAMES ‘utf8′
    SQL (0.000000) SET SQL_AUTO_IS_NULL=0

    As I understand from the log that the connection to the (test) database succeeeded.
    The table was created in the test database.

    Would be a nice thing to have some info on how to better use this tool.
    Mayvbe som API docs?

    Anyway this is very good tool for joomla, looks a lot like Symphony.
    It would be just nice to be able to use it.
    Maybe you could open a forum?

    What do the guys at Joomla! think about this tool?

  5. Ruby Cod Says:

    Hi, with this RoR plugin can I make joomla plugin in ruby ?

  6. Chad Wagner Says:

    Hey Arash, just found something in the most recent build of your jcontroller template.

    in controller.php, line 1 says “?php>” (it’s missing the 1st “<”)

    I’m playing around right now, that’s great… it’s building the structure of the directories which is cool like rails. Thanks for the code!!!

    /trunk/generators/jcontroller/templates/controller.php

  7. Thomas Reitz Says:

    “Hi, I read most of your article and found it great. which is more informative. you want to log on http://www.artfactory.com which is helpful.

    Thank you.

Leave a Reply