<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marco Mastrodonato &#187; Generatori</title>
	<atom:link href="http://mastrodonato.info/index.php/tag/generatori/feed/" rel="self" type="application/rss+xml" />
	<link>http://mastrodonato.info</link>
	<description>Non c&#039;e&#039; prezzo per la miticita&#039;</description>
	<lastBuildDate>Wed, 21 Sep 2011 07:12:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Leonardo: un generatore di applicazioni Rails 3.1</title>
		<link>http://mastrodonato.info/index.php/2011/08/leonardo-generatore-di-applicazioni-rails-3-1/</link>
		<comments>http://mastrodonato.info/index.php/2011/08/leonardo-generatore-di-applicazioni-rails-3-1/#comments</comments>
		<pubDate>Mon, 22 Aug 2011 21:54:42 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Generatori]]></category>
		<category><![CDATA[Leonardo]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=697</guid>
		<description><![CDATA[L&#8217;aggiornamento alla versione più recente lo trovi sul mio nuovo blog. Click here for the english version. Ho creato un nuovo generatore per creare applicazioni rails 3.1 (che attualmente si trova in rc6) ed automatizzare le operazioni che risultano ripetitive. Il nome della gemma è leonardo ed è suddivisa in due generatori: leolay: per creare [...]]]></description>
			<content:encoded><![CDATA[<p><strong>L&#8217;aggiornamento alla versione più recente lo trovi <a href="http://marco.mastrodonato.info/2011/09/leonardo-un-generatore-di-applicazioni.html">sul mio nuovo blog</a>. Click <a href="http://en-marcomastrodonato.blogspot.com/2011/09/leonardo-rails-31-application-generator.html">here for the english version</a>.</strong></p>
<p>Ho creato un nuovo generatore per creare applicazioni rails 3.1 (che attualmente si trova in rc6) ed automatizzare le operazioni che risultano ripetitive.</p>
<p>Il nome della gemma è <a href="https://rubygems.org/gems/leonardo">leonardo</a> ed è suddivisa in due generatori:</p>
<ol>
<li><strong>leolay</strong>: per creare il layout</li>
<li><strong>leosca</strong>: per creare la risorsa (sostituisce lo scaffold)</li>
</ol>
<h2>Perchè creare un nuovo generatore se esiste già lo scaffold di rails?</h2>
<p>Lo scaffold è un comodo generatore e si può anche personalizzare con facilità ma è adatto per scopi didattici e la personalizzazione è limitata alle sole viste. In produzione mi serviva qualcosa di più completo e personalizzabile.</p>
<h2>Preparazione dell&#8217;ambiente ed installazione della gemma</h2>
<p>Creiamo un nuovo ambiente ruby ed installiamo rails 3.1:<br />
gem install rails &#8211;pre<br />
usiamo il &#8211;pre perchè in questo momento si trova in release candidate, toglietelo se invece è stata rilasciata</p>
<p>Possiamo installare la gemma semplicemente con:<br />
gem install leonardo</p>
<p>&#8230;ma non è necessario e consiglio di usare il procedimento che sto per descrivere:<br />
creiamo una nuova applicazione utilizzando il template che ho preparato, raccomandato per agevolare l&#8217;avvio e per meglio sfruttare la gemma:</p>
<p>rails new NewApp -m http://cloud.github.com/downloads/marcomd/Leonardo/template.rb</p>
<p>Se non dovesse funzionare il link potete comunque trovare il template nella root della gemma</p>
<p>Il template chiederà se installare determinate gemme esterne, per una prova consiglio di installarle tutte rispondendo <strong>y</strong> o premendo invio quando propone un default (es. devise)</p>
<p>Verrà poi eseguito un bundle install per verificare la presenza delle gemme ed in seguito verranno eseguite le varie generazioni, il tutto durerà qualche minuto circa.<br />
La generazione del layout chiederà una conferma per sostituire il file en.yml, rispondete <strong>y</strong>. Preferisco non forzare la sostituzione per permettere altre ed eventuali esecuzioni del generatore leolay nel caso si volesse aggiornare il layout.</p>
<p>Al termine otterrete un applicazione &#8220;pronta per partire&#8221; potendo così orientare lo sviluppo sulla parte applicativa.</p>
<p>Spostiamoci nella cartella dell&#8217;applicazione ed avviamo il server<br />
cd NewApp<br />
rails s<br />
indirizziamo il browser all&#8217;indirizzo http://localhost:3000 per accedere alla home</p>
<p>Se clicchiamo su &#8220;<em>Sign in</em>&#8221; verrà richiesta l&#8217;autenticazione (se l&#8217;avete inclusa), per un rapido accesso potete inserire:<br />
email: admin@newapp.com<br />
password: abcd1234</p>
<p><img title="Signed in" src="http://i.imgur.com/BFi37.png" alt="Signed in" /></p>
<p>Vengono create tre utenze con tre ruoli diversi (nel caso avete incluso le autorizzazioni):</p>
<ol>
<li>admin@newapp.com</li>
<li>manager@newapp.com</li>
<li>user@newapp.com</li>
</ol>
<p>Naturalmente si tratta di utenze con finalità legate allo sviluppo per cui ricordate di eliminarle prima di qualsiasi rilascio. Possiamo consultare e modificare i ruoli accedendo al file app/models/ability.rb in quanto la gestione è affidata alla gemma <a href="https://github.com/ryanb/cancan">cancan</a>.</p>
<p>Per cambiare la lingua è sufficiente inviare la nuova come valore del parametro lang, esempio:</p>
<p>http://localhost:3000/?lang=it</p>
<p>il generatore attualmente gestisce :en e :it ma aggiungere il supporto ad un&#8217;altra lingua è semplice, basta aggiungere il file yml nella cartella dove si trovano i primi due, naturalmente deve avere gli stessi tags.</p>
<h2>Generare le risorse</h2>
<p>Ora generiamo qualche risorsa utilizzando il generatore leosca, una sorta di scaffold personalizzato:</p>
<p>rails g leosca category name:string active:boolean</p>
<p>rails g leosca product category:references name:string description:text price:decimal</p>
<p>ora creiamo le due nuove tabelle anche sul database:<br />
rake db:migrate</p>
<p>ed eseguiamo anche il popolamento con qualche dato che leosca ha preparato per noi:<br />
rake db:seed</p>
<p><img title="Listing Categories" src="http://i.imgur.com/rJzCB.png" alt="Listing Categories" /></p>
<p>Alcune note:</p>
<ul>
<li>L&#8217;esportazione dei dati in csv attualmente non funziona se attiva la paginazione ajax, sto cercando un buon metodo per risolvere.</li>
<li>Nelle liste vengono inseriti in automatico tutti i campi per poter applicare dei filtri, eliminiamo quelli che non ci interessano. I filtri su campi boolean non funzionano con sqlite3 a causa di un bug nel driver che non genera un sql corretto. Con SQLServer funziona alla perfezione.</li>
<li>Gli oggetti relazionati mostrano un link col nome e cliccando si effettua una :show. Nel caso la tabella non abbia il campo :name viene utilizzato l&#8217;id che è possibile sostituire con il dato che più lo rappresenta.</li>
<li>Solo l&#8217;operazione destroy è gestita tramite ajax. Show, edit e create attualmente sono gestite tramite http.</li>
<li>Riguardo le utenze create in automatico: con i ruoli di default admin può fare tutto, manager tutte le operazioni crud, user tutte tranne la delete.</li>
<li>Le etichette dei campi vengono inserite in automatico ma la traduzione è necessario farsela da se all&#8217;interno dei files config/locales/*.yml</li>
</ul>
<h2>Personalizzare il generatore</h2>
<p>E&#8217; sufficiente installarlo nel progetto e personalizzarlo secondo le proprie esigenze:<br />
rails g leosca:install<br />
si trova sotto lib ed eventuali modifiche avranno la precedenza sulla gemma</p>
<h2>Conclusioni</h2>
<p>Per ulteriori approfondimenti o aggiornamenti sugli sviluppi futuri è possibile consultare la homepage del progetto:<br />
<a href="https://github.com/marcomd/Leonardo">https://github.com/marcomd/Leonardo</a></p>
<p>Altri riferimenti:<br />
<a href="http://edgeguides.rubyonrails.org/generators.html">http://edgeguides.rubyonrails.org/generators.html</a><br />
<a href="http://railscasts.com/episodes/216-generators-in-rails-3">http://railscasts.com/episodes/216-generators-in-rails-3</a><br />
<a href="http://railscasts.com/episodes/218-making-generators-in-rails-3">http://railscasts.com/episodes/218-making-generators-in-rails-3</a><br />
<a href="http://railscasts.com/episodes/242-thor">http://railscasts.com/episodes/242-thor</a><br />
<a href="http://rdoc.info/github/wycats/thor/master/Thor">http://rdoc.info/github/wycats/thor/master/Thor</a><br />
<a href="http://textmate.rubyforge.org/thor/Thor/Actions.html">http://textmate.rubyforge.org/thor/Thor/Actions.html</a><br />
<a href="http://railscasts.com/episodes/265-rails-3-1-overview">http://railscasts.com/episodes/265-rails-3-1-overview</a></p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2011/08/leonardo-generatore-di-applicazioni-rails-3-1/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creare scheletri di applicazioni con generatori e template</title>
		<link>http://mastrodonato.info/index.php/2010/03/creare-scheletri-di-applicazioni-con-generatori-e-modelli/</link>
		<comments>http://mastrodonato.info/index.php/2010/03/creare-scheletri-di-applicazioni-con-generatori-e-modelli/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 20:32:54 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Generatori]]></category>
		<category><![CDATA[nifty-generators]]></category>
		<category><![CDATA[Templates]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=481</guid>
		<description><![CDATA[Ultima modifica 01/04/2010 Rails è un meraviglioso strumento che permette di realizzare agevolmente, un&#8217;applicazione secondo gli standard moderni. Tuttavia, quando se ne crea una nuova, è necessario eseguire diverse operazioni alquanto noiose: aggiungere plugins o gemme, il recupero di un layout di partenza e tutto ciò che di solito usate nelle vostre applicazioni. Per risolvere [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Ultima modifica 01/04/2010</strong></p>
<p>Rails è un meraviglioso strumento che permette di realizzare agevolmente, un&#8217;applicazione secondo gli standard moderni. Tuttavia, quando se ne crea una nuova, è necessario eseguire diverse operazioni alquanto noiose: aggiungere plugins o gemme, il recupero di un layout di partenza e tutto ciò che di solito usate nelle vostre applicazioni.<br />
Per risolvere questo problema, dalla versione 2.3 è possibile utilizzare i templates, modelli con cui è possibile diversificare la creazione di scheletri di applicazioni. Si trovano molti esempi su internet, io ne presento uno che mostra i vantaggi in combinazione con un generatore personalizzato per le proprie esigenze. <a href="http://railscasts.com/episodes/58-how-to-make-a-generator">Questa guida </a> spiega come creare un generatore. Se siete impazienti potete partire da qualcosa di già fatto come ho fatto io. Ho personalizzato il nifty-generators di Ryan Bates aggiungendo alcune nuove caratteristiche:</p>
<ul>
<li>Layout: ora supporta stili multipli: classic, cloudy and blackwhite. Niente di che, solo una base da sviluppare</li>
<li>Layout: ho inserito il file di configurazione config.rb, contenitore delle costanti applicative come il nome dell&#8217;applicazione, lo stile di default ecc.</li>
<li>Ho introdotto la localizzazione: il nifty_layout aggiunge i files yaml per la lingua en e it. Nell&#8217;application controller viene inserito del codice per la gestione, con il parametro lang si cambia l&#8217;impostazione della lingua (esempio ?lang=it) che verrà mantenuta per tutta la sessione. Il nifty_scaffold aggiunge le risorse e i suoi attributi, pronti per essere tradotti.</li>
<li>nifty_layout: la div per i messaggi è stata spostata dentro un partial nella cartella shared. Ho preferito fare questo per poterla utilizzare anche tramite ajax</li>
<li>nifty_scaffold: le views new e edit, ora utilizzano due partial: render @model che a sua volta richiama il secondo con all&#8217;interno i campi. Come la versione originale, supporta haml e sass ed ho aggiunto il supporto a formtastic potendo quindi scegliere la combinazione preferita!</li>
</ul>
<p></p>
<p>La nuova versione del nifty-generators è scaricata dal template che vi mostrerò, comunque, questi sono i <a href="http://github.com/marcomd/nifty-generators">sorgenti</a>, <a href="http://github.com/marcomd/nifty-generators/downloads">qua</a> invece, potete scaricare l&#8217;ultima versione della gemma e del template.</p>
<p>Cos&#8217;è che dovrebbe fare il generatore e cosa il template?<br />
Beh, i templates dovrebbero essere un contenitore di operazioni da compiere, eseguiti nella fase di creazione di un nuovo progetto. Con i generatori invece, è possibile fare quasi tutto. In genere, si aggiungono nuove funzionalità o si personalizza qualcosa che già esiste. Può essere eseguito ogni volta che si vuole da riga di comando e naturalmente, richiamato all&#8217;interno di un template.</p>
<p>Chi non ha mai usato un template, potrebbe iniziare con un semplice esempio o meglio, con un tutorial:<br />
questo <a href="http://railscasts.com/episodes/148-app-templates-in-rails-2-3">screencast</a> mostra come destreggiarsi e questo invece, è <a href="http://m.onkey.org/2008/12/4/rails-templates">un articolo</a> in html.</p>
<p>I molti esempi che ho visto su internet sono spesso pieni di parametri di configurazione che mi fan venire dei grossi mal di testa. Io invece ho preferito che mi venisse chiesto cosa voglio. Sicuramente, non è ancora perfetto, ma penso che sia una buona base :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#This is a Rails 2.3 template</span>
<span style="color:#008000; font-style:italic;">#Written by Marco Mastrodonato, last update on 01/04/2010 </span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;"># USAGE: rails yourapp -m template.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#This is the unique parameter, after first execution use FALSE to avoid installation</span>
install_gems = <span style="color:#0000FF; font-weight:bold;">false</span>
git_http = <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
WINDOWS = <span style="color:#006600; font-weight:bold;">&#40;</span>RUBY_PLATFORM =~ <span style="color:#006600; font-weight:bold;">/</span>dos<span style="color:#006600; font-weight:bold;">|</span>win32<span style="color:#006600; font-weight:bold;">|</span>cygwin<span style="color:#006600; font-weight:bold;">/</span>i<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006600; font-weight:bold;">&#40;</span>RUBY_PLATFORM =~ <span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#40;</span>:?mswin<span style="color:#006600; font-weight:bold;">|</span>mingw<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">/</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
&nbsp;
<span style="color:#008000; font-style:italic;">#plugin/install doesn't work on my windows systems</span>
<span style="color:#9966CC; font-weight:bold;">def</span> myplugin<span style="color:#006600; font-weight:bold;">&#40;</span>name, url<span style="color:#006600; font-weight:bold;">&#41;</span>
  url.<span style="color:#CC0066; font-weight:bold;">sub!</span> <span style="color:#996600;">'git://'</span>, <span style="color:#996600;">'http://'</span> <span style="color:#9966CC; font-weight:bold;">if</span> git_http
  <span style="color:#9966CC; font-weight:bold;">if</span> WINDOWS
    <span style="color:#008000; font-style:italic;">#run &quot;ruby script/plugin install #{url}&quot;</span>
    run <span style="color:#996600;">&quot;git clone #{url} vendor/plugins/#{name}&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">else</span>
    plugin name, <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> url
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'*'</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">40</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;* Processing template#{WINDOWS ? ' on windows system': ''}...&quot;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">'*'</span> <span style="color:#006600; font-weight:bold;">*</span> <span style="color:#006666;">40</span>
&nbsp;
use_git = yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Do you think to use git ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> use_git
  git <span style="color:#ff3333; font-weight:bold;">:init</span>
  file <span style="color:#996600;">&quot;.gitignore&quot;</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^    <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    .<span style="color:#9900CC;">DS_Store</span>
    log<span style="color:#006600; font-weight:bold;">/*</span>.<span style="color:#9900CC;">log</span>
    tmp<span style="color:#006600; font-weight:bold;">/**/*</span>
    config<span style="color:#006600; font-weight:bold;">/</span>database.<span style="color:#9900CC;">yml</span>
    db<span style="color:#006600; font-weight:bold;">/*</span>.<span style="color:#9900CC;">sqlite3</span>
    nbproject<span style="color:#006600; font-weight:bold;">/*</span>
  EOS
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#Everyone must to have it!</span>
plugin <span style="color:#996600;">'will_paginate'</span>,
        <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/mislav/will_paginate.git&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Will Paginate ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Attachments with no extra database tables, only one library to install for image processing</span>
plugin <span style="color:#996600;">'paperclip'</span>,
       <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/thoughtbot/paperclip.git&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Paperclip ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
formtastic = yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Formtastic ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> formtastic
  <span style="color:#008000; font-style:italic;">#gem 'justinfrench-formtastic', :lib =&gt; 'formtastic', :source =&gt; 'http://gems.github.com'</span>
  <span style="color:#008000; font-style:italic;">#rake &quot;gems:install&quot; if install_gem</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#A Rails FormBuilder DSL (with some other goodies) to make it far easier to create beautiful, semantically rich, syntactically awesome, readily stylable and wonderfully accessible HTML forms in your Rails applications.</span>
  plugin <span style="color:#996600;">'formtastic'</span>,
        <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/justinfrench/formtastic.git&quot;</span>
  <span style="color:#008000; font-style:italic;">#Adds reflective access to validations</span>
  plugin <span style="color:#996600;">'validation_reflection'</span>,
        <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/redinger/validation_reflection.git&quot;</span>
  generate <span style="color:#996600;">&quot;formtastic&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Add testing framework ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># RSpec's official Ruby on Rails plugin  </span>
  plugin <span style="color:#996600;">'rspec'</span>, 
    <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'git://github.com/dchelimsky/rspec.git'</span>
&nbsp;
  plugin <span style="color:#996600;">'rspec-rails'</span>,
         <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/dchelimsky/rspec-rails.git&quot;</span>
  generate <span style="color:#996600;">&quot;rspec&quot;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># BDD that talks to domain experts first and code 2nd</span>
  plugin <span style="color:#996600;">'cucumber'</span>,
         <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/aslakhellesoy/cucumber.git&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Add authentication ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;0. None&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;1. Devise/warden&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;2. Authlogic&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;3. Restful authentication&quot;</span>
  choose = ask<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Choose one:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">case</span> choose
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;1&quot;</span>
      gem <span style="color:#996600;">&quot;warden&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;0.9.6&quot;</span>
      gem <span style="color:#996600;">&quot;devise&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:version</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;1.0.4&quot;</span>
      rake <span style="color:#996600;">&quot;gems:install&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> install_gems
      generate <span style="color:#996600;">&quot;devise_install&quot;</span>
      generate <span style="color:#996600;">&quot;devise&quot;</span>, <span style="color:#996600;">&quot;User&quot;</span>
      generate <span style="color:#996600;">&quot;devise_views&quot;</span>
      <span style="color:#008000; font-style:italic;">#User.create!(:email =&gt; 'admin@administrator.com', :password =&gt; 'secret')</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;2&quot;</span>
      gem <span style="color:#996600;">&quot;authlogic&quot;</span>
      rake <span style="color:#996600;">&quot;gems:install&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> install_gems
      generate <span style="color:#996600;">&quot;session&quot;</span>, <span style="color:#996600;">&quot;user_session&quot;</span>
      generate <span style="color:#996600;">&quot;model&quot;</span>, <span style="color:#996600;">&quot;user&quot;</span>
      generate <span style="color:#996600;">&quot;controller&quot;</span>, <span style="color:#996600;">&quot;user_sessions&quot;</span>
      route <span style="color:#996600;">&quot;map.resource :user_session&quot;</span>
      create_users_file = <span style="color:#CC00FF; font-weight:bold;">Dir</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'db/migrate/*_create_users.rb'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">first</span>
      file create_users_file, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^        <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">class</span> CreateUsers <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
          <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
            create_table <span style="color:#ff3333; font-weight:bold;">:users</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:login</span>,               <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, you can use email instead, or both</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:email</span>,               <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, you can use login instead, or both</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:crypted_password</span>,    <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, see below</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:password_salt</span>,       <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, but highly recommended</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:persistence_token</span>,   <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># required</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:single_access_token</span>, <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::Params</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:perishable_token</span>,    <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>                <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::Perishability</span>
&nbsp;
              <span style="color:#008000; font-style:italic;"># Magic columns, just like ActiveRecord's created_at and updated_at. These are automatically maintained by Authlogic if they are present.</span>
              t.<span style="color:#CC0066; font-weight:bold;">integer</span>   <span style="color:#ff3333; font-weight:bold;">:login_count</span>,         <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span> <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#CC0066; font-weight:bold;">integer</span>   <span style="color:#ff3333; font-weight:bold;">:failed_login_count</span>,  <span style="color:#ff3333; font-weight:bold;">:null</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>, <span style="color:#ff3333; font-weight:bold;">:default</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">0</span> <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#9900CC;">datetime</span>  <span style="color:#ff3333; font-weight:bold;">:last_request_at</span>                                    <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#9900CC;">datetime</span>  <span style="color:#ff3333; font-weight:bold;">:current_login_at</span>                                   <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#9900CC;">datetime</span>  <span style="color:#ff3333; font-weight:bold;">:last_login_at</span>                                      <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:current_login_ip</span>                                   <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
              t.<span style="color:#CC0066; font-weight:bold;">string</span>    <span style="color:#ff3333; font-weight:bold;">:last_login_ip</span>                                      <span style="color:#008000; font-style:italic;"># optional, see Authlogic::Session::MagicColumns</span>
&nbsp;
              t.<span style="color:#9900CC;">timestamps</span>
            <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
            add_index <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:email</span>
            add_index <span style="color:#ff3333; font-weight:bold;">:users</span>, <span style="color:#ff3333; font-weight:bold;">:login</span>, <span style="color:#ff3333; font-weight:bold;">:unique</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
          <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
            drop_table <span style="color:#ff3333; font-weight:bold;">:users</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      EOS
      file <span style="color:#996600;">'app/models/user.rb'</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^        <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#9966CC; font-weight:bold;">class</span> User <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
          acts_as_authentic
        <span style="color:#9966CC; font-weight:bold;">end</span>
      EOS
      file <span style="color:#996600;">'spec/models/user_spec.rb'</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^        <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'spec_helper'</span>
&nbsp;
        describe User <span style="color:#9966CC; font-weight:bold;">do</span>
          before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
            <span style="color:#0066ff; font-weight:bold;">@valid_attributes</span> = <span style="color:#006600; font-weight:bold;">&#123;</span>
              <span style="color:#ff3333; font-weight:bold;">:login</span>                 <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;login&quot;</span>,
              <span style="color:#ff3333; font-weight:bold;">:email</span>                 <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;some@thing.com&quot;</span>,
              <span style="color:#ff3333; font-weight:bold;">:password</span>              <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;password&quot;</span>,
              <span style="color:#ff3333; font-weight:bold;">:password_confirmation</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;password&quot;</span>
            <span style="color:#006600; font-weight:bold;">&#125;</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
          it <span style="color:#996600;">&quot;should create a new instance given valid attributes&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
            User.<span style="color:#9900CC;">create</span>!<span style="color:#006600; font-weight:bold;">&#40;</span>@valid_attributes<span style="color:#006600; font-weight:bold;">&#41;</span>
          <span style="color:#9966CC; font-weight:bold;">end</span>
        <span style="color:#9966CC; font-weight:bold;">end</span>
      EOS
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;3&quot;</span>
      plugin <span style="color:#996600;">'restful-authentication-i18n'</span>,
          <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/dcrec1/restful-authentication-i18n.git&quot;</span>
      <span style="color:#008000; font-style:italic;">#â€”include-activation \ â€”stateful \ â€”rspec \ â€”skip-migration \ â€”skip-routes \ â€”old-passwords </span>
      generate <span style="color:#996600;">&quot;authenticated&quot;</span>, <span style="color:#996600;">&quot;user&quot;</span>, <span style="color:#996600;">&quot;sessions&quot;</span>, <span style="color:#996600;">&quot;â€”include-activation&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Authorization ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    gem <span style="color:#996600;">&quot;cancan&quot;</span>
    rake <span style="color:#996600;">&quot;gems:install&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> install_gems
    <span style="color:#008000; font-style:italic;">#plugin 'cancan', :git =&gt; &quot;git://github.com/ryanb/cancan.git&quot; </span>
    file <span style="color:#996600;">'spec/models/ability.rb'</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^      <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">class</span> Ability  
        <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">CanCan::Ability</span>  
&nbsp;
        <span style="color:#9966CC; font-weight:bold;">def</span> initialize<span style="color:#006600; font-weight:bold;">&#40;</span>user<span style="color:#006600; font-weight:bold;">&#41;</span>  
          user <span style="color:#006600; font-weight:bold;">||</span>= User.<span style="color:#9900CC;">new</span> <span style="color:#008000; font-style:italic;"># Guest user  </span>
          <span style="color:#9966CC; font-weight:bold;">if</span> user.<span style="color:#9900CC;">role</span>? <span style="color:#ff3333; font-weight:bold;">:admin</span>  
            can <span style="color:#ff3333; font-weight:bold;">:manage</span>, <span style="color:#ff3333; font-weight:bold;">:all</span>  
          <span style="color:#9966CC; font-weight:bold;">else</span>  
            can <span style="color:#ff3333; font-weight:bold;">:read</span>, <span style="color:#ff3333; font-weight:bold;">:all</span>  
          <span style="color:#9966CC; font-weight:bold;">end</span>  
        <span style="color:#9966CC; font-weight:bold;">end</span>  
      <span style="color:#9966CC; font-weight:bold;">end</span>  
    EOS
    generate <span style="color:#996600;">&quot;migration&quot;</span>, <span style="color:#996600;">&quot;add_roles_mask_to_users roles_mask:integer&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;Add these lines to user model:&quot;</span>
    <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^      <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      named_scope <span style="color:#ff3333; font-weight:bold;">:with_role</span>, <span style="color:#CC0066; font-weight:bold;">lambda</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>role<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:conditions <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;roles_mask &amp; <span style="color:#000099;">\#</span>{2**ROLES.index(role.to_s)} &gt; 0 &quot;</span><span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> roles=<span style="color:#006600; font-weight:bold;">&#40;</span>roles<span style="color:#006600; font-weight:bold;">&#41;</span>
        <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">roles_mask</span> = <span style="color:#006600; font-weight:bold;">&#40;</span>roles <span style="color:#006600; font-weight:bold;">&amp;</span> ROLES<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">map</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">**</span>ROLES.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span>r<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">sum</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> roles
        ROLES.<span style="color:#9900CC;">reject</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>r<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>roles_mask <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&amp;</span> <span style="color:#006666;">2</span><span style="color:#006600; font-weight:bold;">**</span>ROLES.<span style="color:#9900CC;">index</span><span style="color:#006600; font-weight:bold;">&#40;</span>r<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">zero</span>? <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
      <span style="color:#9966CC; font-weight:bold;">def</span> role_symbols
        roles.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&amp;</span>:to_sym<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    EOS
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>          
&nbsp;
haml = yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Haml?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
<span style="color:#9966CC; font-weight:bold;">if</span> haml
  <span style="color:#008000; font-style:italic;"># The world's greatest templating system</span>
  <span style="color:#008000; font-style:italic;">#plugin 'haml', :git =&gt; &quot;git://github.com/nex3/haml.git&quot;</span>
  gem <span style="color:#996600;">&quot;haml&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;haml&quot;</span>
  rake <span style="color:#996600;">&quot;gems:install&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> install_gems
  run <span style="color:#996600;">&quot;haml --rails .&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Layout ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  plugin <span style="color:#996600;">&quot;marcomd-nifty-generators&quot;</span>, 
    <span style="color:#ff3333; font-weight:bold;">:lib</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;marcomd-nifty-generators&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:git</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;git://github.com/marcomd/nifty-generators.git&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; 1. classic&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; 2. cloudy&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot; 3. blackwhite&quot;</span>
  <span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;or. write names separed by space&quot;</span>
  style = ask<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Choose style:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  style = <span style="color:#9966CC; font-weight:bold;">case</span> style
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;1&quot;</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#996600;">&quot;classic&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;2&quot;</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#996600;">&quot;cloudy&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">when</span> <span style="color:#996600;">&quot;3&quot;</span> <span style="color:#9966CC; font-weight:bold;">then</span> <span style="color:#996600;">&quot;blackwhite&quot;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span> style
  <span style="color:#9966CC; font-weight:bold;">end</span>
  generate <span style="color:#996600;">&quot;nifty_layout&quot;</span>, <span style="color:#996600;">&quot;application #{style} #{haml ? '--haml' : ''} #{formtastic ? '--formtastic' : ''}&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Link to local copy of edge rails</span>
<span style="color:#008000; font-style:italic;">#inside('vendor') { run 'ln -s ~/dev/rails/rails rails' } if yes?(&quot;Rails ?&quot;)</span>
rake<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;rails:freeze:gems&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Freeze rails gems ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Mailer dummy config</span>
initializer <span style="color:#996600;">&quot;mailer.rb&quot;</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^  <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  mailer_options = <span style="color:#CC00FF; font-weight:bold;">YAML</span>.<span style="color:#9900CC;">load_file</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;<span style="color:#000099;">\#</span>{Rails.root}/config/mailer.yml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#6666ff; font-weight:bold;">ActionMailer::Base</span>.<span style="color:#9900CC;">smtp_settings</span> = mailer_options
EOS
file <span style="color:#996600;">&quot;config/mailer.yml&quot;</span>, <span style="color:#006600; font-weight:bold;">&lt;&lt;-</span>EOS.<span style="color:#CC0066; font-weight:bold;">gsub</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">/</span>^  <span style="color:#006600; font-weight:bold;">/</span>, <span style="color:#996600;">''</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#ff3333; font-weight:bold;">:address</span>: mail.<span style="color:#9900CC;">authsmtp</span>.<span style="color:#9900CC;">com</span>
  <span style="color:#ff3333; font-weight:bold;">:domain</span>: yourdomain.<span style="color:#9900CC;">com</span>
  <span style="color:#ff3333; font-weight:bold;">:authentication</span>: <span style="color:#ff3333; font-weight:bold;">:login</span>
  <span style="color:#ff3333; font-weight:bold;">:user_name</span>: USERNAME
  <span style="color:#ff3333; font-weight:bold;">:password</span>: PASSWORD
EOS
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Generate controller home ? (suggested)&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  generate <span style="color:#996600;">&quot;controller&quot;</span>, <span style="color:#996600;">&quot;home&quot;</span>, <span style="color:#996600;">&quot;index&quot;</span>
  route <span style="color:#996600;">&quot;map.root :controller =&gt; :home&quot;</span>
<span style="color:#9966CC; font-weight:bold;">else</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
rake <span style="color:#996600;">&quot;gems:install&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> install_gems
<span style="color:#008000; font-style:italic;"># Unpack all gems to vendor/gems</span>
rake <span style="color:#996600;">&quot;gems:unpack&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> yes?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;Unpack to vendor/gems ?&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
rake <span style="color:#996600;">&quot;db:create:all&quot;</span>
rake <span style="color:#996600;">&quot;db:migrate&quot;</span>
<span style="color:#CC00FF; font-weight:bold;">File</span>.<span style="color:#9900CC;">unlink</span> <span style="color:#996600;">&quot;public/index.html&quot;</span>
&nbsp;
git <span style="color:#ff3333; font-weight:bold;">:add</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;.&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:commit</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;-m 'initial commit'&quot;</span> <span style="color:#9966CC; font-weight:bold;">if</span> use_git
&nbsp;
<span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;ENJOY!&quot;</span></pre></div></div>

<p><strong>Questo è uno dei possibili risultati:</strong></p>
<p><code><br />
marco@d9400:~/Rails$ rails Blog -m marcomd.rb<br />
      create  bla bla bla<br />
    applying  template: marcomd.rb<br />
Processing template...<br />
      plugin  will_paginate<br />
Unpacking objects: 100% (57/57), done.<br />
From git://github.com/mislav/will_paginate<br />
 * branch            HEAD       -> FETCH_HEAD<br />
              Paperclip ?<br />
y<br />
      plugin  paperclip<br />
Unpacking objects: 100% (78/78), done.<br />
From git://github.com/thoughtbot/paperclip<br />
 * branch            HEAD       -> FETCH_HEAD<br />
              Formtastic ?<br />
y<br />
         gem  justinfrench-formtastic<br />
  generating  formtastic_stylesheets<br />
              Add testing framework ?<br />
y<br />
      plugin  rspec<br />
From git://github.com/dchelimsky/rspec<br />
 * branch            HEAD       -> FETCH_HEAD<br />
      plugin  rspec-rails<br />
From git://github.com/dchelimsky/rspec-rails<br />
 * branch            HEAD       -> FETCH_HEAD<br />
      plugin  cucumber<br />
From git://github.com/aslakhellesoy/cucumber<br />
 * branch            HEAD       -> FETCH_HEAD<br />
              Add authentication ?<br />
y<br />
0. None<br />
1. Devise/warden<br />
2. Authlogic<br />
3. Restful authentication<br />
              Choose one:<br />
1<br />
         gem  warden<br />
         gem  devise<br />
  generating  devise_install<br />
              Authorization ?<br />
n<br />
              Haml?<br />
n<br />
              Layout ?<br />
y<br />
      plugin  marcomd-nifty-generators<br />
From git://github.com/marcomd/nifty-generators<br />
 * branch            HEAD       -> FETCH_HEAD<br />
 1. classic<br />
 2. cloudy<br />
 3. blackwhite<br />
or. write names separed by space<br />
              Choose style:<br />
2<br />
  generating  nifty_layout<br />
              Freeze rails gems ?<br />
n<br />
 initializer  mailer.rb<br />
        file  config/mailer.yml<br />
              Generate controller home ? (suggested)<br />
y<br />
  generating  controller<br />
       route  map.root :controller => :home<br />
              Unpack to vendor/gems ?<br />
n<br />
        rake  db:create:all<br />
        rake  db:migrate<br />
ENJOY!<br />
     applied  marcomd.rb<br />
</code></p>
<p>Ecco fatto, ora avviamo mongrel</p>
<p><code><br />
marco@d9400:~/Rails$ cd Blog/<br />
marco@d9400:~/Rails/Blog$ script/server</p>
<p>=> Booting Mongrel<br />
=> Rails 2.3.5 application starting on http://0.0.0.0:3000<br />
=> Call with -d to detach<br />
=> Ctrl-C to shutdown server<br />
</code></p>
<p>http://localhost:3000 e controlliamo il nostro nuovo blog</p>
<p><a href="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd1.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd1-300x107.png" alt="This is only the beginning" title="Questo è solo l'inizio" width="300" height="107" class="aligncenter size-medium wp-image-462" /></a></p>
<p>Ora aggiungiamo una risorsa e lanciamo la migrate per allineare il db:</p>
<p><code><br />
marco@d9400:~/Rails$ cd Blog/<br />
marco@d9400:~/Rails/Blog$ script/generate nifty_scaffold post title:string body:text</p>
<p>      exists  app/models<br />
      create  app/models/post.rb<br />
      exists  db/migrate<br />
      create  db/migrate/20100319175545_create_posts.rb<br />
      exists  test/unit<br />
      create  test/unit/post_test.rb<br />
      exists  test/fixtures<br />
      create  test/fixtures/posts.yml<br />
      exists  app/controllers<br />
      create  app/controllers/posts_controller.rb<br />
      exists  app/helpers<br />
      create  app/helpers/posts_helper.rb<br />
      create  app/views/posts<br />
      create  app/views/posts/index.html.erb<br />
      create  app/views/posts/show.html.erb<br />
      create  app/views/posts/new.html.erb<br />
      create  app/views/posts/edit.html.erb<br />
      create  app/views/posts/_post.html.erb<br />
      create  app/views/posts/_fields.html.erb<br />
       route  map.resources :posts<br />
      exists  test/functional<br />
      create  test/functional/posts_controller_test.rb</p>
<p>marco@d9400:~/Rails/Blog$ rake db:migrate</p>
<p>(in /home/marco/Rails/Blog)<br />
==  CreatePosts: migrating ====================================================<br />
-- create_table(:posts)<br />
   -> 0.0016s<br />
==  CreatePosts: migrated (0.0017s) ===========================================<br />
</code></p>
<p><a href="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd2.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd2-300x293.png" alt="Add post resource" title="Add post resource" width="300" height="293" class="aligncenter size-medium wp-image-465" /></a></p>
<p>I controllers e le viste usano messaggi localizzati, per questo motivo i nomi delle risorse devono essere aggiunti all&#8217;interno degli yaml. Dalla versione 0.3.2.3 nifty_scafold lo fa per voi, dovete pensare solo alla traduzione. Tenete presente inoltre, che se per esempio distruggete una risorsa deve essere rimossa dagli yaml manualmente.</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">#config/locales/en.yml
en:
  activerecord:
    models: &amp;models
      post: &quot;Post&quot;
      posts: &quot;Posts&quot;
    #DO NOT REMOVE MODELS
    attributes: &amp;attributes
      title: &quot;Title&quot;
      body: &quot;Body&quot;
    #DO NOT REMOVE ATTRIBUTES
&nbsp;
  formtastic:
    titles:
    labels:
    hints:
      post:
        title: &quot;Choose a good title for your article&quot;
        body: &quot;Choose a good body for your article&quot;
    #DO NOT REMOVE HINTS
    actions: &amp;actions
      create: &quot;Create my {{model}}&quot;
      update: &quot;Save changes&quot;
  &lt;&lt;: *models
  &lt;&lt;: *attributes
  &lt;&lt;: *actions
&nbsp;
#config/locales/it.yml
it:
  activerecord:
    models: &amp;models
      post: &quot;Messaggio&quot;
      posts: &quot;Messaggi&quot;
    #DO NOT REMOVE MODELS
    attributes: &amp;attributes
      title: &quot;Titolo&quot;
      body: &quot;Corpo&quot;
    #DO NOT REMOVE ATTRIBUTES
&nbsp;
  formtastic:
    titles:
    labels:
    hints:
      post:
        title: &quot;Choose a good title for your article&quot;
        body: &quot;Choose a good body for your article&quot;
    #DO NOT REMOVE HINTS
    actions: &amp;actions
      create: &quot;Crea {{model}}&quot;
      update: &quot;Salva le modifiche&quot;
  #DO NOT REMOVE FORMTASTIC
  &lt;&lt;: *models
  &lt;&lt;: *attributes
  &lt;&lt;: *actions</pre></div></div>

<p>Ho usato dei commenti del tipo: <em>#DO NOT REMOVE ecc.</em> per posizionare i nomi delle risorse o degli attributi nelle posizioni corrette, per cui è necessario non rimuoverli.</p>
<p><a href="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd3.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/03/Blog_marcomd3-300x156.png" alt="Messaggio creato con successo" title="Messaggio creato con successo" width="300" height="156" class="aligncenter size-medium wp-image-469" /></a></p>
<p>Ho da poco iniziato a testare la versione haml e sass per cui potrebbero esserci ancora dei bachi.<br />
Spero che questo articolo possa essere stato utile, buon divertimento!</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2010/03/creare-scheletri-di-applicazioni-con-generatori-e-modelli/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

