<?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>Netphase &#187; Ruby On Rails</title>
	<atom:link href="http://blog.netphase.com/category/blog/rails/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.netphase.com</link>
	<description>for a connected world</description>
	<lastBuildDate>Mon, 14 Dec 2009 21:40:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Netphase Launches Homeschool Apple</title>
		<link>http://blog.netphase.com/2009/03/07/netphase-launches-homeschool-apple/</link>
		<comments>http://blog.netphase.com/2009/03/07/netphase-launches-homeschool-apple/#comments</comments>
		<pubDate>Sun, 08 Mar 2009 04:59:09 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Acts As Amazon Product]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[business]]></category>
		<category><![CDATA[newsworthy]]></category>

		<guid isPermaLink="false">http://www.netphase.com/?p=308</guid>
		<description><![CDATA[Netphase launched the first in what is expected to be a long line of web offerings geared towards making the lives of homeschoolers a little easier. Homeschool Apple takes the pain out of buying and selling used homeschool curriculum.]]></description>
			<content:encoded><![CDATA[<p>Earlier today, Netphase launched its first in what is expected to be a long line of web applications based on its new virtual mall engine, WeSell.  WeSell aims to conquer some of the problems typically associated with selling from multiple vendors in a shopping cart-based system, utilizing only one checkout.</p>
<p>The technology behind it is really quite remarkable. Netphase&#8217;s proprietary solution takes into account the available inventory of the seller, who typically will be an average user with products in their possession that they wish to resell. It prevents shopping cart conflicts, yet also considers how long a prospective buyer is allowed to keep an item in his cart before the item is returned back to the active inventory for others to purchase. Bottom line is that it strives to strike the difficult balance of allowing a seller to have inventory tied up by only those wanting to purchase, and also of allowing a buyer with an item in her cart to shop with confidence, knowing that when she checks out, the item is in fact hers.</p>
<p>Other features of WeSell protect both the buyer and seller by way of a built-in transaction escrow system. It is fully secure, deployed in the infinitely scalable Amazon EC2 Cloud, and of course, it runs on Ruby on Rails.</p>
<h3>About Homeschool Apple</h3>
<p>The <a href="http://homeschoolapple.com">Homeschool Apple</a> mission is to be an oasis for homeschoolers on the internet. HSA seeks to provide a one-stop place where homeschoolers can sell their used, new, self-published or downloadable curriculum, connect with others, and share their talents with the world. While far from complete, Homeschool Apple is well on its way, with the most unique homeschool store on the Web and a rock-solid platform on which to build. The site is currently in public Beta, and homeschoolers are encouraged to sign up for their FREE account now. Pass the word to your friends, won&#8217;t you?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2009/03/07/netphase-launches-homeschool-apple/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails with Spring, Hibernate and JPA</title>
		<link>http://blog.netphase.com/2009/01/23/ruby-on-rails-with-spring-hibernate-and-jpa/</link>
		<comments>http://blog.netphase.com/2009/01/23/ruby-on-rails-with-spring-hibernate-and-jpa/#comments</comments>
		<pubDate>Fri, 23 Jan 2009 18:56:42 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://www.netphase.com/?p=248</guid>
		<description><![CDATA[Scott led a session at Bar Camp extolling the virtues of running your Ruby on Rails app in a pure Java environment, accessing the same resources as your programming brethren.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m leading a discussion on this topic at the <a href="http://barcampcharlotte.com/">Charlotte BarCamp</a> tomorrow.</p>
<p>Many companies who have invested a lot of time and money in Java technology could still benefit greatly from Ruby on Rails by using it to build new sites that make use of their backend Java based services.</p>
<p>Here&#8217;s an example of an <a href="http://rspec.info/">RSpec</a> test that uses a Java DAO, provided by <a href="http://www.springsource.org">Spring</a> to access a database with <a href="http://java.sun.com/javaee/technologies/persistence.jsp">JPA</a> / <a href="http://www.hibernate.org/">Hibernate</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">it <span style="color:#996600;">&quot;should retrieve an Account by ID&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
  load_data <span style="color:#996600;">&quot;dao/accounts.xml&quot;</span>
  dao = spring.<span style="color:#9900CC;">getBean</span> <span style="color:#996600;">'accountDao'</span>
  acct = dao.<span style="color:#9900CC;">find</span> <span style="color:#996600;">&quot;5000&quot;</span>
  acct.<span style="color:#9900CC;">should_not</span> be_nil
  acct.<span style="color:#9900CC;">getName</span>.<span style="color:#9900CC;">should</span> == <span style="color:#996600;">&quot;Alpha&quot;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>It&#8217;s surprisingly easy to use a Spring context here by adding the following method to my spec_helper:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">import <span style="color:#996600;">'org.springframework.context.support.ClassPathXmlApplicationContext'</span>
import <span style="color:#996600;">'org.springframework.core.io.ClassPathResource'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> spring<span style="color:#006600; font-weight:bold;">&#40;</span>context_file=<span style="color:#996600;">&quot;beans.xml&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  java.<span style="color:#9900CC;">lang</span>.<span style="color:#CC00FF; font-weight:bold;">Thread</span>.<span style="color:#9900CC;">current_thread</span>.<span style="color:#9900CC;">context_class_loader</span> =
      JRuby.<span style="color:#9900CC;">runtime</span>.<span style="color:#9900CC;">getJRubyClassLoader</span>
  <span style="color:#0066ff; font-weight:bold;">@ctx</span> <span style="color:#006600; font-weight:bold;">||</span>= ClassPathXmlApplicationContext.<span style="color:#9900CC;">new</span> context_file
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Using that helper I am able to access any component managed by Spring.  As designed, Spring will wire up any required dependencies, provide a pooled database connection, and handle any other configuration necessary so that the bean returned is ready to use.</p>
<p>My test also uses <a href="http://www.dbunit.org/">DBUnit</a> to load an XML fixture.  It can do that by adding this method to spec_helper:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">import <span style="color:#996600;">'org.dbunit.database.DatabaseConnection'</span>
import <span style="color:#996600;">'org.dbunit.dataset.xml.FlatXmlDataSet'</span>
import <span style="color:#996600;">'org.dbunit.operation.DatabaseOperation'</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">def</span> load_data<span style="color:#006600; font-weight:bold;">&#40;</span>data_file<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">begin</span>
    <span style="color:#0066ff; font-weight:bold;">@dataSource</span> <span style="color:#006600; font-weight:bold;">||</span>= spring.<span style="color:#9900CC;">getBean</span> <span style="color:#996600;">'dataSource'</span>
    <span style="color:#0066ff; font-weight:bold;">@conn</span> <span style="color:#006600; font-weight:bold;">||</span>= DatabaseConnection.<span style="color:#9900CC;">new</span> <span style="color:#0066ff; font-weight:bold;">@dataSource</span>.<span style="color:#9900CC;">getConnection</span>
    <span style="color:#0066ff; font-weight:bold;">@dataset</span> <span style="color:#006600; font-weight:bold;">||</span>=
        FlatXmlDataSet.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>ClassPathResource.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>data_file<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">getInputStream</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#6666ff; font-weight:bold;">DatabaseOperation::CLEAN_INSERT</span>.<span style="color:#9900CC;">execute</span><span style="color:#006600; font-weight:bold;">&#40;</span>@conn, <span style="color:#0066ff; font-weight:bold;">@dataset</span><span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">rescue</span>
    <span style="color:#CC0066; font-weight:bold;">print</span> <span style="color:#996600;">&quot;load_data error: &quot;</span>, $!, <span style="color:#996600;">&quot;<span style="color:#000099;">\n</span>&quot;</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>DBUnit provides a useful way to load data fixtures into a test database.  The reason I&#8217;m using DBUnit instead of the more common YAML format used in Rails apps, is so that I can make use of the fixtures already available in the Java project.</p>
<p>Here&#8217;s what my DBUnit fixture looks like:</p>
<p>This combination of technologies seems like a pretty powerful combination to me, especially for those companies who are entrenched in Java and are looking for a way to improve their productivity.</p>
<p>I&#8217;m interested to know who else is using this type of setup.  Leave a comment and describe your experience.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2009/01/23/ruby-on-rails-with-spring-hibernate-and-jpa/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AAAP Updated!</title>
		<link>http://blog.netphase.com/2008/12/05/acts_as_amazon_product-update/</link>
		<comments>http://blog.netphase.com/2008/12/05/acts_as_amazon_product-update/#comments</comments>
		<pubDate>Fri, 05 Dec 2008 17:45:02 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Acts As Amazon Product]]></category>
		<category><![CDATA[Netphase News]]></category>
		<category><![CDATA[Open Source]]></category>
		<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[aaap]]></category>
		<category><![CDATA[acts_as_amazon_product]]></category>
		<category><![CDATA[gem]]></category>

		<guid isPermaLink="false">http://www.netphase.com/?p=160</guid>
		<description><![CDATA[It&#8217;s been a while since we have called attention to it, but over the course of the year, our Amazon product integration gem, acts_as_amazon_product, has been improved.
First, we got some participation from David Eisinger to help improve testing and some other critical features. Thanks, David! Recently though, we added some new functionality we had lying [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been a while since we have called attention to it, but over the course of the year, our Amazon product integration gem, <a href="http://github.com/netphase/aaap/tree/master" target="_new">acts_as_amazon_product</a>, has been improved.</p>
<p>First, we got some participation from David Eisinger to help improve testing and some other critical features. Thanks, David! Recently though, we added some new functionality we had lying around in an old plugin called acts_as_amazonable.</p>
<p>The new functionality allows you to load Amazon product attributes into a new or existing model object on your system. You can define which fields are loaded, and which attributes in your model they map to. We are using this functionality on a new double-secret project that we are working on, and it is great. In that project, we are letting our users type in an ISBN code, and we pre-populate a form with all of the Amazon data we want for that product, saving the user from a lot of typing. We also pick up the images, so they don&#8217;t even have  to load an image to sell their item. Pretty cool!</p>
<p>Here&#8217;s how you use aaap&#8217;s new functionality:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'acts_as_amazon_product'</span>
&nbsp;
 <span style="color:#9966CC; font-weight:bold;">class</span> LocalBook <span style="color:#006600; font-weight:bold;">&amp;</span>lt; <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
   acts_as_amazon_product<span style="color:#006600; font-weight:bold;">&#40;</span>
     <span style="color:#ff3333; font-weight:bold;">:asin</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'isbn'</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
     <span style="color:#ff3333; font-weight:bold;">:access_key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'0123456'</span>, <span style="color:#ff3333; font-weight:bold;">:associate_tag</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'assoc-20'</span>,
     <span style="color:#ff3333; font-weight:bold;">:auto_load_fields</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#123;</span>
         <span style="color:#ff3333; font-weight:bold;">:title</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
         <span style="color:#ff3333; font-weight:bold;">:isbn</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'asin'</span>,
         <span style="color:#ff3333; font-weight:bold;">:publisher_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'manufacturer'</span>,
         <span style="color:#ff3333; font-weight:bold;">:author</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'author'</span><span style="color:#006600; font-weight:bold;">&#125;</span>
   <span style="color:#006600; font-weight:bold;">&#41;</span>
 <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>The keys in the auto_load_fields hash are the fields in your object, and the values are their Amazon equivalents. In the true Rails convention over configuration spirit, we have included some of the most popular auto load fields as defaults. If your object contains the following attributes, they will automatically be loaded with their Amazon equivalents:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#ff3333; font-weight:bold;">:auto_load_fields</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#123;</span>
     <span style="color:#ff3333; font-weight:bold;">:title</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
     <span style="color:#ff3333; font-weight:bold;">:isbn</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'asin'</span>,
     <span style="color:#ff3333; font-weight:bold;">:publisher_name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'manufacturer'</span>,
     <span style="color:#ff3333; font-weight:bold;">:author</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'author'</span>,
     <span style="color:#ff3333; font-weight:bold;">:binding</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'binding'</span>,
     <span style="color:#ff3333; font-weight:bold;">:list_price</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'listprice/amount'</span>,
     <span style="color:#ff3333; font-weight:bold;">:pages</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'numberofpages'</span>,
     <span style="color:#ff3333; font-weight:bold;">:description</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'content'</span>,
     <span style="color:#ff3333; font-weight:bold;">:small_image_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'smallimage/url'</span>,
     <span style="color:#ff3333; font-weight:bold;">:medium_image_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'mediumimage/url'</span>,
     <span style="color:#ff3333; font-weight:bold;">:large_image_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'largeimage/url'</span>,
     <span style="color:#ff3333; font-weight:bold;">:detail_url</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'detailpageurl'</span>
<span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Don&#8217;t worry if your object does not have some or most of these fields; Acts as Amazon Product is smart enough to check to see if the method exists before attempting to load up the Amazon value.</p>
<p>Now, if by some coincidence you have some fields in your local object that have the same name as our auto_load defaults, you can choose to specifically ignore those fields using, you guessed it, :ignore_fields.  :ignore_fields is a simple array of fields in your object that you do NOT want to be replaced with the Amazon equivalent.  You would specify them like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">acts_as_amazon_product<span style="color:#006600; font-weight:bold;">&#40;</span>
    <span style="color:#ff3333; font-weight:bold;">:asin</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'isbn'</span>, <span style="color:#ff3333; font-weight:bold;">:name</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'title'</span>,
    <span style="color:#ff3333; font-weight:bold;">:access_key</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'0123456'</span>, <span style="color:#ff3333; font-weight:bold;">:associate_tag</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#996600;">'assoc-20'</span>,
    <span style="color:#ff3333; font-weight:bold;">:ignore_fields</span> =<span style="color:#006600; font-weight:bold;">&amp;</span>gt; <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#ff3333; font-weight:bold;">:small_image_url</span>, <span style="color:#ff3333; font-weight:bold;">:medium_image_url</span><span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>In this instance, the object in question would load Amazon info for any of the default fields that the object has, with the exception of the small_image_url and medium_image_url values.</p>
<p>To load the Amazon values into your object for local storage, you may call the following methods:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">load_from_amazon<span style="color:#006600; font-weight:bold;">&#40;</span>title_or_asin, by_title = <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span>
load_from_amazon!<span style="color:#006600; font-weight:bold;">&#40;</span>title_or_asin, by_title = <span style="color:#0000FF; font-weight:bold;">false</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>The two methods do exactly the same thing, except the first simply returns a new, unsaved object with its Amazon-like fields loaded with their appropriate Amazon values.  The second version (load_from_amazon!) goes one step further and saves the object for good measure.</p>
<p>You may pass either an isbn (asin) or a title in.  If you are passing a title, you need to specify that you are passing a title by also passing true as the second parameter so we will perform an item_search rather than an item_lookup. Note that passing a title is risky business, since currently the code will take the first result returned and load that into your object. So unless your title is unique, I would recommend sticking with the isbn (asin) value.</p>
<p>Since that is the default, all you need to do would be something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@local_book</span> = LocalBook.<span style="color:#9900CC;">load_from_amazon</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'1590598415'</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>If your object was similar to the LocalBook object in our example, for instance, you would not need to specify the auto_load_fields in the acts_as call at all.  The title, isbn, publisher_name and author methods would automatically be called once you call load_from_amazon, and their values populated appropriately.</p>
<p>It is important to note that if you call load_from_amazon with an invalid asin, you will simply get back an empty object.</p>
<p>Now, calling load_from_amazon still allows you to take advantage of the other Acts as Amazon features, like the dynamic loading of Amazon attributes on the fly by using the various amazon.foo methods as it always has.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2008/12/05/acts_as_amazon_product-update/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>nested error_messages_for</title>
		<link>http://blog.netphase.com/2008/03/06/nested-error_messages_for/</link>
		<comments>http://blog.netphase.com/2008/03/06/nested-error_messages_for/#comments</comments>
		<pubDate>Thu, 06 Mar 2008 03:20:55 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.netphase.com/2008/03/06/nested-error_messages_for/</guid>
		<description><![CDATA[I think lots of folks have run into this problem of using error_messages_for to display errors of nested or subordinate objects.  The current Rails method doesn&#8217;t handle it well.  In addition, there&#8217;s the problem of not being able to specify the full message.  So&#8230; I tackled this problem today and came up [...]]]></description>
			<content:encoded><![CDATA[<p>I think lots of folks have run into this problem of using error_messages_for to display errors of nested or subordinate objects.  The current Rails method doesn&#8217;t handle it well.  In addition, there&#8217;s the problem of not being able to specify the full message.  So&#8230; I tackled this problem today and came up with a pretty good solution using recursion.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> nested_error_messages_for<span style="color:#006600; font-weight:bold;">&#40;</span>object<span style="color:#006600; font-weight:bold;">&#41;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#CC00FF; font-weight:bold;">Symbol</span>, <span style="color:#CC0066; font-weight:bold;">String</span><span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#9900CC;">member</span>? object.<span style="color:#9966CC; font-weight:bold;">class</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    object = instance_variable_get<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;@#{object}&quot;</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;">unless</span> object.<span style="color:#0000FF; font-weight:bold;">nil</span>? <span style="color:#006600; font-weight:bold;">||</span> object.<span style="color:#9900CC;">errors</span>.<span style="color:#9900CC;">count</span>.<span style="color:#9900CC;">zero</span>?
    error_messages = object.<span style="color:#9900CC;">errors</span>.<span style="color:#9900CC;">to_a</span>.<span style="color:#9900CC;">uniq</span>.<span style="color:#9900CC;">map</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>key, value<span style="color:#006600; font-weight:bold;">|</span>
      object2 = object.<span style="color:#9900CC;">send</span><span style="color:#006600; font-weight:bold;">&#40;</span>key<span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">if</span> object2.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#CC0066; font-weight:bold;">Array</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        object2.<span style="color:#9900CC;">collect</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>obj<span style="color:#006600; font-weight:bold;">|</span> 
          content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:li</span>, nested_error_messages_for<span style="color:#006600; font-weight:bold;">&#40;</span>obj<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span> 
        <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#9966CC; font-weight:bold;">elsif</span> object2.<span style="color:#9900CC;">is_a</span>?<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span><span style="color:#006600; font-weight:bold;">&#41;</span>
        content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:li</span>, nested_error_messages_for<span style="color:#006600; font-weight:bold;">&#40;</span>object2<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;">elsif</span> value.<span style="color:#9900CC;">match</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:#006600; font-weight:bold;">&#41;</span>
        content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:li</span>, value<span style="color:#006600; font-weight:bold;">&#91;</span>1..<span style="color:#9900CC;">value</span>.<span style="color:#9900CC;">length</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      <span style="color:#9966CC; font-weight:bold;">else</span>
        content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:li</span>, 
           <span style="color:#996600;">&quot;#{key.underscore.split('_').join(' ').humanize} #{value}&quot;</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>
    content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:div</span>,
      content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:div</span>, 
        <span style="color:#996600;">&quot;#{object.class} has errors&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'error_field'</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">+</span>
      content_tag<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:ul</span>, error_messages<span style="color:#006600; font-weight:bold;">&#41;</span>, <span style="color:#ff3333; font-weight:bold;">:class</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'error_block'</span><span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">else</span>
      <span style="color:#996600;">''</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>To use it, just replace your current call to error_messages_for with nested_error_messages_for.  There&#8217;s nothing extra that needs to be done in the controller as with some recipes.  You&#8217;ll also want to make it look good with some CSS.</p>
<p>I&#8217;m pretty sure there&#8217;s more that could be done with this, but thought I&#8217;d present it here first and try to get some feedback before turning it into a gem.  Let me know how it works for you.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2008/03/06/nested-error_messages_for/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Extending ActiveRecord with count_since</title>
		<link>http://blog.netphase.com/2008/02/23/extending-activerecord-with-count_since/</link>
		<comments>http://blog.netphase.com/2008/02/23/extending-activerecord-with-count_since/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 02:29:15 +0000</pubDate>
		<dc:creator>scottned</dc:creator>
				<category><![CDATA[Ruby On Rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://blog.netphase.com/2008/02/23/extending-activerecord-with-count_since/</guid>
		<description><![CDATA[I just implemented a statistics page for an application and found that I was using this pattern over and over again:

User.count&#40;:conditions =&#62; &#91;'created_at &#62; ?', 30.days.ago&#93;&#41;

Here&#8217;s a simple extension I made to ActiveRecord to DRY it up:

module ActiveRecord
  class Base
      def self.count_since&#40;time_ago&#41;
        [...]]]></description>
			<content:encoded><![CDATA[<p>I just implemented a statistics page for an application and found that I was using this pattern over and over again:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">User.<span style="color:#9900CC;">count</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'created_at &gt; ?'</span>, 30.<span style="color:#9900CC;">days</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#93;</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Here&#8217;s a simple extension I made to ActiveRecord to <abbr title="Don't Repeat Yourself">DRY</abbr> it up:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">module</span> ActiveRecord
  <span style="color:#9966CC; font-weight:bold;">class</span> Base
      <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">count_since</span><span style="color:#006600; font-weight:bold;">&#40;</span>time_ago<span style="color:#006600; font-weight:bold;">&#41;</span>
        count<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:conditions</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'created_at &gt; ?'</span>, time_ago<span style="color:#006600; font-weight:bold;">&#93;</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>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Put that snippet in a file in your lib directory.  I called mine rails_extensions.rb.  Then add <em>require &#8216;rails_extensions&#8217;</em> to the bottom of your environment.rb file.</p>
<p>Now you can just do:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">User.<span style="color:#9900CC;">count_since</span><span style="color:#006600; font-weight:bold;">&#40;</span>30.<span style="color:#9900CC;">days</span>.<span style="color:#9900CC;">ago</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>That&#8217;s a little cleaner, don&#8217;t you think?</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.netphase.com/2008/02/23/extending-activerecord-with-count_since/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
