Thursday, January 1, 2009

Learning Ext JS

Packt Publishing recently published a new book Learning Ext JS

This book is well written and examples are easy to follow. although they are using PHP for most of the examples, it is fairly easy to convert to rails. It is a very good and time saving introduction to the Ext JS framework before digging into the official documentation.

Ext Scaffold returns

In a previous post, I spoke about Ext Js scaffolding with the plugin written by Martin Rehfeld

The exact description is the following :

The Ext Scaffold Generator Plugin is a drop-in replacement for Rails’ standard Scaffold Generator. Accepting the very same options, it will generate views using data grid and form components from the Ext JS Javascript GUI framework as well as a controller acting as an Ext-compatible JSON web service. The generated code can be used as a starting point for further implementation and outlines solutions on how to integrate the Ext JS library with Rails as a backend.


you can now find the plugin on github

and install it in your application like this :

ruby script/plugin install git://github.com/martinrehfeld/ext_scaffold.git

More info on GL Network

The advantage of this new version is that all actions happen in the same window (in your index view). The javascript code is now explicit and does not use magic helpers anymore. This is a great improvement. It allows an easy customization of the code and is a great resource to learn how to interact with the Ext JS framework and rails. It also facilitates integration in your current application.

Monday, December 8, 2008

New rails 2.2.Sql Server adapter

There is a new SQL-Server adapter for active-record. The new features are the following :

- Enabled support for DDL transactions.
- Micro second support. Time#usec is automatically converted to SQL Server's 3.33 millisecond limitation.
- Datetime data types before type casting are represented correctly. For example: 1998-01-01 23:59:59.997
- Implementation for #disable_referential_integrity used by ActiveRecord's Fixtures class.
- Pessimistic locking suppot. See the #add_lock! method for details.
- Enabled #case_sensitive_equality_operator used by unique validations.
- Unicode character support for nchar, nvarchar and ntext data types.
- View support for table names, identity inserts, and column defaults.

to install it :

gem install rails-sqlserver-2000-2005-adapter -s http://gems.github.com

More info on article on railsinside.com or get the code on git

Update : After some tests I can confirm it does not function with SQL Server 2008, so stick to the normal adapter mentionned in my previous post if you are running SQL Server 2008 if you want to avoid the nice message saying "Currently only 2000 and 2005 are supported". Somehow frustrating if you have 2008 !

New Update : It seems that there is a workaround ! You can play with the constant SUPPORTED_VERSIONS by adding 2008 to it. The reason why it is not officially supported is that the author would like to rewrite the code to take advantage of the new features of SQL Server 2008. I can only thank him for this idea, let's hope the wait won't be too long !

By the way, this adapter is now also tested and improved for rails 2.3.2

Sunday, November 2, 2008

Using will_paginate for pagination

Since Rails 2.0 the pagination has been removed from the core rails. This allows more flexibility in the choice of the pagination method you want to use.

One secure choice is will_paginate. It is available as a gem

To install the will_paginate gem follow the procedure :

1. rake gems:install
2. gem sources -a http://gems.github.com
3. gem install mislav-will_paginate

The usage of this gem in a project is simple :

1. In your config\environment.rb file add

Rails::initializer.run do |config|
config.gem 'mislav-will_paginate', :version => '~> 2.2.3', :lib => 'will_paginate', :source => 'http://gems.github.com'
end


2. rake gems:install

3. In your controller, replace

@yourmodels = Yourmodel.find

with

@yourmodels = Yourmodel.paginate :page => params[:page], :order => 'created_at DESC'

4. In your index view, add

<%= will_paginate @yourmodels %>

Note : a trick, if it tells you it does not find the method in the controller, configure the gem directly in config\development, test or production file.

Sunday, October 26, 2008

Switching to Thin server

By default, rails uses Webrick which is not particularly efficient. Although a lot of rails sites are using Mongrel in a cluster mode, I've chosen to use Thin.

The installation is really easy :

Yourapplicationdir$\ gem install thin

Then choose the i386 version when asked (at least with such a name, you're sure it's working on Windows).

Usage: thin start instead of the usual ruby script/server start

More information can be found here or on this site.

Ext JS GUI Designer

There is a GUI designer for Ext-js that can be found here

More about it can be found in this ext js forum.

Sunday, September 7, 2008

Rails against a legacy sql server database

The main problem with legacy database is that you can't do what you want. And there is 99% of chances that your legacy database does not follow the rails conventions.

Here are a few tricks to simplify your life :

1. First of all, SQL Server views are updatable so use them !

ex. create view
as
select mylegacyid as id,
originalcolumn1,
originalcolumn2,
originalcolumn3,
originalcolumn4,
originalcolumn...,
record_creation_date as created_at,
record_modification_date as updated_at

This way your legacy system continues to work correctly and you have a Rails layer built on top using specific views.

Then a trick, as rails builds its indentation on the last record in the database, you must have at least one record in each table otherwise rails won't be able to insert a new record with this method. I've found that this method works well with tables with few fields (25 fields) and saves time.

The other method is to modify the conventions and specify everything (key, table name etc..) manually. If you can't avoid it, here are a few tips :

HowToUseLegacySchemas

Legacy databases

Masking the Database

Rails and Legacy databases

My advice here is to start generating your empty scaffold with the rails convention for your model : ruby script/generate scaffold Car

Then you need to edit the model and indicate the primary key and the table name:

class Car < ActiveRecord::Base
set_table_name "TABLE_VintageCars"
set_primary_key "MyPrimaryKey_Identifier"
end

Then finally update your views. Be carefull with the column names as you know that rails is case sensitive (not like most of your SQL Server or ODBC setup).

This method functions as well and although the creation of new views seems to conform more to the rails philosophy, it might be usefull not to add a ton of new object in your db and to live with existing tables.