Skip to main content

Agile Programming, Continuation

OK, I said I would continue some other time and if history was any indication, it would have been a long time. But I had the burning urge to say something and my ardent followers were just holding their breath to read it (:-)).

I talked about conventions and ignorance. Here is a case in point (and actually this is a really good example of how to do a multiple selection list in Ruby On Rails).

Here is how to get started. In a windows command prompt window you type:

cd c:\workspaces
rails msexample
cd msexample
ruby script\generate model Movie title:string producer:string
ruby script\generate model Category label:string
ruby script\generate migration CreateCategoriesMovies

Then you edit db\migrate\003_create_categories_movies.rb as follows:

class CreateCategoriesMovies < ActiveRecord::Migration
def self.up
create_table :categories_movies, :id => false do |t|
t.column :category_id, :integer
t.column :movie_id, :integer
end
add_index :categories_movies, [:category_id, :movie_id], :unique => true

end

def self.down
drop_table :categories_movies
end
end

This will add the has_and_belongs_to_many (habtm) relationship which is describe here. You need to edit the two models also. Here is app\models\category.rb:

class Category < ActiveRecord::Base
has_and_belongs_to_many :movies
end

Here is app\models\movie.rb:

class Movie < ActiveRecord::Base
has_and_belongs_to_many :categories
end

We then create the MySQL database using the following commands:

mysqladmin -u root create msexample_development
rake db:migrate

We will then create a set of standard views to be able to edit our list of movies.

ruby script\generate scaffold Movie

We will further customize the form created for this purpose by the scaffold generator by simply adding 2 lines. Here is the app\views\movies\_form.rhtml:

<%= error_messages_for 'movie' %>

<!--[form:movie]-->
<p><label for="movie_title">Title</label><br/>
<%= text_field 'movie', 'title' %></p>

<p><label for="movie_producer">Producer</label><br/>
<%= text_field 'movie', 'producer' %></p>

<p><label for="movie_categories">Categories</label><br/>
<%= select('movie', 'category_ids', Category.find(:all).collect {|p| [ p.label, p.id ] }, { :include_blank => true}, {:multiple => true, :size => 3}) %></p>
<!--[eoform:movie]-->

The last thing we need to do is to create the categories scaffold (the rails thing that will allow us to edit categories).

ruby script\generate controller Categories

We then edit app\controllers\categories_controller.rb:

class CategoriesController < ApplicationController
scaffold :category
end

We are now ready to start the server and navigate to http://localhost:3000/categories to edit and add a few categories and then http://localhost:3000/movies to edit our movies and assign categories to them.

ruby script\server

In my next post I will describe the huge number of conventions that went into making this possible. And it is worthy to note that this only required 60 lines of code (as the rake stats command show below will tell you) of which 49 were created by the rails generation software:

C:\Documents and Settings\mario\workspaces\radrails2\msexample>rake stats
(in C:/Documents and Settings/mario/workspaces/radrails2/msexample)
+----------------------+-------+-------+---------+---------+-----+-------+
| Name | Lines | LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers | 61 | 48 | 3 | 8 | 2 | 4 |
| Helpers | 7 | 6 | 0 | 0 | 0 | 0 |
| Models | 6 | 6 | 2 | 0 | 0 | 0 |
| Libraries | 0 | 0 | 0 | 0 | 0 | 0 |
| Components | 0 | 0 | 0 | 0 | 0 | 0 |
| Integration tests | 0 | 0 | 0 | 0 | 0 | 0 |
| Functional tests | 110 | 79 | 4 | 13 | 3 | 4 |
| Unit tests | 20 | 14 | 2 | 2 | 1 | 5 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total | 204 | 153 | 11 | 23 | 2 | 4 |
+----------------------+-------+-------+---------+---------+-----+-------+
Code LOC: 60 Test LOC: 93 Code to Test Ratio: 1:1.6

Comments

Popular posts from this blog

Running an I/O benchmark using IOMETER

The following document describes the methodology used at MFJ Associates for running a disk I/O benchmark.  This document assumes that the IOMETER software has been downloaded from www.iometer.org . IOMETER runs on Microsoft Windows as well as various flavors of Unix and Linux(referred to as *nix in this blog). It is made up of two components: iometer.exe a GUI program that only runs on Windows (which means you have to have at least 1 Windows desktop or server to run the GUI part) dynamo.exe or dynamo (on *nix) called the manager. In order to run a benchmark the Windows computer running iometer.exe must have TCP connectivity with the computer(s) where the benchmark will be performed.  Both must be able to connect to one another. How to run a benchmark Here is a high level view of running a benchmark. A detailed explanation will follow. You need to start the iometer.exe program on the Windows computer.  This will start the dynamo.exe program on that same compu...

Handling multipart form data in Spring 3.1

Introduction Multipart mime encoded is a format used to transmit binary and arbitrary data in 1 single HTTP request transaction. In this post, I will describe how to create and process multipart form data using Spring 3.1, the leading industry standard java application framework for creating Java web application.  I will start the discussion from the user perspective by talking about two main use cases and will expand it by describing how these two use cases translate into 7 possible application system use cases. Use Cases Here are some use cases of this feature: A browser submits or uploads a file to a web server using an HTML page. This is by far the most common use case of the multipart form data feature. A multipart is required because the form data and the file are both included in the request body. A java program (a java application or servlet instance) sends multipart form data to a web server (most likely a web service).  This...
Setting Up a WireGuard VPN: Client and Server Configuration In today's digitally connected world, secure and private communication is paramount. WireGuard is a cutting-edge VPN protocol known for its simplicity and high performance. If you're looking to set up a WireGuard VPN, here's a quick guide on what you need to do for both the client and server sides. Client Configuration The client configuration is essential for initiating a connection to the WireGuard server. Here’s what you need: WireGuard Client Software : Install the WireGuard client software on your device. Private Key : Generate a private key for the client. Server’s Public Key : Obtain the public key of the server you want to connect to. Configuration File : Create a configuration file (e.g., client.conf ) with the following settings: Client PrivateKey = <client_private_key> Address = 10.0 . 0.2 / 24 DNS = 8.8 . 8.8 [Peer] PublicKey = <server_public_key> AllowedIPs = 10.0 . 0.1 / 24 Endp...