<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Dynamic and Concise &#187; view</title>
	<atom:link href="http://yiwenandsoftware.wordpress.com/tag/view/feed/" rel="self" type="application/rss+xml" />
	<link>http://yiwenandsoftware.wordpress.com</link>
	<description>Everything about my experience with Software Development</description>
	<lastBuildDate>Sat, 14 Mar 2009 22:05:25 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='yiwenandsoftware.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/fa9ad49ca1c5d02890788e46a95fe979?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Dynamic and Concise &#187; view</title>
		<link>http://yiwenandsoftware.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://yiwenandsoftware.wordpress.com/osd.xml" title="Dynamic and Concise" />
		<item>
		<title>More on form_for(@non_arb)</title>
		<link>http://yiwenandsoftware.wordpress.com/2008/03/14/more-on-form_fornon_arb/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2008/03/14/more-on-form_fornon_arb/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 13:59:56 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[presenter]]></category>
		<category><![CDATA[ruby o rails]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/?p=25</guid>
		<description><![CDATA[My previous post talked about how to make form_for(@non_arb) work. When we need it for multiple classes, we came up with a little bit meta-programming so that classes that needs such ability can simply say:

  polymorphic_routes_for :person,
                 :update_path [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=25&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>My <a href="http://yiwenandsoftware.wordpress.com/2008/03/03/form_fornon_arb/">previous post</a> talked about how to make form_for(@non_arb) work. When we need it for multiple classes, we came up with a little bit meta-programming so that classes that needs such ability can simply say:<br />
<code><br />
  polymorphic_routes_for :person,<br />
                 :update_path =&gt; :admin_person_path,<br />
                 :create_path =&gt; :admin_people_path<br />
</code></p>
<p>As you can see, it also takes options, <i>update_path</i> and <i>create_path</i>, if you don&#8217;t specify the paths, it will infer from the first param, which would make update path to person_path and create path to people_path.</p>
<p>The magic behind the scene is in the following, it can be in a module that you can mix in, or in our situation, in a superclass, <i>Presenter</i> where all presenters inherit from.</p>
<pre class="brush: ruby;">
class Presenter
  class &lt;&lt; self
    def polymorphic_routes_for(symbol, options = {})
      self.class_eval &quot;def id; #{symbol}.id; end&quot;
      self.class_eval &quot;def to_param; #{symbol}.to_param; end&quot;
      self.class_eval &quot;def new_record?; #{symbol}.new_record?; end&quot;
      update_path = options[:update_path] || &quot;#{symbol}_path&quot;
      ActionView::Base.send :alias_method, &quot;#{self.name.underscore}_path&quot;, update_path

      create_path = options[:create_path] || &quot;#{symbol.to_s.pluralize}_path&quot;
      ActionView::Base.send :alias_method, &quot;#{self.name.underscore.pluralize}_path&quot;, create_path
    end

  end
</pre>
<p>and here is the specs for it:</p>
<pre class="brush: ruby;">
describe Presenter, &quot;when specifying the polymorphic routes for a model&quot; do
  before :each do
    ActionView::Base.class_eval(&quot;def test_model_path;end&quot;)
    ActionView::Base.class_eval(&quot;def test_models_path;end&quot;)
    class A &lt; Presenter
      polymorphic_routes_for :test_model
      attr_reader :test_model
    end

    @presenter = A.new :test_model =&gt; mock(&quot;Model&quot;)
  end

  it &quot;should generate id method&quot; do
    @presenter.test_model.should_receive(:id).and_return &quot;id&quot;
    @presenter.id.should == &quot;id&quot;
  end

  it &quot;should generate to_param method&quot; do
    @presenter.test_model.should_receive(:to_param).and_return &quot;to param&quot;
    @presenter.to_param.should == &quot;to param&quot;
  end

  it &quot;should generate new_record? method&quot; do
    @presenter.test_model.should_receive(:new_record?).and_return &quot;new record&quot;
    @presenter.new_record?.should == &quot;new record&quot;
  end

  it &quot;should alias update named route method to ActionView::Base&quot; do
    ActionView::Base.new.should respond_to(:a_path)
  end

  it &quot;should alias create named route method to ActionView::Base&quot; do
    ActionView::Base.new.should respond_to(:as_path)
  end
end

describe Presenter, &quot;when specifying the polymorphic routes for a model and specifying update_path option&quot; do
  before :each do
    ActionView::Base.class_eval(&quot;def test_update_path; 'customized update path'; end&quot;)
    ActionView::Base.class_eval(&quot;def test_models_path;end&quot;)

    class A &lt; Presenter
      polymorphic_routes_for :test_model, :update_path =&gt; :test_update_path
      attr_reader :test_model
    end

    @presenter = A.new :test_model =&gt; mock(&quot;Model&quot;)
  end

  it &quot;should alias update named route method given by update_path option to ActionView::Base&quot; do
    ActionView::Base.new.a_path.should == 'customized update path'
  end

end

describe Presenter, &quot;when specifying the polymorphic routes for a model and specifying create_path option&quot; do
  before :each do
    ActionView::Base.class_eval(&quot;def test_model_path; end&quot;)
    ActionView::Base.class_eval(&quot;def test_create_path; 'customized create path'; end&quot;)

    class A &lt; Presenter
      polymorphic_routes_for :test_model, :create_path =&gt; :test_create_path
      attr_reader :test_model
    end

    @presenter = A.new :test_model =&gt; mock(&quot;Model&quot;)
  end

  it &quot;should alias create named route method given by update_path option to ActionView::Base&quot; do
    ActionView::Base.new.as_path.should == 'customized create path'
  end

end
</pre>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/25/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/25/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/25/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/25/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/25/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=25&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2008/03/14/more-on-form_fornon_arb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
		<item>
		<title>form_for(@non_arb)</title>
		<link>http://yiwenandsoftware.wordpress.com/2008/03/03/form_fornon_arb/</link>
		<comments>http://yiwenandsoftware.wordpress.com/2008/03/03/form_fornon_arb/#comments</comments>
		<pubDate>Tue, 04 Mar 2008 05:07:54 +0000</pubDate>
		<dc:creator>Yi Wen</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[ruby o rails]]></category>
		<category><![CDATA[view]]></category>

		<guid isPermaLink="false">http://yiwenandsoftware.wordpress.com/?p=24</guid>
		<description><![CDATA[In an erb template, you can have your form written as:

     form_for(@arb)

where @arb is a ActiveRecord::Base object. form_for will smartly realize which url to generate based-on @arb&#8217;s state. For example, if I have

 form_for(@person)

in the template and @person is a new record, the form_for helper will correctly infer that this form [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=24&subd=yiwenandsoftware&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>In an erb template, you can have your form written as:<br />
<code></p>
<p>     form_for(@arb)<br />
</code><br />
where @arb is a ActiveRecord::Base object. <i>form_for</i> will smartly realize which url to generate based-on @arb&#8217;s state. For example, if I have<br />
<code></p>
<p> form_for(@person)<br />
</code><br />
in the template and @person is a new record, the <i>form_for</i> helper will correctly infer that this form is for creating, thus generates the form submission url as <i>people_path</i> with POST. And if @person is not a new record, the helper will generate the url as <i>person_path(@person)</i> with a &#8220;PUT&#8221;. Benefits you can get from this are less code, and you can even merge new and edit forms all together.</p>
<p>In one instance, we want to use <a href="http://blog.jayfields.com/">Jay Fields&#8217;</a> <a href="http://blog.jayfields.com/2007/03/rails-presenter-pattern.html">Presenter Pattern</a>. So instead of @person, we use @person_presenter, and we still want to keep the simple syntax on the view, like <code>form_for(@person_presenter)</code>. To make it work, we need to do the followings:</p>
<p>In your PersonPresenter class, we need to forward <i>new_record?<i> and <i>to_param</i> to person, something like this, or just use Ruby&#8217;s Forwardable.</p>
<p><code></p>
<p>def new_record?<br />
  person.new_record?<br />
end<br />
</code><br />
<code></p>
<p>def to_param<br />
   person.to_param<br />
end<br />
</code></p>
<p>Now the <i>form_for</i> method will generate <i>person_presenters_path</i> and <i>person_presenter_path</i> respectively. So we need to add two routes in routes.rb:</p>
<p><code><br />
  map.person_presenters 'people', :controller =&gt; 'people', :action =&gt; 'create', :conditions =&gt; { :method =&gt; :post }<br />
  map.person_presenter 'people/:id', :controller =&gt; 'people', :action =&gt; 'update', :conditions =&gt; { :method =&gt; :put }<br />
</code></p>
<p>Now the system works just fine.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/yiwenandsoftware.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/yiwenandsoftware.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/yiwenandsoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/yiwenandsoftware.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/yiwenandsoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/yiwenandsoftware.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/yiwenandsoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/yiwenandsoftware.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/yiwenandsoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/yiwenandsoftware.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/yiwenandsoftware.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/yiwenandsoftware.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=yiwenandsoftware.wordpress.com&blog=2109631&post=24&subd=yiwenandsoftware&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://yiwenandsoftware.wordpress.com/2008/03/03/form_fornon_arb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/f711c24e96cbc441b0c94141f22f2d23?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">hayafirst</media:title>
		</media:content>
	</item>
	</channel>
</rss>