<?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/"
	>

<channel>
	<title>RailsRuminations</title>
	<atom:link href="http://dougselph.com/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://dougselph.com/rails</link>
	<description>Solutions, Quandaries, Tips</description>
	<pubDate>Wed, 02 Jun 2010 16:50:22 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A Rails3 TabsRenderer</title>
		<link>http://dougselph.com/rails/2010/06/rails3_tabsrenderer/</link>
		<comments>http://dougselph.com/rails/2010/06/rails3_tabsrenderer/#comments</comments>
		<pubDate>Wed, 02 Jun 2010 11:48:35 +0000</pubDate>
		<dc:creator>Doug Selph</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[jQuery]]></category>

		<category><![CDATA[jQuery UI]]></category>

		<category><![CDATA[Rails3]]></category>

		<guid isPermaLink="false">http://dougselph.com/rails/?p=40</guid>
		<description><![CDATA[I&#8217;ve been working on a project using beta 3 of Rails3, and was looking to use tabs from the jQuery UI project. Having run across a post featuring a TabsRenderer helper class, I thought I&#8217;d give it a whirl. The code from that post was written in 2008, and it did not work with the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working on a project using beta 3 of Rails3, and was looking to use tabs from the <a href="http://jqueryui.com/home">jQuery UI</a> project. Having run across a <a href="http://www.codeofficer.com/blog/entry/ruby_on_rails_jquery_ui_and_tabsrenderer/">post featuring a TabsRenderer helper class</a>, I thought I&#8217;d give it a whirl. The code from that post was written in 2008, and it did not work with the beta of Rails3, so I set out to see if I could get it working.</p>
<p>The original code for the helper class, when executed in Rails3, was rendering escaped HTML for the tab bodies, and nothing for the tab headers. Simply changing the call in the view to tabs.render to this:</p>
<pre>tabs.render.html_safe</pre>
<p>didn&#8217;t do the trick for unescaping the tab bodies. What ended up working was using this for the TabsRenderer class:</p>
<pre>class TabsRenderer

  def initialize(template, options={})
    @template = template
    @options = options
    @tabs = []
  end

  def create(tab_id, tab_text, options={}, &amp;block)
    raise "Block needed for TabsRenderer#CREATE" unless block_given?
    @tabs &lt;&lt; [tab_id, tab_text, options, block]
  end

  def render
    content_tag(:div, (render_tabs.html_safe + render_bodies.html_safe), {:id =&gt; :tabs}.merge(@options))
  end

  private #  ---------------------------------------------------------------------------

  def render_tabs
    content_tag(:ul, render_headers.html_safe)
  end

  def render_headers
    @tabs.collect do |tab|
      content_tag(:li, link_to(content_tag(:span, tab[1]), "##{tab[0]}") )
    end.to_s
  end

  def  render_bodies
    @tabs.collect do |tab|
      content_tag(:div, capture(&amp;tab[3]), tab[2].merge(:id =&gt; tab[0]))
    end.to_s
  end

  def method_missing(*args, &amp;block)
    @template.send(*args, &amp;block)
  end

end</pre>
<p>Everything else about the sample code on the <a href="http://www.codeofficer.com/blog/entry/ruby_on_rails_jquery_ui_and_tabsrenderer/">original post</a> can be used as originally presented.</p>
<p>Hope this helps you as you adjust to Rails3!</p>
]]></content:encoded>
			<wfw:commentRss>http://dougselph.com/rails/2010/06/rails3_tabsrenderer/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Email Address Validation in Rails</title>
		<link>http://dougselph.com/rails/2009/05/email-address-validation-in-rails/</link>
		<comments>http://dougselph.com/rails/2009/05/email-address-validation-in-rails/#comments</comments>
		<pubDate>Fri, 22 May 2009 03:06:58 +0000</pubDate>
		<dc:creator>Doug Selph</dc:creator>
		
		<category><![CDATA[Rails]]></category>

		<category><![CDATA[Validation]]></category>

		<category><![CDATA[EmailVeracity]]></category>

		<guid isPermaLink="false">http://dougselph.com/rails/?p=5</guid>
		<description><![CDATA[I recently had a need to validate email addresses submitted to a Rails application. I found several plugins/gems that could help with the task, and eventually settled on one. Then I had to figure out how to actually integrate that gem with the validation process of ActiveRecord. As has often been the case in my [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had a need to validate email addresses submitted to a Rails application. I found several plugins/gems that could help with the task, and eventually settled on one. Then I had to figure out how to actually integrate that gem with the validation process of ActiveRecord. As has often been the case in my experience with Rails, once I figured it out it was incredibly easy, but it was less obvious on the front-end how to go about doing what I needed. So let&#8217;s begin&#8230;</p>
<p>I settled on using the <a href="http://github.com/heycarsten/email-veracity/tree/master">email-veracity</a> gem by Carsten Nielsen for validations. It is available from the gem site at github.<br />
<code>
<div class="code">
<br />
sudo gem sources -a http://gems.github.com<br />
sudo gem install heycarsten-email-veracity<br />
&nbsp;
</div>
<p></code></p>
<p>
Great. Now I was able to play around with validating emails from irb, but what about in my app? Hmm&hellip;</p>
<p>I started digging around in the source code for the ActiveRecord class, wondering how I might create my own validation macro to use email-veracity. That was going ok until I couldn&#8217;t figure out how to access the error collection for my ActiveRecord object. Thankfully, that sent me back to scouring the web for a lead on another way to skin this cat.</p>
<p>Now with a fresher mind than when I&#8217;d last been in the ActiveRecord source, I looked around some more and took a close look at the <code>validates_each</code> macro. This is what I wound up with.</p>
<blockquote><p><strong>Note</strong>: You can get cleanly formatted copies of the code excerpts below from <a href="http://gist.github.com/115893">gist.github.com</a>.</p></blockquote>
<p><code>
<div class="code">
<br />
class Customer &lt; ActiveRecord::Base<br />
# EmailVeracity is used to validate email addresses submitted<br />
&nbsp;require &#8216;email_veracity&#8217;<br />
&hellip;<br />
&nbsp;validates_each :email_address, :on =&gt; :save \<br />
&nbsp;&nbsp;&nbsp;&nbsp;do |record, attr, value|<br />
&nbsp;<br />
&nbsp;&nbsp;# create new Address object from string param<br />
&nbsp;&nbsp;address = EmailVeracity::Address.new(value)<br />
&nbsp;<br />
&nbsp;&nbsp;# Add to errors collection unless address is valid<br />
&nbsp;&nbsp;record.errors.add attr, \<br />
&nbsp;&nbsp;&nbsp;&nbsp;&#8217;- Does not appear to be a valid email address&#8217; \<br />
&nbsp;&nbsp;&nbsp;&nbsp;unless address.valid?<br />
&nbsp;end<br />
end<br />
&nbsp;
</div>
<p></code></p>
<p>
This gives basic validation as to format of the address supplied. Now, in another model, I wanted to confirm that the address not only was of good form, but that the domain in the email address belonged to the customer&#8217;s domain as recorded in the database.<br />
<code>
<div class="code">
<br />
class Email < ActiveRecord::Base<br />
&nbsp;# EmailVeracity is used to validate email addresses submitted<br />
&nbsp;require 'email_veracity'<br />
&nbsp;<br />
&nbsp;validates_each :email_address do |record, attr, value|<br />
&nbsp;&nbsp;# create new Address object from string param<br />
&nbsp;&nbsp;address = EmailVeracity::Address.new(value)<br />
&nbsp;&nbsp;if address.valid?<br />
&nbsp;&nbsp;&nbsp;# Parse out domain from address<br />
&nbsp;&nbsp;&nbsp;domain = address.domain<br />
&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;# Is domain in database on customers account?<br />
&nbsp;&nbsp;&nbsp;domain_count = Domain.count(:conditions => \<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8221;domain_name = &#8216;#{domain}&#8217; and customer_id = \<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#{record[:customer_id]}&#8221;)<br />
&nbsp;&nbsp;&nbsp;record.errors.add attr, \<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8217;- The email address submitted is on a domain \<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;which is not linked to your customer account&#8217; \<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless domain_count > 0<br />
&nbsp;&nbsp;else<br />
&nbsp;&nbsp;&nbsp;record.errors.add attr, \<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8217;- Does not appear to be a valid email address&#8217; \<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;unless address.valid?<br />
&nbsp;&nbsp;end<br />
&nbsp;end<br />
end<br />
&nbsp;
</div>
<p></code></p>
<p>
Is this a perfect solution? There will be varied opinions on that question. It is a working solution, but comes at the price of adding EmailVeracity to those of your ActiveRecord instances where you want to validate email addresses. You&#8217;ll have to judge whether that tradeoff is a reasonable one for your application.</p>
<p>EmailVeracity can do many other things, including finding the MX hosts for a given address&#8217;s domain name. Now you&#8217;re talking about an operation that necessitates a network call, which could fail or timeout, so you typically would not want to include such methods in a validation sequence. </p>
]]></content:encoded>
			<wfw:commentRss>http://dougselph.com/rails/2009/05/email-address-validation-in-rails/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Umm, hello?</title>
		<link>http://dougselph.com/rails/2009/05/umm-hello/</link>
		<comments>http://dougselph.com/rails/2009/05/umm-hello/#comments</comments>
		<pubDate>Thu, 21 May 2009 21:34:58 +0000</pubDate>
		<dc:creator>Doug Selph</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://dougselph.com/rails/?p=3</guid>
		<description><![CDATA[Greetings! There have been times in my experience of using Rails when I struggled to figure out how to do something, and once it was solved I thought, &#8220;I really should write that down somewhere.&#8221; And guess what? I finally decided to give myself a place to &#8220;write it down&#8221; in this blog. So this [...]]]></description>
			<content:encoded><![CDATA[<p>Greetings! There have been times in my experience of using Rails when I struggled to figure out how to do something, and once it was solved I thought, &#8220;I really should write that down somewhere.&#8221; And guess what? I finally decided to give myself a place to &#8220;write it down&#8221; in this blog. So this is here to serve my own interests in having recall of how I did something (without having to go digging through my code base in a quest for the answer), and hopefully, with a little help from Google, et al, others who find answers to their own questions here.</p>
<p>Thanks for stopping by. Enjoy!</p>
]]></content:encoded>
			<wfw:commentRss>http://dougselph.com/rails/2009/05/umm-hello/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
