<?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>Schadenfreude &#187; validation</title>
	<atom:link href="http://www.ralree.com/tag/validation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ralree.com</link>
	<description>Malicious enjoyment derived from observing someone else's misfortune</description>
	<lastBuildDate>Thu, 09 Feb 2012 01:49:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Doing things in models on save/update</title>
		<link>http://www.ralree.com/2006/06/19/doing-things-in-models-on-save-update/</link>
		<comments>http://www.ralree.com/2006/06/19/doing-things-in-models-on-save-update/#comments</comments>
		<pubDate>Mon, 19 Jun 2006 13:31:14 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2007/10/13/doing-things-in-models-on-save-update</guid>
		<description><![CDATA[So, I wanted to do some sweet gsubbing before the save of projects in Rails. I found the API docs had a wealth of information about this, but I figured I&#8217;d post my method anyway: before_create :fix_amount before_update :fix_amount Now you just define them under private in the model: def fix_amount self.amount.gsub!(/^\$/, &#34;&#34;) end This takes those pesky $&#8217;s right out, back to the back yard where they belong.]]></description>
			<content:encoded><![CDATA[<p>So, I wanted to do some sweet gsubbing before the save of projects in Rails.  I found the <a href="http://api.rubyonrails.com/classes/ActiveRecord/Callbacks.html">API docs</a> had a wealth of information about this, but I figured I&#8217;d post my method anyway:</p>
<div class="CodeRay">
<div class="code">
<pre><code>
  before_create <span class="sy">:fix_amount</span>
  before_update <span class="sy">:fix_amount</span>
</code></pre>
</div>
</div>
<p>Now you just define them under <strong>private</strong> in the model:</p>
<div class="CodeRay">
<div class="code">
<pre><code>
  <span class="r">def</span> <span class="fu">fix_amount</span>
    <span class="pc">self</span>.amount.gsub!(<span class="rx"><span class="dl">/</span><span class="k">^</span><span class="ch">\$</span><span class="dl">/</span></span>, <span class="s"><span class="dl">&quot;</span><span class="dl">&quot;</span></span>)
  <span class="r">end</span>
</code></pre>
</div>
</div>
<p>This takes those pesky $&#8217;s right out, back to the back yard where they belong.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2006/06/19/doing-things-in-models-on-save-update/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>HTTP URL Validation Improved</title>
		<link>http://www.ralree.com/2006/06/16/http-url-validation-improved/</link>
		<comments>http://www.ralree.com/2006/06/16/http-url-validation-improved/#comments</comments>
		<pubDate>Fri, 16 Jun 2006 16:40:28 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[plugins]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2007/10/13/http-url-validation-improved</guid>
		<description><![CDATA[I found the HTTP URL Validator for Rails very interesting, and well coded, yet it lacked some things such as URL format restrictions. I added some things, and I came up with a sweet solution. It checks the format of the given URL, the content type, and whether it was permanently moved. I might be adding to this in the future. #Check for content type: validates_http_url :url, :content_type =&#62; &#34;text/html&#34; #Do not check for content type, just make sure the [...]]]></description>
			<content:encoded><![CDATA[<p>I found the <a href="http://www.ccjr.name/blog/2006/01/25/http-url-validator">HTTP URL Validator</a> for Rails very interesting, and well coded, yet it lacked some things such as URL format restrictions.  I added some things, and I came up with <a href="http://modzer0.cs.uaf.edu/repos/hank/code/http_url_validation_improved/lib/http_url_validation_improved.rb">a sweet solution.</a><br />
It checks the format of the given URL, the content type, and whether it was permanently moved.  I might be adding to this in the future.</p>
<div class="CodeRay">
<div class="code">
<pre><code>
<span class="c">#Check for content type:</span>
  validates_http_url <span class="sy">:url</span>, <span class="sy">:content_type</span> =&gt; <span class="s"><span class="dl">&quot;</span><span class="k">text/html</span><span class="dl">&quot;</span></span>

<span class="c">#Do not check for content type, just make sure the site is accessible:</span>
  validates_http_url <span class="sy">:website</span>

<span class="c">#Make sure there is a DNS entry for a domain</span>
  validates_http_domain <span class="sy">:domain</span>
<span class="c"># Domain must be in 'www.site.com' for or 'site.com' form.</span>
<span class="c"># No http://, no path.</span>
</code></pre>
</div>
</div>
<h3>Update (6/26/06)</h3>
<p>Added the <em>validates_http_domain</em> method:</p>
<div class="CodeRay">
<div class="code">
<pre><code>
<span class="r">def</span> <span class="fu">validates_http_domain</span>(*attr_names)
  validates_each(attr_names) <span class="r">do</span> |record, attr_name, value|
    <span class="c"># Set valid true on successful connect (all we need is one, one is all we need)</span>
    failed = <span class="pc">true</span>
    possibilities = [value, <span class="s"><span class="dl">&quot;</span><span class="k">www.</span><span class="dl">&quot;</span></span>+value]
    possibilities.each <span class="r">do</span> |url|
      <span class="r">begin</span>
        temp = <span class="co">Socket</span>.gethostbyname(url)
        <span class="r">rescue</span> <span class="co">SocketError</span>
          <span class="r">next</span>
        <span class="r">end</span>
        failed = <span class="pc">false</span>
        <span class="r">break</span>
    <span class="r">end</span>
    record.errors.add(attr_name, <span class="s"><span class="dl">&quot;</span><span class="k">cannot be resolved.</span><span class="dl">&quot;</span></span>) <span class="r">if</span> failed
  <span class="r">end</span>
<span class="r">end</span>
</code></pre>
</div>
</div>
<p>Now I can just use </p>
<pre><code>
:validates_http_domain :website
</code></pre>
<p>in my controller, and everything comes up roses. ;)</p>
<h3>Update (9/30/06)</h3>
<p>It was brought to my attention through a dialogue of emails and the comments that I needed a simple way for people to modify the plugin to accept different codes depending on their needs, or at least a simple way for me to modify the default accepted codes.  Therefore, I made an array in the library called <strong>allowed_codes</strong>:</p>
<div class="CodeRay">
<div class="code">
<pre><code>
           allowed_codes = [
            <span class="co">Net</span>::<span class="co">HTTPMovedPermanently</span>,
            <span class="co">Net</span>::<span class="co">HTTPOK</span>,
            <span class="co">Net</span>::<span class="co">HTTPCreated</span>,
            <span class="co">Net</span>::<span class="co">HTTPAccepted</span>,
            <span class="co">Net</span>::<span class="co">HTTPNonAuthoritativeInformation</span>,
            <span class="co">Net</span>::<span class="co">HTTPPartialContent</span>,
            <span class="co">Net</span>::<span class="co">HTTPFound</span>,
            <span class="co">Net</span>::<span class="co">HTTPTemporaryRedirect</span>,
            <span class="co">Net</span>::<span class="co">HTTPSeeOther</span>
           ]
</code></pre>
</div>
</div>
<p>I&#8217;ll make it so you can push on your own custom codes from the model soon.  This is what I envision:</p>
<div class="CodeRay">
<div class="code">
<pre><code>
  validates_http_url <span class="sy">:website</span>, <span class="sy">:extra_codes</span> =&gt; [ <span class="co">HTTPResetContent</span>, <span class="co">HTTPPartialContent</span> ]
</code></pre>
</div>
</div>
<p>I&#8217;ll post here when this is reality, and probably make another blog post as well so the aggregators get it.</p>
<hr/>
<p>You can download it with svn:</p>
<pre><code>
svn co https://modzer0.cs.uaf.edu/repos/hank/code/http_url_validation_improved
</code></pre>
<p>Or use it as a plugin:</p>
<pre><code>
./script/plugin install -x https://modzer0.cs.uaf.edu/repos/hank/code/http_url_validation_improved
</code></pre>
<p>The above command only works if you have your entire rails project in subversion.  If you do not, which I don&#8217;t recommend, you should either add it to a repository or alternatively remove the <em>*-x</em>* from the command.  Of course, this will remove support for updating to the new code if I make a change.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2006/06/16/http-url-validation-improved/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

