<?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>Rails Garden &#187; Rails Plugins</title>
	<atom:link href="http://www.railsgarden.com/category/rails-plugins/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.railsgarden.com</link>
	<description>A delicious blend of Ruby and Rails...</description>
	<lastBuildDate>Mon, 06 Apr 2009 05:05:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>ActiveRecord Drafts with has_draft</title>
		<link>http://www.railsgarden.com/2008/12/30/activerecord-drafts-with-has_draft/</link>
		<comments>http://www.railsgarden.com/2008/12/30/activerecord-drafts-with-has_draft/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 22:28:21 +0000</pubDate>
		<dc:creator>benhughes</dc:creator>
				<category><![CDATA[Rails General]]></category>
		<category><![CDATA[Rails Plugins]]></category>
		<category><![CDATA[Rails Tools]]></category>

		<guid isPermaLink="false">http://www.railsgarden.com/2008/12/30/activerecord-drafts-with-has_draft/</guid>
		<description><![CDATA[I ran into a problem a while back of creating draft copies of ActiveRecord models for the purpose of establishing a draft/live system.  I&#8217;ve since found a reason to resurrect this and publish it to GitHub and clean some things up.  Check out has_draft on GitHub.
has_draft allows for multiple &#8220;drafts&#8221; of a model [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into a problem a while back of creating draft copies of ActiveRecord models for the purpose of establishing a draft/live system.  I&#8217;ve since found a reason to resurrect this and publish it to GitHub and clean some things up.  Check out <a href="https://github.com/railsgarden/has_draft/tree" title="has_draft">has_draft on GitHub</a>.</p>
<p>has_draft allows for multiple &#8220;drafts&#8221; of a model which can be useful when developing:</p>
<ul>
<li>Draft/Live Version of Pages, for examples</li>
<li>A workflow system whereby a live copy may need to be active while a draft copy is awaiting approval.</li>
</ul>
<p>The semantics of this as well as most of the inspiration comes from version_fu, an excellent plugin for a similar purpose of maintaining several &#8220;versions&#8221; of a model.</p>
<p>This was built to be able to be tacked on to existing models, so the data schema doesn&#8217;t need to change at all for the model this is applied to.  As such, drafts are actually stored in a nearly-identical table and there is a has_one relationship to this.  This separation allows the base model to really be treated just as before without having to apply conditions in queries to make sure you are really getting the &#8220;live&#8221; (non-draft) copy: Page.all will still only return the non-draft pages.  This separate table is backed by a model created on the fly as a constant on the original model class.  For example if a Page has_draft, a Page::Draft class will exist as the model for the page_drafts table.</p>
<p><strong>Basic Example:</strong>:</p>
<pre class="rails" name="code">
## First Migration (If Creating base model and drafts at the same time):
class InitialSchema &lt; ActiveRecord::Migration

  [:articles, :article_drafts].each do |table_name|
    create_table table_name, :force =&gt; true do |t|
      t.references :article if table_name == :article_drafts

      t.string :title
      t.text :summary
      t.text :body
      t.date :post_date
    end
  end
end

## Model Class
class Article &lt; ActiveRecord::Base
  has_draft
end

## Exposed Class Methods &amp; Scopes:
Article.draft_class
=&gt; Article::Draft
Article.with_draft.all
=&gt; (Articles that have an associated draft)
Article.without_draft.all
=&gt; (Articles with no associated draft)

## Usage Examples:
article = Article.create(
  :title =&gt; "My Title",
  :summary =&gt; "Information here.",
  :body =&gt; "Full body",
  :post_date =&gt; Date.today
)

article.has_draft?
=&gt; false

article.instantiate_draft!

article.has_draft?
=&gt; true

article.draft
=&gt; Article::Draft Instance

article.draft.update_attributes(
  :title =&gt; "New Title"
)

article.replace_with_draft!

article.title
=&gt; "New Title"

article.destroy_draft!

article.has_draft?
=&gt; false</pre>
<p><strong>Custom Options:</strong>:</p>
<pre class="rails" name="code">
## First Migration (If Creating base model and drafts at the same time):
class InitialSchema &lt; ActiveRecord::Migration

  [:articles, :article_copies].each do |table_name|
    create_table table_name, :force =&gt; true do |t|
      t.integer :news_article_id if table_name == :article_copies

      t.string :title
      t.text :summary
      t.text :body
      t.date :post_date
    end
  end

end

## Model Class
class Article &lt; ActiveRecord::Base
  has_draft :class_name =&gt; 'Copy', :foreign_key =&gt; :news_article_id, :table_name =&gt; 'article_copies'
end</pre>
<p><strong>Method Callbacks</strong>:<br />
There are three callbacks you can specify directly as methods.</p>
<pre class="rails" name="code">
class Article &lt; ActiveRecord::Base
  has_draft

  def before_instantiate_draft
    # Do Something
  end

  def before_replace_with_draft
    # Do Something
  end

  def before_destroy_draft
    # Do Something
  end
end</pre>
<p><strong>Block of Code Run for Draft Class</strong>:<br />
Because you don&#8217;t directly define the draft class, you can specify a block of code to be run in its<br />
context by passing a block to has_draft.</p>
<pre class="rails" name="code">
class Article &lt; ActiveRecord::Base
  belongs_to :user

  has_draft do
    belongs_to :last_updated_user

    def approve!
      self.approved_at = Time.now
      self.save
    end
  end

end</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.railsgarden.com/2008/12/30/activerecord-drafts-with-has_draft/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>message_block: a error_messages_for replacement for flash message and model error handling</title>
		<link>http://www.railsgarden.com/2008/11/26/message_block-a-error_messages_for-replacement-for-flash-message-and-model-error-handling/</link>
		<comments>http://www.railsgarden.com/2008/11/26/message_block-a-error_messages_for-replacement-for-flash-message-and-model-error-handling/#comments</comments>
		<pubDate>Wed, 26 Nov 2008 22:54:25 +0000</pubDate>
		<dc:creator>benhughes</dc:creator>
				<category><![CDATA[Rails Plugins]]></category>
		<category><![CDATA[Rails Techniques]]></category>

		<guid isPermaLink="false">http://www.railsgarden.com/2008/11/26/message_block-a-error_messages_for-replacement-for-flash-message-and-model-error-handling/</guid>
		<description><![CDATA[
One of the most common needs in any application I build is to have some abstract way of handling messages to end users.  Sometimes I&#8217;ll want to show a confirmation message or a warning.  Other times I&#8217;ll want to show a confirmation message but also show ActiveRecord validations.  While the error_messages_for helper in Rails works [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.railsgarden.com/wp-content/uploads/2009/01/message_block_example1.png" alt="Message Block Example" style="float: right; margin-left: 12px; margin-bottom: 10px" /></p>
<p>One of the most common needs in any application I build is to have some abstract way of handling messages to end users.  Sometimes I&#8217;ll want to show a confirmation message or a warning.  Other times I&#8217;ll want to show a confirmation message but also show ActiveRecord validations.  While the error_messages_for helper in Rails works fairly well for showing ActiveRecord validation issues, I wanted a unified approach to handling this and flash messaging with multiple flash types in one package.</p>
<p>I <a href="http://www.railsgarden.com/2007/12/11/user-alert-management-with-flash-and-activerecorderrors/">blogged before</a> about an approach I developed to solve this problem, I <a href="http://github.com/railsgarden/message_block" title="message_block Rails Plugin">rolled my own message_block plugin</a>.  The README file explains things pretty so be sure to check it out for more details, but here is the intro:</p>
<p><strong>Introduction</strong>:</p>
<p>Implements the common view pattern by which a list of messages are shown at the top, often a combination of flash messages and ActiveRecord validation issues on one or more models. This allows for a nice, stylized block of messages at the top of the page with icons indicating what type of message it is (error, confirmation, warning, etc.)</p>
<p>This view helper acts as a replacement for error_messages_for by taking error messages from your models and combing them with flash messages (multiple types such as error, confirm, etc.) and outputting them to your view. This plugin comes with an example stylesheet and images.</p>
<p><strong>Usage</strong>:</p>
<p>Once you install this, you should now have a set of images at public/images/message_block and a basic stylesheet installed at public/stylesheets/message_block.css. First you’ll want to either reference this in your layout or copy the declarations to your main layout. Then you can use the helper <tt>&lt;%= message_block %&gt;</tt> as described below:</p>
<p>The first argument specifies a hash options:</p>
<ul>
<li><tt> <img src='http://www.railsgarden.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> n</tt> &#8211; specifies one or many model names for which to check error messages.</li>
<li><tt>:model_error_type</tt> &#8211; specifies the message type to use for validation errors; defaults to ‘error’</li>
<li><tt>:flash_types</tt> &#8211; specifies the keys to check in the flash hash. Messages will be grouped in ul lists according to this type. Defaults to: %w(back confirm error info warn)</li>
<li><tt>:html</tt> &#8211; Specifies HTML options for the containing div</li>
<li><tt>:id</tt> &#8211; Specifies ID of the containing div; defaults to ‘message_block’</li>
<li><tt>:class</tt> &#8211; Specifies class name of the containing div; defaults to nothing.</li>
</ul>
<p>Imagine you have a form for entering a user and a comment:</p>
<pre name="code" class="rails">
&lt;%= message_block <img src='http://www.railsgarden.com/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> n =&gt; [:user, :comment] %&gt;</pre>
<p>Imagine also you set these flash variables in the controller:</p>
<pre name="code" class="rails">
  class CommentsController
    def create
      flash.now[:error] = "Error A"
      flash.now[:confirm] = "Confirmation A"  # Note you can use different types
      flash.now[:warn] = ["Warn A", "Warn B"]  # Can set to an array for multiple messages
    end
  end</pre>
<p>And let’s say that you want to show these messages but also show the validation issues given that both user and comment fail ActiveRecord validation:</p>
<pre name="code" class="html">
  &lt;div id="message_block"&gt;
    &lt;ul class="error"&gt;
      &lt;li&gt;Error A&lt;/li&gt;
      &lt;li&gt;User first name is required.&lt;/li&gt;
      &lt;li&gt;Comment contents is required.&lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul class="confirm"&gt;
      &lt;li&gt;Confirmation A&lt;/li&gt;
    &lt;/ul&gt;
    &lt;ul class="warn"&gt;
      &lt;li&gt;Warn A&lt;/li&gt;
      &lt;li&gt;Warn B&lt;/li&gt;
    &lt;/ul&gt;
  &lt;/div&gt;</pre>
<p>Which will by default leave you with this look:</p>
<p><img src="http://www.railsgarden.com/wp-content/uploads/2009/01/message_block_example.png" alt="Message Block Example" /></p>
<p><a href="http://github.com/railsgarden/message_block" title="message_block on GitHub"><strong>message_block on GitHub</strong></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsgarden.com/2008/11/26/message_block-a-error_messages_for-replacement-for-flash-message-and-model-error-handling/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Nested URL Parameters</title>
		<link>http://www.railsgarden.com/2007/12/20/nested-url-parameters/</link>
		<comments>http://www.railsgarden.com/2007/12/20/nested-url-parameters/#comments</comments>
		<pubDate>Thu, 20 Dec 2007 15:07:12 +0000</pubDate>
		<dc:creator>benhughes</dc:creator>
				<category><![CDATA[Rails General]]></category>
		<category><![CDATA[Rails Plugins]]></category>
		<category><![CDATA[Rails Techniques]]></category>

		<guid isPermaLink="false">http://www.railsgarden.com/2007/12/20/nested-url-parameters/</guid>
		<description><![CDATA[One of the great things about Rails is the ability to wire together form logic with extreme ease through Rails’ support of essentially representing hashes through the “object[name]” syntax of URL parameters. Arrays are also supported in a similar manner, making things like many-to-many relationship management cake.
In Rails 1.2 one issue I ran into is [...]]]></description>
			<content:encoded><![CDATA[<p>One of the great things about Rails is the ability to wire together form logic with extreme ease through Rails’ support of essentially representing hashes through the “object[name]” syntax of URL parameters. Arrays are also supported in a similar manner, making things like many-to-many relationship management cake.</p>
<p>In Rails 1.2 one issue I ran into is that this hash-based access logic cannot be nested when using helpers such as url_for (which is in turned used by helpers such as link_to, etc.) This type of functionality is rather useful when you are trying to encapsulate a set of name-value pairs within one “thing”, such as a “search” which is many criteria =&gt; value pairs. For a particularly project of mine I established a pretty nice design pattern for working with searches and results in a very abstract manner, an area of Rails that I generally find underdeveloped with no standard practice.</p>
<p>I was lucky to stumble on a Rails plugin that monkey-patches Rails to support this: nested_params_patch. Information about this plugin is quite sparse but this article does a good job of better-explaining what the plugin does. Essentially it allows you to do things like this:</p>
<pre name="code" class="rails">
person_url(:name =&gt; {0 =&gt; 'Ryan', 1 =&gt; 'Kinderman})
</pre>
<p>Pretty cool stuff! I am always amazed and the kind of plugins and extensions that are possible, all resulting from Ruby’s dynamicism and Rails’ heavily extensible architecture.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.railsgarden.com/2007/12/20/nested-url-parameters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
