Scaffolding from schema.rb

Tomaso Minelli, <tomaso.minelli@unipd.it>

  Rails Conf Europe

  Berlin

  September 2008

A Red Ruby #!/usr/bin/env ruby

About me - work

About me - life

About me - session

Scaffolding

./script/generate scaffold ... first floor      
      ./script/generate scaffold ... second floor
... AND SO ON ...
rake scaffolding:application:from:schema entire building

DHH Scaffolding

AjaxScaffold Scaffolding

ActiveScaffold

Streamlined

Compare Scaffolding Ideas

Do you remember algebra?
5 : 10 = 50 : 100

Rails
Scaffolding
System
: ActiveScaffold
or
Streamlined
= AjaxScaffold : ActiveScaffold
Just Evolution!

Panorama

Schema.rb

Schema.rb ten day ago

Scaffolding from schema.rb

Typical use of schema.rb
schema.rb SQL
Scaffolding FROM schema.rb: first step
schema.rb Ruby Data Structure
Scaffolding FROM schema.rb: second step
Ruby Data Structure Migrations, models, controller, test, layout

Scaffolding from schema.rb - Synthesis

Live demo

Let's CODE!

Scaffolding from schema.rb - Step 1

Create Rails Application

Scaffolding from schema.rb - Step 2

Install the plugin...

Scaffolding from schema.rb - Step 3

...ActiceScaffold plugin will be installed automagically

Scaffolding from schema.rb - Step 4

Let's configure database connection...

Scaffolding from schema.rb - Step 5

...with your parameters

Scaffolding from schema.rb - Step 6

Let's do SFS rake task: generate schema.rb

Scaffolding from schema.rb - Step 7

Ask when overwriting application controller...

Scaffolding from schema.rb - Step 8

Ask when overwriting routes and helper

Scaffolding from schema.rb - Step 9

Start Rails server...

Scaffolding from schema.rb - Step 10

...and love your application scaffold

Scaffolding from schema.rb - Controller

Controller contain a complete active_scaffold configuration: you must only delete unnecessary fields

class AccountsController < ApplicationController
	before_filter :set_title
	...
	active_scaffold :account do |config|
		...
		config.actions.add :live_search
		...
		config.list.columns = [:id, :code, :description, :tipo, :category, :customer_vendor, :link, :gifi, :debit, :credit, :parent_id, :doc_rows, :partners, :tran_rows, :vat_registers]
		...
	end
end

Scaffolding from schema.rb - Routes, Layout

Set the root controller

map.root :controller => "accounts"

Layout include style and scripts, title and menu

<title ><%= @title %></title>
	<%= javascript_include_tag :defaults %>
	...
	<div id="top_nav"><%= render(:partial => '/layouts/top_menu') %></div>
	...
	<%= render(:partial => "/layouts/side_menu") %>
	...

Scaffolding from schema.rb - Models 1

We include all direct model associations...

class Account < ActiveRecord::Base
	has_many :doc_rows, :class_name => 'DocRow', :foreign_key => 'account_id'
	has_many :partners, :class_name => 'Partner', :foreign_key => 'account_id'
	has_many :tran_rows, :class_name => 'TranRow', :foreign_key => 'account_id'
	has_many :vat_registers, :class_name => 'VatRegister', :foreign_key => 'account_id'
	...
	def to_label
		self.to_s
	end
end

Scaffolding from schema.rb - Models 2

...and all possibles has_many :through associations (dashed)

#has_many :docs, :through => doc_rows
#has_many :items, :through => doc_rows
#has_many :tax_rates, :through => doc_rows
#has_many :accounts, :through => doc_rows
#has_many :payments, :through => partners
#has_many :banks, :through => partners
#has_many :tax_rates, :through => partners
#has_many :accounts, :through => partners
#has_many :trans, :through => tran_rows
#has_many :accounts, :through => tran_rows
#has_many :tax_rates, :through => tran_rows
#has_many :accounts, :through => vat_registers

Scaffolding from schema.rb - Helper

The top and side menus content (Streamlined style)

def top_menus
	...
end
def side_menus
	...
end

and the active_scaffold bindings

def active_scaffold_controller_for(klass)
	...
end

Q&A

Q&A

Tomaso Minelli
minni -at- stat.unipd.it
http://www.rails-experience.com