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

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...

Tutorial on how to write and run a javaFX 11 Spring Boot program using Eclipse

Since the decoupling of javaFX from the JDK (see http://openjfx.io ) it has become somewhat of a challenge to use javaFX with the new module capability since java 9.  This tutorial is a brief roadmap between a java 8 javafx to the java 11 version.  All code for this sample resides in https://github.com/marioja/javafx  and can be imported into eclipse and should run as is.  You can also do this manually following these instructions. First you need to use an eclipse that supports the java 11 execution environment (Eclipse 2018-12 at the time of this writing).  Then you need to make sure you have a java 11 jdk installed(11.0.2 at the time of this writing).  I downloaded openjdk11  and unzipped into a folder on my home directory (user profile on windows).  Lets call this directory jdk-11.0.2.  You should add the eclipse -vm argument in the eclipse.ini file  to select jdk-11.0.2. Start eclipse and create a brand new workspace (one that d...

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...