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 is

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 does not exist).  I called it javafx-tutorial.  If

Put your WARs on a diet with Maven: Maven and skinny wars!

I have seen a lot of confusion and misunderstanding on what is and why use a java enterprise concept called skinny war . In simple terms , a skinny war is a WAR where some of its dependencies are moved from the WAR module WEB-INF/lib to the EAR lib folder .   This post is specifically written to deal with skinny wars and does not explain or go into details which jars must be in the WAR class loader and which one do not have to . It is assumed that the reader is familiar with this concept and the concept of the WAR class loader versus the application class loader . Suffice it to say that typically, JAR that must be scanned by CDI usually belongs in the WAR class loader . Web fragments and jars with tag libraries are other candidates . Also, all test and provided scope artifacts must be specified in the WAR module as having maven remove them from the WAR does not make sense as they were never to be put there and having them in a deps POM file does not work . They will not be e