<?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; Ruby on Rails</title>
	<atom:link href="http://mastrodonato.info/index.php/category/ror/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, 08 Sep 2010 14:27: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>Pluralizzare in tutte le lingue</title>
		<link>http://mastrodonato.info/index.php/2010/09/pluralizzare-in-tutte-le-lingue/</link>
		<comments>http://mastrodonato.info/index.php/2010/09/pluralizzare-in-tutte-le-lingue/#comments</comments>
		<pubDate>Wed, 08 Sep 2010 10:33:46 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[I18n]]></category>
		<category><![CDATA[Internazionalizzazione]]></category>
		<category><![CDATA[Pluralize]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=612</guid>
		<description><![CDATA[In questo articolo mostrerò come utilizzare quel comodo helper di rails, pluralize, con tutte le lingue mediante l&#8217;utilizzo di I18n. Diamo per scontato che l&#8217;applicazione sia internazionalizzata, quindi modelli e campi presenti nei file yaml di configurazione. Ora dobbiamo aggiungere i termini per i quali vogliamo internazionalizzare la pluralità: modelli, attributi ecc. activerecord: &#38;activerecord models: [...]]]></description>
			<content:encoded><![CDATA[<p>In questo articolo mostrerò come utilizzare quel comodo helper di rails, <strong>pluralize</strong>, con tutte le lingue mediante l&#8217;utilizzo di I18n.</p>
<p>Diamo per scontato che l&#8217;applicazione sia internazionalizzata, quindi modelli e campi presenti nei file yaml di configurazione.<br />
Ora dobbiamo aggiungere i termini per i quali vogliamo internazionalizzare la pluralità: modelli, attributi ecc.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">activerecord: <span style="color:#006600; font-weight:bold;">&amp;</span>activerecord
    models: <span style="color:#006600; font-weight:bold;">&amp;</span>models
      user: <span style="color:#996600;">&quot;Utente&quot;</span>
      users: <span style="color:#996600;">&quot;Utenti&quot;</span>
      activity: <span style="color:#996600;">&quot;Impiego&quot;</span>
      activities: <span style="color:#996600;">&quot;Impieghi&quot;</span>
      task: <span style="color:#996600;">&quot;Incarico&quot;</span>
      tasks: <span style="color:#996600;">&quot;Incarichi&quot;</span>
      project: <span style="color:#996600;">&quot;Progetto&quot;</span>
      projects: <span style="color:#996600;">&quot;Progetti&quot;</span>
&nbsp;
    attributes: <span style="color:#006600; font-weight:bold;">&amp;</span>attributes
      activity:
        task: <span style="color:#996600;">&quot;Incarico&quot;</span>
        day: <span style="color:#996600;">&quot;Giorno&quot;</span>
        hours: <span style="color:#996600;">&quot;Ore&quot;</span>
        hour: <span style="color:#996600;">&quot;Ora&quot;</span>
        description: <span style="color:#996600;">&quot;Descrizione&quot;</span></pre></div></div>

<p>Ipotizziamo che il nostro modello &#8220;Incarico&#8221; sia relazionato col modello &#8220;Impiego&#8221; e che questo abbia un campo di nome &#8220;Ore&#8221;. Vogliamo creare una frase che descriva quanti impieghi sono presenti per tale incarico ed il totale delle ore.</p>
<p>In base ai dati nel database, vogliamo ottenere frasi del tipo:<br />
<em>Questo incarico ha <strong>1 impiego</strong> per un totale di <strong>1 ora</strong>.<br />
Questo incarico ha <strong>13 impieghi</strong> per un totale di <strong>0 ore</strong>.<br />
Questo incarico ha <strong>1 impiego</strong> per un totale di <strong>15 ore</strong>.<br />
</em></p>
<p>&#8230;e in tutte le lingue:<br />
<em><br />
This task has <strong>1 activity</strong> with an amount of <strong>15 hours</strong>.<br />
Diese Aufgabe haben <strong>1-Aktivität</strong> für eine Gesamtmenge von <strong>15 Stunden</strong>.<br />
Cette tâche a <strong>1 activité</strong> pour un total de <strong>15 heures</strong>.<br />
Esta tarea tiene <strong>1 actividad</strong> para un total de <strong>15 horas</strong>.<br />
このタスクは、 15時間の量1活性を持つ</em></p>
<p>Vogliamo evitare:<br />
<del>Questo incarico ha 1 impieghi per un totale di 1 ore</del><br />
<del>This task has 1 activities with an amount of 1 hours</del></p>
<p>Aggiungiamo la frase in ogni file yaml I18n:</p>

<div class="wp_syntax"><div class="code"><pre class="yaml" style="font-family:monospace;">  #it.yml
  task_activities: &quot;Questo incarico ha &lt;strong&gt;{{activities}}&lt;/strong&gt; per un totale di &lt;strong&gt;{{hours}}&lt;/strong&gt;.&quot;
  #en.yml
  task_activities: &quot;This task has &lt;strong&gt;{{activities}}&lt;/strong&gt; with an amount of &lt;strong&gt;{{hours}}&lt;/strong&gt;.&quot;
  #etc.</pre></div></div>

<p>Per fare ciò ci avvaliamo del comodo helper di rails, pluralize, opportunamente modificato per eliminare il numero dal risultato. Pluralizziamo il termine inglese da usare come chiave per I18n.</p>
<p>Creiamo nell&#8217;helper dell&#8217;applicazione:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ApplicationHelper
   <span style="color:#008000; font-style:italic;">#Pluralize without the number</span>
   <span style="color:#9966CC; font-weight:bold;">def</span> I18n_pluralize<span style="color:#006600; font-weight:bold;">&#40;</span>count, singular, plural = <span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
     <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#40;</span>count == <span style="color:#006666;">1</span> <span style="color:#006600; font-weight:bold;">||</span> count =~ <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#40;</span>\.0<span style="color:#006600; font-weight:bold;">+</span><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> ? singular : <span style="color:#006600; font-weight:bold;">&#40;</span>plural <span style="color:#006600; font-weight:bold;">||</span> singular.<span style="color:#9900CC;">pluralize</span><span style="color:#006600; font-weight:bold;">&#41;</span><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></pre></div></div>

<p>Nella view richiamiamo la frase I18n passando il numero di impieghi ed il totale di ore (per motivi descrittivi ho inserito nella view operazioni che però riterrei più opportuno posizionare nel controller):</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#006600; font-weight:bold;">&lt;%</span> tot_hours = <span style="color:#0066ff; font-weight:bold;">@activities</span>.<span style="color:#9900CC;">map</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>a<span style="color:#006600; font-weight:bold;">|</span> a.<span style="color:#9900CC;">hours</span><span style="color:#006600; font-weight:bold;">&#125;</span>.<span style="color:#9900CC;">inject</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>tot,h<span style="color:#006600; font-weight:bold;">|</span> tot<span style="color:#006600; font-weight:bold;">+</span>h<span style="color:#006600; font-weight:bold;">&#125;</span>  <span style="color:#006600; font-weight:bold;">||</span> <span style="color:#006666;">0</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> str_hour = t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;attributes.activity.#{I18n_pluralize(tot_hours,'hour')}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span> str_activity = t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;models.#{I18n_pluralize(@activities.size,'activity')}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= t <span style="color:#ff3333; font-weight:bold;">:task_activities</span>, <span style="color:#ff3333; font-weight:bold;">:activities</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{@activities.size} #{str_activity}&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:hours</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;#{@tot_hours} #{str_hour}&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Osserviamo questa riga alla moviola:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;attributes.activity.#{I18n_pluralize(tot_hours,'hour')}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>In base al numero di ore otteniamo la chiave I18n: hour o hours</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">chiave = I18n_pluralize<span style="color:#006600; font-weight:bold;">&#40;</span>tot_hours,<span style="color:#996600;">'hour'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>equivale</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;attributes.activity.#{chiave}&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>se tot_hours = 1 avremo:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;attributes.activity.hour&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>altrimenti:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;attributes.activity.hours&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Questo è il risultato:<br />
<a href="http://mastrodonato.info/wp-content/uploads/2010/09/Esempio_Pluralize_I18n.png"><img src="http://mastrodonato.info/wp-content/uploads/2010/09/Esempio_Pluralize_I18n-300x248.png" alt="Esempio Pluralize I18n" title="Esempio Pluralize I18n" width="300" height="248" class="aligncenter size-medium wp-image-614" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2010/09/pluralizzare-in-tutte-le-lingue/feed/</wfw:commentRss>
		<slash:comments>0</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>
		<item>
		<title>Gestire instradamenti errati</title>
		<link>http://mastrodonato.info/index.php/2009/11/gestire-instradamenti-errati/</link>
		<comments>http://mastrodonato.info/index.php/2009/11/gestire-instradamenti-errati/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 11:28:25 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Instradamenti]]></category>
		<category><![CDATA[Routes]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=89</guid>
		<description><![CDATA[La nostra applicazione può rispondere solo a determinate richieste, naturalmente a quelle che abbiamo previsto. Per tutte le altre viene generato un errore in base al tipo: Errori di instradamento Errori nel controller Errori di instradamento Piccola premessa: In un&#8217;applicazione tradizionale, equivaleva a richiamare una pagina inesistente ricevendo dal web server un codice 404. In [...]]]></description>
			<content:encoded><![CDATA[<p>La nostra applicazione può rispondere solo a determinate richieste, naturalmente a quelle che abbiamo previsto. Per tutte le altre viene generato un errore in base al tipo:</p>
<ul>
<li>Errori di instradamento</li>
<li>Errori nel controller</li>
</ul>
<h3>Errori di instradamento</h3>
<p><strong>Piccola premessa</strong>: In un&#8217;applicazione tradizionale, equivaleva a richiamare una pagina inesistente ricevendo dal web server un codice 404. In un framework con un pattern mvc, le richieste transitano attraverso un controller e questo ci permette di definirne il comportamento. Le richieste vengono smistate in base alle regole di instradamento ed in Rails, per convenzione, tutti i controllers possono essere indirizzati, in fondo al file routes.rb scopriamo il perchè:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config\routes.rb</span>
map.<span style="color:#9900CC;">connect</span> <span style="color:#996600;">':controller/:action/:id'</span>
map.<span style="color:#9900CC;">connect</span> <span style="color:#996600;">':controller/:action/:id.:format'</span></pre></div></div>

<p>Per fare in modo che solo determinati controllers possano essere instradati, basta eliminare quelle due righe in fondo al file che definiscono solo la forma della richiesta, lasciando solo gli instradamenti espliciti. Fine della premessa.</p>
<p>Se non esiste una rotta, viene generato un <strong>errore di instradamento</strong> (routing error) poichè Rails non sa cosa intendiamo richiamare. Per gestire questa evenienza, basta semplicemente aggiungere un&#8217;ultima strada prima di generare l&#8217;errore che percorrerà solo per informare che non ha trovato altre strade valide:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config\routes.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#aggiungiamo in ultima riga, in coda a tutto</span>
map.<span style="color:#9900CC;">catch_all</span> <span style="color:#996600;">&quot;*anything&quot;</span> , <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;home&quot;</span> , <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;unknown_request&quot;</span></pre></td></tr></table></div>

<p>Quest&#8217;ultima rotta cattura gli instradamenti non gestiti e li indirizza nel controller dove vogliamo gestirli, nell&#8217;esempio indirizza nel controller home col metodo unknown_request:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app\controllers\home_controller.rb</span>
<span style="color:#9966CC; font-weight:bold;">def</span> unknown_request
  respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:error</span><span style="color:#006600; font-weight:bold;">&#93;</span> = I18n.<span style="color:#9900CC;">t</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:unknown_request</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      redirect_to root_path
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span>  render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>:root <span style="color:#006600; font-weight:bold;">=&gt;</span> I18n.<span style="color:#9900CC;">t</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:unknown_request</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unprocessable_entity</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;">end</span></pre></div></div>

<h3>Errori nel controller</h3>
<p>Se il controller è stato indirizzato non è detto che la richiesta sia comunque corretta, ognuno infatti ha una serie di operazioni che devono essere previste e devono poter operare sulla risorsa quindi gestire le eccezioni sollevate dal database.</p>
<p>Dalla versione 2.0 di rails, possiamo utilizzare rescue_from nel controller:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app\controllers\my_controller.rb</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> MyController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  rescue_from <span style="color:#6666ff; font-weight:bold;">ActionController::UnknownAction</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:action_not_found</span>
  rescue_from <span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:record_not_found</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> action_not_found
    render <span style="color:#996600;">'shared/action_not_found'</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">404</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> record_not_found
    render <span style="color:#996600;">'shared/record_not_found'</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">404</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Queste sono le viste:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">#app\views\sharedaction _not_found.html.erb
&lt;h2&gt;Sorry, the action doesn't exists!&lt;/h2&gt;
&nbsp;
#app\views\sharedrecord_not_found.html.erb
&lt;h2&gt;Sorry, the record doesn't exists!&lt;/h2&gt;</pre></div></div>

<p>Usare una proc come parametro a rescue_from potrebbe essere un&#8217;alternativa più concisa:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app\controllers\my_controller.rb</span>
rescue_from<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::RecordNotFound</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> render <span style="color:#996600;">'shared/record_not_found'</span>, <span style="color:#ff3333; font-weight:bold;">:layout</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">404</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/11/gestire-instradamenti-errati/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FreeTTS, una libreria java in jRuby on Rails</title>
		<link>http://mastrodonato.info/index.php/2009/08/freetts-una-libreria-java-in-jruby-on-rails/</link>
		<comments>http://mastrodonato.info/index.php/2009/08/freetts-una-libreria-java-in-jruby-on-rails/#comments</comments>
		<pubDate>Mon, 03 Aug 2009 15:08:19 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[JRuby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[FreeTTS]]></category>
		<category><![CDATA[jar]]></category>
		<category><![CDATA[jRuby]]></category>
		<category><![CDATA[Text to speech]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=75</guid>
		<description><![CDATA[In questo articolo vedremo come utilizzare le classi java contenute in un file JAR Per questo scopo andremo ad utilizzare una simpatica libreria opensource sviluppata dalla Carnegie Mellon University: FreeTTS. L&#8217;acronimo TTS significa Text To speech tradotto: &#8220;da testo a voce&#8221;, permette infatti di trasformare un testo in formato audio. Noi la utilizzeremo in un [...]]]></description>
			<content:encoded><![CDATA[<p>In questo articolo vedremo come utilizzare le classi java contenute in un file JAR<br />
Per questo scopo andremo ad utilizzare una simpatica libreria opensource sviluppata dalla Carnegie Mellon University: <a href="http://freetts.sourceforge.net/">FreeTTS</a>.<br />
L&#8217;acronimo TTS significa Text To speech tradotto: &#8220;da testo a voce&#8221;, permette infatti di trasformare un testo in formato audio. Noi la utilizzeremo in un progetto jRuby on Rails per farci leggere il testo che inseriremo nel db.</p>
<p>Nel <a href="http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/">precedente articolo</a>, abbiamo visto come configurare l&#8217;ambiente, partiamo quindi creando la nuova applicazione:</p>
<p><code><br />
C:>rails ProvaFreeTTS<br />
</code></p>
<p>Nella cartella lib (dalla root) creiamo una sotto cartella freetts, scarichiamo il file <a href="http://sourceforge.net/projects/freetts/files/">freetts-1.2.2-bin.zip</a>, scompattiamo il contenuto in una cartella temporanea, copiamo solamente il contenuto della cartella lib (files jar e jsapi) nella cartella appena creata: tua_applicazionelibfreetts.</p>
<p>Ora creiamo l&#8217;interfaccia per il jar:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#libfreetts.rb</span>
&nbsp;
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'freetts/freetts.jar'</span>
&nbsp;
import com.<span style="color:#9900CC;">sun</span>.<span style="color:#9900CC;">speech</span>.<span style="color:#9900CC;">freetts</span>.<span style="color:#9900CC;">Voice</span>
import com.<span style="color:#9900CC;">sun</span>.<span style="color:#9900CC;">speech</span>.<span style="color:#9900CC;">freetts</span>.<span style="color:#9900CC;">VoiceManager</span>
import com.<span style="color:#9900CC;">sun</span>.<span style="color:#9900CC;">speech</span>.<span style="color:#9900CC;">freetts</span>.<span style="color:#9900CC;">util</span>.<span style="color:#9900CC;">Utilities</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> FreeTTS
  <span style="color:#9966CC; font-weight:bold;">def</span> initialize
    <span style="color:#0066ff; font-weight:bold;">@voice</span> = VoiceManager.<span style="color:#9900CC;">getInstance</span>.<span style="color:#9900CC;">getVoice</span><span style="color:#006600; font-weight:bold;">&#40;</span>Utilities.<span style="color:#9900CC;">getProperty</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;voice16kName&quot;</span>,<span style="color:#996600;">&quot;kevin16&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0066ff; font-weight:bold;">@voice</span>.<span style="color:#9900CC;">allocate</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> speak<span style="color:#006600; font-weight:bold;">&#40;</span>txt=<span style="color:#0000FF; font-weight:bold;">nil</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">nil</span> <span style="color:#9966CC; font-weight:bold;">unless</span> txt
    <span style="color:#0066ff; font-weight:bold;">@voice</span>.<span style="color:#9900CC;">speak</span> txt
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Creiamo una semplicissima risorsa &#8220;<em>sentence</em>&#8221; con un solo campo &#8220;<em>body</em>&#8221;</p>
<p><code><br />
C:ProvaFreeTTS>jruby script/generate scaffold sentence body:text<br />
</code></p>
<p>Ora creiamo due nuove operazioni, come ho spiegato approfonditamente in un <a href="http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/">precedente articolo</a>.</p>
<p>Iniziamo dal controller aggiungendo in coda:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">  <span style="color:#008000; font-style:italic;">#appcontrollerssentences_controller.rb</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> speak
    <span style="color:#0066ff; font-weight:bold;">@sentence</span> = Sentence.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:sentence</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'freetts'</span>
    tts = FreeTTS.<span style="color:#9900CC;">new</span>
    tts.<span style="color:#9900CC;">speak</span> <span style="color:#0066ff; font-weight:bold;">@sentence</span>.<span style="color:#9900CC;">body</span>
    render <span style="color:#006600; font-weight:bold;">&#40;</span>@sentence.<span style="color:#9900CC;">new_record</span>? ? <span style="color:#ff3333; font-weight:bold;">:new</span> : <span style="color:#ff3333; font-weight:bold;">:edit</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> read
    <span style="color:#0066ff; font-weight:bold;">@sentence</span> = Sentence.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'freetts'</span>
    tts = FreeTTS.<span style="color:#9900CC;">new</span>
    tts.<span style="color:#9900CC;">speak</span> <span style="color:#0066ff; font-weight:bold;">@sentence</span>.<span style="color:#9900CC;">body</span>
    redirect_to <span style="color:#ff3333; font-weight:bold;">:back</span>
  <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Ora andiamo a modificare le viste.<br />
Creiamo un nuovo file, più precisamente un partial dove inseriremo il form dati per la nostra risorsa, in questo modo utilizzeremo lo stesso codice per tutte le operazioni:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#appviewssentence_sentence.html.erb
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@sentence<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_area</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">'Update'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
&lt;h2&gt;Preview&lt;/h2&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> form_for <span style="color:#0066ff; font-weight:bold;">@sentence</span>, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> speak_sentences_path, <span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:put</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_area</span> <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">'Speak'</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Aggiungiamo un form clone di quello creato dallo scaffold ma con l&#8217;action diversa, per richiamare l&#8217;operazione speak.</p>
<p>Ora modifichiamo le viste create in automatico:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#appviewssentencenew.html.erb
&nbsp;
&lt;h1&gt;New sentence&lt;/h1&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= render <span style="color:#0066ff; font-weight:bold;">@sentence</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Back'</span>, sentences_path <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
#appviewssentenceedit.html.erb
&nbsp;
&lt;h1&gt;Editing sentence&lt;/h1&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= render <span style="color:#0066ff; font-weight:bold;">@sentence</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Show'</span>, <span style="color:#0066ff; font-weight:bold;">@sentence</span> <span style="color:#006600; font-weight:bold;">%&gt;</span> |
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Back'</span>, sentences_path <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Infine modifichiamo la lista per richiamare la seconda delle operazioni implementate, read. Sostanzialmente cliccando sulla riga corrispondente leggerà il testo precedentemente memorizzato nel db.</p>
<p>Dobbiamo solamente aggiungere una riga ottenendo questa view:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#appviewssentenceindex.html.erb
&lt;h1&gt;Listing sentences&lt;/h1&gt;
&nbsp;
&lt;table&gt;
  &lt;tr&gt;
    &lt;th&gt;Body&lt;/th&gt;
  &lt;/tr&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#0066ff; font-weight:bold;">@sentences</span>.<span style="color:#9900CC;">each</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>sentence<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;tr&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>=h sentence.<span style="color:#9900CC;">body</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Read'</span>, read_sentence_path<span style="color:#006600; font-weight:bold;">&#40;</span>sentence<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Show'</span>, sentence <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Edit'</span>, edit_sentence_path<span style="color:#006600; font-weight:bold;">&#40;</span>sentence<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
    &lt;td&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'Destroy'</span>, sentence, <span style="color:#ff3333; font-weight:bold;">:confirm</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'Are you sure?'</span>, <span style="color:#ff3333; font-weight:bold;">:method</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:delete</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/td&gt;
  &lt;/tr&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/table&gt;
&nbsp;
&lt;br /&gt;
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to <span style="color:#996600;">'New sentence'</span>, new_sentence_path <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Infine andiamo a modificare il routing:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#configroutes.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#sostituiamo map.resources :sentences con</span>
map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:sentences</span>, <span style="color:#ff3333; font-weight:bold;">:member</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:read</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:get</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#ff3333; font-weight:bold;">:collection</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:speak</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:put</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
&nbsp;
<span style="color:#008000; font-style:italic;">#aggiungiamo</span>
map.<span style="color:#9900CC;">root</span> <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;sentences&quot;</span></pre></div></div>

<p>Configuriamo il file database.yml, creiamo il db e le tabelle con rake e facciamo partire glassfish come spiegato <a href="http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/">qua</a> (cerca database.yml)</p>
<p>Come abbiamo visto, utilizzare librerie java con jruby on rails è semplicissimo, siamo pronti per far pronunciare al nostro server tutte le frasi più sporcaccione!</p>
<p>Il <strong>progetto completo</strong> potete scaricarlo <a href="http://www.gigasize.com/get.php?d=p4wsyz5z29c">qua</a>.</p>
<p>Per ulteriori approfondimenti c&#8217;è anche questo <a href="http://ruby.html.it/articoli/leggi/3110/jruby-java-e-ruby-insieme/1/">articolo</a>, mentre <a href="http://java.html.it/articoli/leggi/3098/text-to-speech-con-java-e-freetts/1/">questo </a>tratta la libreria FreeTTS in un progetto Java.</p>
<p>Buona continuazione.</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/08/freetts-una-libreria-java-in-jruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Configurare un ambiente per jRuby on Rails</title>
		<link>http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/</link>
		<comments>http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/#comments</comments>
		<pubDate>Wed, 29 Jul 2009 16:52:31 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[Glassfish]]></category>
		<category><![CDATA[jdbc]]></category>
		<category><![CDATA[jRuby]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=54</guid>
		<description><![CDATA[Nei prossimi articoli utilizzerò jruby 1.3.1 con Rails 2.3.3 e ne approfitto per scrivere due righe su come configurare il sistema. A proposito, il sistema utilizzato è un windows xp virtualizzato, java sdk 6, glassfish come application server e javadb. Scarichiamo e installiamo l&#8217;sdk java, siamo alla versione 6 update 14. C&#8217;è anche il download [...]]]></description>
			<content:encoded><![CDATA[<p>Nei prossimi articoli utilizzerò jruby 1.3.1 con Rails 2.3.3 e ne approfitto per scrivere due righe su come configurare il sistema.<br />
A proposito, il sistema utilizzato è un windows xp virtualizzato, java sdk 6, glassfish come application server e javadb.</p>
<p>Scarichiamo e installiamo l&#8217;<a href="http://java.sun.com/javase/downloads/index.jsp" target="_blank">sdk java</a>, siamo alla versione 6 update 14.</p>
<p>C&#8217;è anche il download dell&#8217;sdk con netbeans, un ottimo IDE per gestire progetti ruby on rails. Potrebbe essere un occasione per provarlo. Il pacchetto comprende anche l&#8217;application server glassfish V3.</p>
<p>Ora scarichiamo e installiamo <a href="http://www.jruby.org/" target="_blank">jRuby</a>, siamo alla versione 1.3.1.</p>
<p>L&#8217;installazione è semplicissima: scompattiamo lo zip in un percorso del tipo C:\ruby\jruby-131</p>
<p>Andiamo ad impostare le variabili d&#8217;ambiente, tasto destro su &#8220;Risorse del computer&#8221; sul desktop -> &#8220;Proprietà&#8221; -> seleziona il tab &#8220;Avanzate&#8221; -> click su &#8220;variabili d&#8217;ambiente&#8221;.</p>
<ul>
<li>Aggiungiamo alla variabile <strong>PATH</strong> il percorso della cartella bin dove abbiamo installato jruby, per esempio C:\ruby\jruby-131\bin.</li>
<li>Impostiamo la variabile d&#8217;ambiente <strong>JAVA_HOME</strong>.  Nella zona &#8220;variabili di sistema&#8221; -> &#8220;nuovo&#8221; -> come nome &#8220;JAVA_HOME&#8221; come valore il percorso di installazione dell&#8217;sdk per esempio C:\Programmi\Java\jdk1.6.0_14</li>
</ul>
<p>Non è necessario riavviare il sistema operativo, testiamo il risultato:<br />
<code><br />
C:\><strong>jruby -v</strong><br />
jruby 1.3.1 (ruby 1.8.6p287) (2009-06-15 2fd6c3d) (Java HotSpot(TM) Client VM 1.6.0_14) [x86-java]<br />
</code></p>
<p>Eureka! Ora abbiamo jruby, jirb ed il solito gem. <strong>Se nel sistema è presente anche ruby classico è necessario specificare jruby -S</strong> per richiamare gem, utilizziamolo subito per installare rails 2.3.3:<br />
<code><br />
C:\><strong>jruby -S gem install rails</strong><br />
Successfully installed activesupport-2.3.3<br />
Successfully installed activerecord-2.3.3<br />
Successfully installed actionpack-2.3.3<br />
Successfully installed actionmailer-2.3.3<br />
Successfully installed activeresource-2.3.3<br />
Successfully installed rails-2.3.3<br />
6 gems installed<br />
Installing ri documentation for activesupport-2.3.3...<br />
Installing ri documentation for activerecord-2.3.3...<br />
Installing ri documentation for actionpack-2.3.3...<br />
Installing ri documentation for actionmailer-2.3.3...<br />
Installing ri documentation for activeresource-2.3.3...<br />
Installing ri documentation for rails-2.3.3...<br />
Installing RDoc documentation for activesupport-2.3.3...<br />
Installing RDoc documentation for activerecord-2.3.3...<br />
Installing RDoc documentation for actionpack-2.3.3...<br />
Installing RDoc documentation for actionmailer-2.3.3...<br />
Installing RDoc documentation for activeresource-2.3.3...<br />
Installing RDoc documentation for rails-2.3.3...<br />
</code></p>
<p>Installiamo l&#8217;adapter jdbc per collegarci a java DB (da ora ometto jruby -S, nel mio sistema non è necessario):<br />
<code><br />
C:\><strong>gem install activerecord-jdbcderby-adapter</strong><br />
Successfully installed activerecord-jdbc-adapter-0.9.1<br />
Successfully installed jdbc-derby-10.4.2.0<br />
Successfully installed activerecord-jdbcderby-adapter-0.9.1<br />
3 gems installed<br />
Installing ri documentation for activerecord-jdbc-adapter-0.9.1...<br />
Installing ri documentation for jdbc-derby-10.4.2.0...<br />
Installing ri documentation for activerecord-jdbcderby-adapter-0.9.1...<br />
Installing RDoc documentation for activerecord-jdbc-adapter-0.9.1...<br />
Installing RDoc documentation for jdbc-derby-10.4.2.0...<br />
Installing RDoc documentation for activerecord-jdbcderby-adapter-0.9.1...<br />
</code></p>
<p>Poi installiamo la gemma per glassfish:<br />
<code><br />
C:\><strong>gem install glassfish</strong><br />
Successfully installed rack-1.0.0<br />
Successfully installed glassfish-0.9.5-universal-java<br />
2 gems installed<br />
Installing ri documentation for rack-1.0.0...<br />
Installing ri documentation for glassfish-0.9.5-universal-java...<br />
Installing RDoc documentation for rack-1.0.0...<br />
Installing RDoc documentation for glassfish-0.9.5-universal-java...<br />
</code></p>
<p>Siamo pronti, creiamo un nuovo progetto:<br />
<code><br />
C:\>rails ProvaArticolo<br />
</code></p>
<p>&#8230;nel file di configurazione dei db utilizziamo:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># config/database.yml</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># JavaDB Setup</span>
<span style="color:#008000; font-style:italic;">#</span>
<span style="color:#008000; font-style:italic;"># You may need to copy derby.jar into</span>
<span style="color:#008000; font-style:italic;">#  TODO: location C:\ruby\jruby-131\lib</span>
<span style="color:#008000; font-style:italic;"># With Java SE 6 and later this is not necessary.</span>
development:
  adapter: derby
  database: db<span style="color:#006600; font-weight:bold;">/</span>development.<span style="color:#9900CC;">db</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Warning: The database defined as 'test' will be erased and</span>
<span style="color:#008000; font-style:italic;"># re-generated from your development database when you run 'rake'.</span>
<span style="color:#008000; font-style:italic;"># Do not set this db to the same as development or production.</span>
test:
  adapter: derby
  database: db<span style="color:#006600; font-weight:bold;">/</span>test.<span style="color:#9900CC;">db</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Warning: The database defined as 'test' will be erased and</span>
<span style="color:#008000; font-style:italic;"># re-generated from your development database when you run 'rake'.</span>
<span style="color:#008000; font-style:italic;"># Do not set this db to the same as development or production.</span>
production:
  adapter: derby
  database: db<span style="color:#006600; font-weight:bold;">/</span>production.<span style="color:#9900CC;">db</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Warning: The database defined as 'test' will be erased and</span>
<span style="color:#008000; font-style:italic;"># re-generated from your development database when you run 'rake'.</span>
<span style="color:#008000; font-style:italic;"># Do not set this db to the same as development or production.</span></pre></div></div>

<p>Facciamo partire il server, dal prompt dentro la nostra applicazione:<br />
<code><br />
C:\ProvaArticolo><strong>glassfish</strong><br />
Starting GlassFish server at: 127.0.0.1:3000 in development environment...<br />
Writing log messages to: C:/ProvaArticolo/log/development.log.<br />
Press Ctrl+C to stop.<br />
</code></p>
<p>All&#8217;indirizzo http://localhost:3000/ dovremmo vedere la pagina di benvenuto.<br />
Ora fermiamo il server con Ctrl+C e creiamo il db in ambiente sviluppo:</p>
<p><code>C:\ProvaArticolo><strong>rake db:create</strong><br />
(in C:/ProvaArticolo)<br />
db/development.db already exists</code></p>
<p>Creiamo qualcosa all&#8217;interno del database, iniziamo con la risorsa di rails:<br />
<code><br />
C:\ProvaArticolo>jruby script/generate scaffold article name:string body:text<br />
      create  app/models/<br />
      exists  app/controllers/<br />
      exists  app/helpers/<br />
      exists  app/views/articles<br />
      create  app/views/layouts/<br />
      create  test/functional/<br />
      create  test/unit/<br />
      create  test/unit/helpers/<br />
      create  public/stylesheets/<br />
      create  app/views/articles/index.html.erb<br />
      create  app/views/articles/show.html.erb<br />
      create  app/views/articles/new.html.erb<br />
      create  app/views/articles/edit.html.erb<br />
      create  app/views/layouts/articles.html.erb<br />
      create  public/stylesheets/scaffold.css<br />
      create  app/controllers/articles_controller.rb<br />
      create  test/functional/articles_controller_test.rb<br />
      create  app/helpers/articles_helper.rb<br />
      create  test/unit/helpers/articles_helper_test.rb<br />
       route  map.resources :articles<br />
  dependency  model<br />
      exists    app/models/<br />
      exists    test/unit/<br />
      create    test/fixtures/<br />
      create    app/models/article.rb<br />
      create    test/unit/article_test.rb<br />
      create    test/fixtures/articles.yml<br />
      exists    db/migrate<br />
      create    db/migrate/20090729171105_create_articles.rb<br />
</code></p>
<p>e creiamo la tabella nel db con la migrate:<br />
<code><br />
<strong>C:\ProvaArticolo>rake db:migrate</strong><br />
(in C:/ProvaArticolo)<br />
==  CreateArticles: migrating =================================================<br />
-- create_table(:articles)<br />
   -> 0.0700s<br />
   -> 0 rows<br />
==  CreateArticles: migrated (0.0700s) ========================================<br />
</code></p>
<p>Infine eliminiamo il file index.html da dentro la cartella public e creiamo la route iniziale:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config\routes.rb</span>
map.<span style="color:#9900CC;">root</span> <span style="color:#ff3333; font-weight:bold;">:controller</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;articles&quot;</span></pre></div></div>

<p>Riavviamo il server e sempre all&#8217;indirizzo http://localhost:3000/ questa volta dovremmo vedere la lista degli articoli.</p>
<p>Se non mi sono dimenticato qualcosa, jruby on rails è pronto, buon divertimento!</p>
]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/07/configurare-un-ambiente-per-jruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Aggiungere un operazione ad una risorsa in un sistema RESTful</title>
		<link>http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/</link>
		<comments>http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 10:31:00 +0000</pubDate>
		<dc:creator>Marco Mastrodonato</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[CRUD]]></category>
		<category><![CDATA[RESTful]]></category>
		<category><![CDATA[risorsa]]></category>

		<guid isPermaLink="false">http://mastrodonato.info/?p=31</guid>
		<description><![CDATA[Creare un applicazione RESTful con rails è molto semplice: C:>rails MyApp In un sistema REST, l&#8217;applicazione è composta da risorse web aventi un insieme vincolato di operazioni. Con lo scaffold generiamo una risorsa e le quattro basilari funzioni per gestirla, denominate CRUD (Create, Read, Update, Delete) C:>script/generate scaffold assets name:string Abbiamo creato la base da [...]]]></description>
			<content:encoded><![CDATA[<p>Creare un applicazione <a href="http://it.wikipedia.org/wiki/Representational_State_Transfer" target="_blank">RESTful</a> con rails è molto semplice:</p>
<p><code>C:>rails MyApp</code></p>
<p>In un sistema REST, l&#8217;applicazione è composta da risorse web aventi un insieme vincolato di operazioni. Con lo scaffold generiamo una risorsa e le quattro basilari funzioni per gestirla, denominate <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" target="_blank">CRUD</a> (Create, Read, Update, Delete)</p>
<p><code>C:>script/generate scaffold assets name:string</code></p>
<p>Abbiamo creato la base da cui partire per testare questo breve articolo.</p>
<p>Supponiamo di voler aggiungere una nuova operazione alla nostra risorsa risorsa <em>Asset</em>, per esempio vogliamo gestire la copia. Iniziamo creando il nuovo indirizzamento modificando quanto creato dallo scaffold:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config/routes.rb</span>
map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:assets</span>, <span style="color:#ff3333; font-weight:bold;">:member</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:copy</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:get</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Testiamo l&#8217;esito:<br />
<code>C:>rake routes</code></p>
<p>verificando che ci sia il nuovo percorso:<br />
<code><br />
copy_asset GET   /assets/:id/copy(.:format)   {:controller=>"assets", :action=>"copy"}<br />
</code></p>
<p>La copia è un&#8217;operazione utile in molti contesti in quanto permette di effettuare l&#8217;inserimento di una nuova risorsa partendo da una già esistente.<br />
Traduciamo in codice e andiamo a creare la nuova operazione nel controller:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app/controllers/assets_controller.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># GET /assets/copy</span>
<span style="color:#008000; font-style:italic;"># GET /assets/copy.xml</span>
<span style="color:#9966CC; font-weight:bold;">def</span> copy
  <span style="color:#0066ff; font-weight:bold;">@asset</span> = asset.<span style="color:#9900CC;">new</span>
  <span style="color:#0066ff; font-weight:bold;">@asset</span>.<span style="color:#9900CC;">attributes</span> = Asset.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:id</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">attributes</span>
&nbsp;
  respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:new</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@asset</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;">end</span></pre></div></div>

<p>Eventualmente si può creare la view, dipende dall&#8217;operazione che si deve gestire. In questo esempio si sfrutta la view dell&#8217;operazione new in quanto sono molto simili e richiamano entrambe la :create.</p>
<p>Creare o non creare una nuova azione sulla risorsa è pura filosofia, tralasciando la questione vediamo come indirizzare una nuova create, ci riferiamo ad una collezione di risorse:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#config/routes.rb</span>
map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:assets</span>, <span style="color:#ff3333; font-weight:bold;">:member</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:copy</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:get</span> <span style="color:#006600; font-weight:bold;">&#125;</span>, <span style="color:#ff3333; font-weight:bold;">:collection</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:createcp</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:put</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>ho utilizzato una collezione perchè in questo caso non serve un riferimento ad una risorsa esistente, se avessi usato :member il path per la generazione dell&#8217;uri lo avrebbe richiesto.</p>
<p>se testiamo nuovamente le routes dovrebbe aggiungersi questa nuova:<br />
<code><br />
createcp_asset PUT   /assets/createcp(.:format)   {:controller=>"assets", :action=>"createcp"}<br />
</code></p>
<p>creiamo quindi la nuova view e il form con la uri della nuova azione specificando il metodo (per precisione ho indicato put ma equivale ad utilizzare un post) :</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#app/views/assets/copy.html.erb
&nbsp;
<span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@asset, <span style="color:#ff3333; font-weight:bold;">:url</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> createcp_assets_path, <span style="color:#ff3333; font-weight:bold;">:method</span><span style="color:#006600; font-weight:bold;">=&gt;</span>:put <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:name</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
  &lt;p&gt;
    <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:create</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  &lt;/p&gt;
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>e la nuova azione nel controller:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;">#app/controllers/assets_controller.rb</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># POST /assets/createcp</span>
<span style="color:#008000; font-style:italic;"># POST /assets/createcp.xml</span>
<span style="color:#9966CC; font-weight:bold;">def</span> createcp
  <span style="color:#0066ff; font-weight:bold;">@asset</span> = Asset.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:asset</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
  <span style="color:#008000; font-style:italic;">#do something</span>
&nbsp;
  respond_to <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>format<span style="color:#006600; font-weight:bold;">|</span>
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@asset</span>.<span style="color:#9900CC;">save</span>
      flash<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:notice</span><span style="color:#006600; font-weight:bold;">&#93;</span> = I18n.<span style="color:#9900CC;">t</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:created</span>, <span style="color:#ff3333; font-weight:bold;">:model</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> I18n.<span style="color:#9900CC;">t</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'model.asset'</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> redirect_to assets_path <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@asset</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:created</span>, <span style="color:#ff3333; font-weight:bold;">:location</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@asset</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">html</span> <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:action</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;copy&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#CC0066; font-weight:bold;">format</span>.<span style="color:#9900CC;">xml</span>  <span style="color:#006600; font-weight:bold;">&#123;</span> render <span style="color:#ff3333; font-weight:bold;">:xml</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0066ff; font-weight:bold;">@asset</span>.<span style="color:#9900CC;">errors</span>, <span style="color:#ff3333; font-weight:bold;">:status</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:unprocessable_entity</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;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Per richiamare la nuova operazione dalla index:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">#app/views/assets/index.html.erb
<span style="color:#006600; font-weight:bold;">&lt;%</span>= link_to t<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:copy</span><span style="color:#006600; font-weight:bold;">&#41;</span>, copy_asset_path<span style="color:#006600; font-weight:bold;">&#40;</span>asset<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Infine ed eventualmente, queste sono le parole utilizzate dall&#8217;esempio per l&#8217;internazionalizzazione:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">create: <span style="color:#996600;">&quot;Crea&quot;</span>
copy: <span style="color:#996600;">&quot;Copia&quot;</span>
created: <span style="color:#996600;">&quot;L'oggetto {{model}} è stato creato correttamente.&quot;</span>
name: <span style="color:#996600;">&quot;Nome&quot;</span>
model:
  asset: <span style="color:#996600;">&quot;Allegato&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://mastrodonato.info/index.php/2009/06/aggiungere-un-operazione-ad-una-risorsa-in-un-sistema-restful/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
