<?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>Blog Patrick Espake &#187; rb</title>
	<atom:link href="http://blog.patrickespake.com/tag/rb/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.patrickespake.com</link>
	<description>O conhecimento evolui quando é compartilhado!</description>
	<lastBuildDate>Fri, 03 Sep 2010 04:24:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1-alpha</generator>
		<item>
		<title>Ruby on Rails plugin tableless_model</title>
		<link>http://blog.patrickespake.com/2009/08/11/ruby-on-rails-plugin-tableless_model/</link>
		<comments>http://blog.patrickespake.com/2009/08/11/ruby-on-rails-plugin-tableless_model/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 17:15:04 +0000</pubDate>
		<dc:creator>patrickespake</dc:creator>
				<category><![CDATA[Plugins]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[:binary]]></category>
		<category><![CDATA[:boolean]]></category>
		<category><![CDATA[:datetime]]></category>
		<category><![CDATA[:timestamp]]></category>
		<category><![CDATA[activerecord]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[browser]]></category>
		<category><![CDATA[código fonte]]></category>
		<category><![CDATA[column]]></category>
		<category><![CDATA[column_name:sql_type]]></category>
		<category><![CDATA[contact]]></category>
		<category><![CDATA[contacts]]></category>
		<category><![CDATA[contato]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[Date]]></category>
		<category><![CDATA[decimal]]></category>
		<category><![CDATA[destroy]]></category>
		<category><![CDATA[documentação]]></category>
		<category><![CDATA[erb]]></category>
		<category><![CDATA[exemplo]]></category>
		<category><![CDATA[exemplos]]></category>
		<category><![CDATA[fixture]]></category>
		<category><![CDATA[float]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[formulários]]></category>
		<category><![CDATA[form_for]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[generators]]></category>
		<category><![CDATA[geradores]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[instalação]]></category>
		<category><![CDATA[install]]></category>
		<category><![CDATA[integer]]></category>
		<category><![CDATA[label]]></category>
		<category><![CDATA[navegador]]></category>
		<category><![CDATA[new]]></category>
		<category><![CDATA[patrickespake]]></category>
		<category><![CDATA[Plugin]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rb]]></category>
		<category><![CDATA[recursos]]></category>
		<category><![CDATA[resources]]></category>
		<category><![CDATA[restful]]></category>
		<category><![CDATA[rotas]]></category>
		<category><![CDATA[routes]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[sem banco de dados]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[submit]]></category>
		<category><![CDATA[TablelessModel]]></category>
		<category><![CDATA[tableless_model]]></category>
		<category><![CDATA[tableless_scaffold]]></category>
		<category><![CDATA[test]]></category>
		<category><![CDATA[text]]></category>
		<category><![CDATA[text_area]]></category>
		<category><![CDATA[text_field]]></category>
		<category><![CDATA[Time]]></category>
		<category><![CDATA[validações]]></category>
		<category><![CDATA[validates_format_of]]></category>
		<category><![CDATA[validates_length_of]]></category>
		<category><![CDATA[validates_presence_of]]></category>
		<category><![CDATA[validations]]></category>

		<guid isPermaLink="false">http://blog.patrickespake.com/?p=934</guid>
		<description><![CDATA[tableless_model Ruby on Rails model sem banco de dados, com todo o poder do ActiveRecord e validações. A principal utilidade é usar em formulários que não precisam de banco de dados, mas precisam das validações do ActiveRecord, form_for e rotas RESTful. Por exemplo formulário de contato. Instalação ./script/plugin install git://github.com/patrickespake/tableless_model.git Geradores Gerador tableless_model Cria o [...]]]></description>
			<content:encoded><![CDATA[<p><strong>tableless_model</strong></p>
<p>Ruby on Rails model sem banco de dados, com todo o poder do ActiveRecord e validações.</p>
<p>A principal utilidade é usar em formulários que não precisam de banco de dados, mas precisam das validações do ActiveRecord, form_for e rotas RESTful. Por exemplo formulário de contato.</p>
<p><strong>Instalação</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>plugin <span style="color: #c20cb9; font-weight: bold;">install</span> git:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>patrickespake<span style="color: #000000; font-weight: bold;">/</span>tableless_model.git</pre></div></div>

<p><strong>Geradores</strong></p>
<p><strong>Gerador tableless_model</strong></p>
<p>Cria o esboço de um novo tableless model. Passe o nome do tableless model, em CamelCased ou under_scored, e uma lista de pares de atributos opcionais como argumentos.</p>
<p>Os atributos pares opcionais são column_name:sql_type como argumentos, especificando os atributos do tableless model.</p>
<p>Você não tem que pensar em cada atributos lá na frente, mas ajuda a esboçar um pouco para que você possa começar a trabalhar com o tableless model imediatamente.</p>
<p>Isto gera uma classe tableless model em app/models, um teste de unidade em test/unit e uma fixture em test/fixtures/singular_name.yml</p>
<p><strong>Exemplos:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>generate tableless_model contact</pre></div></div>

<p>Cria o tableless model Contact, test e fixture:</p>
<ul>
<li>Model: app/models/contact.rb</li>
<li>Test: test/unit/contact_test.rb</li>
<li>Fixtures: test/fixtures/contacts.yml</li>
</ul>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>generate tableless_model invite name:string body:text email:string</pre></div></div>

<p>Cria o tableless model Invite com a string name, text body e string email.</p>
<p><strong>Gerador tableless_scaffold</strong></p>
<p>Tableless scaffolds é composto por vários recursos, a partir do model, controller e views, juntamente com um conjunto completo de testes. O recurso está pronto para ser usado como um ponto de partida, sendo RESTful, orientado para o pedido.</p>
<p>Passe o nome do tableless model (na forma singular), em CamelCased ou under_scored, como primeiro argumento, e uma lista de pares de atributos opcionais.</p>
<p>Os pares de atributos opcionais são column_name:sql_type, especificando os atributos do modelo.</p>
<p>Você não tem que pensar em cada atributo lá na frente, mas ajuda esboçar um pouco para que você possa começar a trabalhar com os recursos imediatamente.</p>
<p>Por exemplo &#8216;tableless_scaffold contact name:string telephone:string firm:string email:string message:text&#8217; dá-lhe um tableless model com cinco atributos, um controlador para lidar com a criação, formulário para criar os contatos e routas declaradas em config/routes.rb.</p>
<p>Se você quiser remover todos os arquivos gerados, execute:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">script<span style="color: #000000; font-weight: bold;">/</span>destroy scaffold ModelName</pre></div></div>

<p><strong>Exemplos:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>generate tableless_scaffold invite</pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>generate tableless_scaffold contact name:string telephone:string firm:string email:string message:text</pre></div></div>

<p><strong>Tipos de colunas disponíveis nos tableless_models</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ModelName <span style="color:#006600; font-weight:bold;">&lt;</span> TablelessModel
  column <span style="color:#ff3333; font-weight:bold;">:column_name1</span>, <span style="color:#ff3333; font-weight:bold;">:column_type</span>
  column <span style="color:#ff3333; font-weight:bold;">:column_name2</span>, <span style="color:#ff3333; font-weight:bold;">:column_type</span>
  column <span style="color:#ff3333; font-weight:bold;">:column_name3</span>, <span style="color:#ff3333; font-weight:bold;">:column_type</span>
  column <span style="color:#ff3333; font-weight:bold;">:column_nameN</span>, <span style="color:#ff3333; font-weight:bold;">:column_type</span>
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Tipos de colunas (column_type) disponíveis:</p>
<ul>
<li>:string</li>
<li>:text</li>
<li>:integer</li>
<li>:float</li>
<li>:decimal</li>
<li>:datetime</li>
<li>:timestamp</li>
<li>:time</li>
<li>:date</li>
<li>:binary</li>
<li>:boolean</li>
</ul>
<p><strong>Validações do ActiveRecord funcionam perfeitamente</strong></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Contact <span style="color:#006600; font-weight:bold;">&lt;</span> TablelessModel
  column <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:telephone</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:firm</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:message</span>, <span style="color:#ff3333; font-weight:bold;">:text</span>
&nbsp;
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:message</span>
  validates_format_of <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#91;</span>A<span style="color:#006600; font-weight:bold;">-</span>Z0<span style="color:#006600; font-weight:bold;">-</span>9._<span style="color:#006600; font-weight:bold;">%+-</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span>@<span style="color:#006600; font-weight:bold;">&#91;</span>A<span style="color:#006600; font-weight:bold;">-</span>Z0<span style="color:#006600; font-weight:bold;">-</span>9.<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span>\.<span style="color:#006600; font-weight:bold;">&#91;</span>A<span style="color:#006600; font-weight:bold;">-</span>Z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">2</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#125;</span>$<span style="color:#006600; font-weight:bold;">/</span>i
  validates_length_of <span style="color:#ff3333; font-weight:bold;">:message</span>, <span style="color:#ff3333; font-weight:bold;">:minimum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p><strong>Exemplo completo usando tableless_scaffold gerador</strong></p>
<p>Criar o tableless scaffold</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>generate tableless_scaffold contact name:string telephone:string firm:string email:string message:text</pre></div></div>

<p>Adicionar as validações no model</p>
<p>Abra o arquivo app/models/contact.rb e adicione as validações:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Contact <span style="color:#006600; font-weight:bold;">&lt;</span> TablelessModel
  column <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:telephone</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:firm</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:message</span>, <span style="color:#ff3333; font-weight:bold;">:text</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Validations</span>
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:message</span>
  validates_format_of <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#91;</span>A<span style="color:#006600; font-weight:bold;">-</span>Z0<span style="color:#006600; font-weight:bold;">-</span>9._<span style="color:#006600; font-weight:bold;">%+-</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span>@<span style="color:#006600; font-weight:bold;">&#91;</span>A<span style="color:#006600; font-weight:bold;">-</span>Z0<span style="color:#006600; font-weight:bold;">-</span>9.<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span>\.<span style="color:#006600; font-weight:bold;">&#91;</span>A<span style="color:#006600; font-weight:bold;">-</span>Z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">2</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#125;</span>$<span style="color:#006600; font-weight:bold;">/</span>i
  validates_length_of <span style="color:#ff3333; font-weight:bold;">:message</span>, <span style="color:#ff3333; font-weight:bold;">:minimum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Execute</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>server</pre></div></div>

<p>Abra o seu navegador em: <a href="http://localhost:3000/contacts/new">http://localhost:3000/contacts/new</a></p>
<p><strong>Exemplo completo usando tableless_model gerador</strong></p>
<p>Criar tableless model</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>generate tableless_model contact name:string telephone:string firm:string email:string message:text</pre></div></div>

<p>Adicionar as validações no model</p>
<p>Abra o arquivo app/models/contact.rb e adicione as validações:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Contact <span style="color:#006600; font-weight:bold;">&lt;</span> TablelessModel
  column <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:telephone</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:firm</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
  column <span style="color:#ff3333; font-weight:bold;">:message</span>, <span style="color:#ff3333; font-weight:bold;">:text</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Validations</span>
  validates_presence_of <span style="color:#ff3333; font-weight:bold;">:name</span>, <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:message</span>
  validates_format_of <span style="color:#ff3333; font-weight:bold;">:email</span>, <span style="color:#ff3333; font-weight:bold;">:with</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">/</span>^<span style="color:#006600; font-weight:bold;">&#91;</span>A<span style="color:#006600; font-weight:bold;">-</span>Z0<span style="color:#006600; font-weight:bold;">-</span>9._<span style="color:#006600; font-weight:bold;">%+-</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span>@<span style="color:#006600; font-weight:bold;">&#91;</span>A<span style="color:#006600; font-weight:bold;">-</span>Z0<span style="color:#006600; font-weight:bold;">-</span>9.<span style="color:#006600; font-weight:bold;">-</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">+</span>\.<span style="color:#006600; font-weight:bold;">&#91;</span>A<span style="color:#006600; font-weight:bold;">-</span>Z<span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">2</span>,<span style="color:#006666;">4</span><span style="color:#006600; font-weight:bold;">&#125;</span>$<span style="color:#006600; font-weight:bold;">/</span>i
  validates_length_of <span style="color:#ff3333; font-weight:bold;">:message</span>, <span style="color:#ff3333; font-weight:bold;">:minimum</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006666;">3</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Criar o controlador</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>generate controller contacts new create</pre></div></div>

<p>Adicionar os códigos das actions:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> ContactController <span style="color:#006600; font-weight:bold;">&lt;</span> ApplicationController
  <span style="color:#9966CC; font-weight:bold;">def</span> new
    <span style="color:#0066ff; font-weight:bold;">@contact</span> = Contact.<span style="color:#9900CC;">new</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> create
    <span style="color:#0066ff; font-weight:bold;">@contact</span> = Contact.<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;">:contact</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
    <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@contact</span>.<span style="color:#9900CC;">save</span> <span style="color:#008000; font-style:italic;"># or @contact.valid?</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> = <span style="color:#996600;">&quot;Contact email sent successfully.&quot;</span>
&nbsp;
      <span style="color:#008000; font-style:italic;"># Send contact email</span>
      <span style="color:#008000; font-style:italic;">#MyMailer.deliver_contact(@contact)</span>
&nbsp;
      redirect_to new_contact_path
    <span style="color:#9966CC; font-weight:bold;">else</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;new&quot;</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></td></tr></table></div>

<p>Criar as routas para os contacts</p>
<p>Abra o arquivo config/routes.rb e adicione a linha:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#6666ff; font-weight:bold;">ActionController::Routing::Routes</span>.<span style="color:#9900CC;">draw</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>map<span style="color:#006600; font-weight:bold;">|</span>
  map.<span style="color:#9900CC;">resources</span> <span style="color:#ff3333; font-weight:bold;">:contacts</span>, <span style="color:#ff3333; font-weight:bold;">:only</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:new</span>, <span style="color:#ff3333; font-weight:bold;">:create</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  ...
<span style="color:#9966CC; font-weight:bold;">end</span></pre></td></tr></table></div>

<p>Criar o formulário de contato</p>
<p>Abra o arquivo app/views/contacts/new.html.erb e adicione o conteúdo:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> form_for<span style="color:#006600; font-weight:bold;">&#40;</span>@contact<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;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <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>
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:telephone</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:telephone</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:firm</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:firm</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_field</span> <span style="color:#ff3333; font-weight:bold;">:email</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">label</span> <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">text_area</span> <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&nbsp;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">submit</span> <span style="color:#996600;">&quot;Send&quot;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<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></td></tr></table></div>

<p>Apague o arquivo app/views/contacts/create.html.erb.</p>
<p>Execute</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">.<span style="color: #000000; font-weight: bold;">/</span>script<span style="color: #000000; font-weight: bold;">/</span>server</pre></div></div>

<p>Abra o seu navegador em: <a href="http://localhost:3000/contacts/new">http://localhost:3000/contacts/new</a></p>
<p><strong>Documentação</strong></p>
<p><a href="http://lab.patrickespake.com/tableless_model">http://lab.patrickespake.com/tableless_model</a></p>
<p><strong>Código fonte</strong></p>
<p><a href="http://github.com/patrickespake/tableless_model">http://github.com/patrickespake/tableless_model</a></p>
<p>Se você gostou desse texto e acha que ajudou você, me recomende: <a href="http://www.workingwithrails.com/recommendation/new/person/15479-patrick-espake"><img src="http://workingwithrails.com/images/tools/compact-small-button.jpg" alt="Recommend Me" /></a>.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.patrickespake.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.patrickespake.com/2009/08/11/ruby-on-rails-plugin-tableless_model/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails usando strip_tags nos controllers, models e libs</title>
		<link>http://blog.patrickespake.com/2009/06/15/ruby-on-rails-usando-strip_tags-nos-controllers-models-e-libs/</link>
		<comments>http://blog.patrickespake.com/2009/06/15/ruby-on-rails-usando-strip_tags-nos-controllers-models-e-libs/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 19:46:08 +0000</pubDate>
		<dc:creator>patrickespake</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[actioncontroller]]></category>
		<category><![CDATA[actionview]]></category>
		<category><![CDATA[Base]]></category>
		<category><![CDATA[class]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[controller]]></category>
		<category><![CDATA[exemplo]]></category>
		<category><![CDATA[helper]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[implementação]]></category>
		<category><![CDATA[initializers]]></category>
		<category><![CDATA[lib]]></category>
		<category><![CDATA[método]]></category>
		<category><![CDATA[model]]></category>
		<category><![CDATA[new_rails_defaults]]></category>
		<category><![CDATA[public_methods]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[rb]]></category>
		<category><![CDATA[remover]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[sanitize]]></category>
		<category><![CDATA[sanitizehelper]]></category>
		<category><![CDATA[self]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[strip_tags]]></category>
		<category><![CDATA[tags]]></category>
		<category><![CDATA[usando]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://blog.patrickespake.com/?p=717</guid>
		<description><![CDATA[O Ruby on Rails possui o métogo strip_tags para remover tags html. Esse método está apenas disponível na camada de view, uma vez que faz parte ActionView::Helpers::SanitizeHelper. Eu não concordo com essa implementação do Rails, acho que deveria ser disponível também na camada de controller e model, onde são os lugares que esse método é [...]]]></description>
			<content:encoded><![CDATA[<p>O Ruby on Rails possui o métogo strip_tags para remover tags html. Esse método está apenas disponível na camada de <strong>view</strong>, uma vez que faz parte <strong>ActionView::Helpers::SanitizeHelper</strong>.</p>
<p>Eu não concordo com essa implementação do Rails, acho que deveria ser disponível também na camada de controller e model, onde são os lugares que esse método é mais útil.</p>
<p>Para implementar o strip_tags nos controllers, models e libs, nós podemos adicionar esse método na classe <strong>String</strong>, desta forma estará disponível em qualquer lugar.</p>
<p>Abra o arquivo <strong>config/initializers/new_rails_defaults.rb</strong>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Be sure to restart your server when you modify this file.</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># These settings change the behavior of Rails 2 apps and will be defaults</span>
<span style="color:#008000; font-style:italic;"># for Rails 3. You can remove this initializer when Rails 3 is released.</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>ActiveRecord<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># Include Active Record class name as root for JSON serialized output.</span>
  <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">include_root_in_json</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Store the full class name (including module namespace) in STI type column.</span>
  <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">store_full_sti_class</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Use ISO 8601 format for JSON serialized times and dates.</span>
ActiveSupport.<span style="color:#9900CC;">use_standard_json_time_format</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Don't escape HTML entities in JSON, leave that for the #json_escape helper.</span>
<span style="color:#008000; font-style:italic;"># if you're including raw json in an HTML page.</span>
ActiveSupport.<span style="color:#9900CC;">escape_html_entities_in_json</span> = <span style="color:#0000FF; font-weight:bold;">false</span></pre></td></tr></table></div>

<p>Adicione no final do arquivo as linhas:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">String</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> strip_tags
    <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>.<span style="color:#9900CC;">helpers</span>.<span style="color:#9900CC;">strip_tags</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</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></td></tr></table></div>

<p>As linhas acima criam o método <strong>strip_tags</strong> na classe <strong>String</strong>.</p>
<p>Ficando o arquivo completo assim:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Be sure to restart your server when you modify this file.</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># These settings change the behavior of Rails 2 apps and will be defaults</span>
<span style="color:#008000; font-style:italic;"># for Rails 3. You can remove this initializer when Rails 3 is released.</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#9966CC; font-weight:bold;">defined</span>?<span style="color:#006600; font-weight:bold;">&#40;</span>ActiveRecord<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#008000; font-style:italic;"># Include Active Record class name as root for JSON serialized output.</span>
  <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">include_root_in_json</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
  <span style="color:#008000; font-style:italic;"># Store the full class name (including module namespace) in STI type column.</span>
  <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">store_full_sti_class</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Use ISO 8601 format for JSON serialized times and dates.</span>
ActiveSupport.<span style="color:#9900CC;">use_standard_json_time_format</span> = <span style="color:#0000FF; font-weight:bold;">true</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Don't escape HTML entities in JSON, leave that for the #json_escape helper.</span>
<span style="color:#008000; font-style:italic;"># if you're including raw json in an HTML page.</span>
ActiveSupport.<span style="color:#9900CC;">escape_html_entities_in_json</span> = <span style="color:#0000FF; font-weight:bold;">false</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">String</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> strip_tags
    <span style="color:#6666ff; font-weight:bold;">ActionController::Base</span>.<span style="color:#9900CC;">helpers</span>.<span style="color:#9900CC;">strip_tags</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</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></td></tr></table></div>

<p>Agora reinicie o seu servidor web para pegar essas novas configurações na inicialização da aplicação.</p>
<p>Agora quando você precisar do strip_tags pode usar assim, exemplos:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'title'</span><span style="color:#006600; font-weight:bold;">&#93;</span> = params<span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'title'</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">strip_tags</span>
&nbsp;
ou
&nbsp;
<span style="color:#006600; font-weight:bold;">&gt;&gt;</span> <span style="color:#996600;">'&lt;b&gt;meu texto&lt;/b&gt;'</span>.<span style="color:#9900CC;">strip_tags</span>
<span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;meu texto&quot;</span></pre></div></div>

<p>Se você gostou desse texto e acha que ajudou você, me recomende: <a href="http://www.workingwithrails.com/recommendation/new/person/15479-patrick-espake"><img src="http://workingwithrails.com/images/tools/compact-small-button.jpg" alt="Recommend Me" /></a>.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://blog.patrickespake.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://blog.patrickespake.com/2009/06/15/ruby-on-rails-usando-strip_tags-nos-controllers-models-e-libs/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
