<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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>Comments on: A smarter Rails url_for helper</title>
	<atom:link href="http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/feed/" rel="self" type="application/rss+xml" />
	<link>http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/</link>
	<description></description>
	<lastBuildDate>Thu, 14 Mar 2013 03:50:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
	<item>
		<title>By: Anonymous</title>
		<link>http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-252</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 28 Mar 2012 12:27:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-252</guid>
		<description>First, its worth pointing out that I only need to register handlers for the &quot;special case&quot; models. Everything else just falls through to url_for.
The predicate approach gives me more flexibility, but it is possible I won&#039;t need its full power. The class checking will likely be the most common case. Another possibility would be a hybrid approach - first check a hash for the class of the model. If it not found there, check the model against the collection of predicates, and only after that fails, fall through to url_for.</description>
		<content:encoded><![CDATA[<p>First, its worth pointing out that I only need to register handlers for the &#8220;special case&#8221; models. Everything else just falls through to url_for.<br />
The predicate approach gives me more flexibility, but it is possible I won&#8217;t need its full power. The class checking will likely be the most common case. Another possibility would be a hybrid approach &#8211; first check a hash for the class of the model. If it not found there, check the model against the collection of predicates, and only after that fails, fall through to url_for.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark IJbema</title>
		<link>http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-251</link>
		<dc:creator>Mark IJbema</dc:creator>
		<pubDate>Wed, 28 Mar 2012 09:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-251</guid>
		<description>If you have a lot of models this becomes quite expensive though. As far as I can see the lookup is O(n). If your primary usecase is checking the model class, wouldn&#039;t it be better to store the mappings in a hash, so that lookup is constant? (and then per class you could possibly still do another check, if you need to check other stuff than the model class)</description>
		<content:encoded><![CDATA[<p>If you have a lot of models this becomes quite expensive though. As far as I can see the lookup is O(n). If your primary usecase is checking the model class, wouldn&#8217;t it be better to store the mappings in a hash, so that lookup is constant? (and then per class you could possibly still do another check, if you need to check other stuff than the model class)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jak Charlton</title>
		<link>http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-250</link>
		<dc:creator>Jak Charlton</dc:creator>
		<pubDate>Wed, 28 Mar 2012 04:51:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-250</guid>
		<description>If you can use a model of any kind, you can use Draper - it just decorates other models and you just replace their use with its decorator

If your models are just conforming to one generic model, then just create a view model for that use case, which has the generic properties to display, and the url to go to

</description>
		<content:encoded><![CDATA[<p>If you can use a model of any kind, you can use Draper &#8211; it just decorates other models and you just replace their use with its decorator</p>
<p>If your models are just conforming to one generic model, then just create a view model for that use case, which has the generic properties to display, and the url to go to</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-249</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 28 Mar 2012 04:45:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-249</guid>
		<description>Thanks for your feedback, Jak. I didn&#039;t intend this as a patch submission to Rails, so I don&#039;t need everyone to like it. Personally, putting the url generation on the model (which is how we started) was hard for me to stomach. To each his own.
Wrapping each model in a decorator again presumes that you know the model type (I think) so that you know which decorator to wrap it with. I haven&#039;t used Draper yet, but I do agree with its intent. If it has a way to generically wrap a model with the correct decorator, without you explicitly telling it which decorator to use, it may be a possible solution.</description>
		<content:encoded><![CDATA[<p>Thanks for your feedback, Jak. I didn&#8217;t intend this as a patch submission to Rails, so I don&#8217;t need everyone to like it. Personally, putting the url generation on the model (which is how we started) was hard for me to stomach. To each his own.<br />
Wrapping each model in a decorator again presumes that you know the model type (I think) so that you know which decorator to wrap it with. I haven&#8217;t used Draper yet, but I do agree with its intent. If it has a way to generically wrap a model with the correct decorator, without you explicitly telling it which decorator to use, it may be a possible solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jak Charlton</title>
		<link>http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-248</link>
		<dc:creator>Jak Charlton</dc:creator>
		<pubDate>Wed, 28 Mar 2012 04:30:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-248</guid>
		<description>And if you don&#039;t like the idea of using a view only property in your model, you should probably be using view models anyway, I suggest Draper, where you can then implement view only logic about models
https://github.com/jcasimir/draper
</description>
		<content:encoded><![CDATA[<p>And if you don&#8217;t like the idea of using a view only property in your model, you should probably be using view models anyway, I suggest Draper, where you can then implement view only logic about models<br />
<a href="https://github.com/jcasimir/draper" rel="nofollow">https://github.com/jcasimir/draper</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jak Charlton</title>
		<link>http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-247</link>
		<dc:creator>Jak Charlton</dc:creator>
		<pubDate>Wed, 28 Mar 2012 04:28:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-247</guid>
		<description>Then ask the model

class Comment  def url_for      ... whatever ...  endend

Personal opinion, I really dislike your current solution - overly complicated for a really simple thing 

Your assertion that &quot;This is what url_for is supposed to do&quot; is just wrong. There is a well founded presumption that the view knows the properties model structure it is displaying (as you would for knowing the fields for a form for example).

If your view is that generic that it is only asking about a few properties of a model via a protocol (say &quot;title&quot; and url link), then implement a method as in the class above - models that implement this protocol can be displayed 



</description>
		<content:encoded><![CDATA[<p>Then ask the model</p>
<p>class Comment  def url_for      &#8230; whatever &#8230;  endend</p>
<p>Personal opinion, I really dislike your current solution &#8211; overly complicated for a really simple thing </p>
<p>Your assertion that &#8220;This is what url_for is supposed to do&#8221; is just wrong. There is a well founded presumption that the view knows the properties model structure it is displaying (as you would for knowing the fields for a form for example).</p>
<p>If your view is that generic that it is only asking about a few properties of a model via a protocol (say &#8220;title&#8221; and url link), then implement a method as in the class above &#8211; models that implement this protocol can be displayed </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-246</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 28 Mar 2012 04:19:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-246</guid>
		<description>The *call site* does not know the model. Yes, somewhere in my application, that knowledge exists (the config/initializer). But the point at which I need to generate a URL, I do not know the model. 
I have a method that returns a collection of models. Models of different types. I want to display a list of links to those models. The code that iterates over the models doesn&#039;t know, and shouldn&#039;t have to care, what the models actually are. It just wants to ask Rails for their URL. That is what url_for is supposed to do. But it doesn&#039;t in some circumstances, like nested resources.</description>
		<content:encoded><![CDATA[<p>The *call site* does not know the model. Yes, somewhere in my application, that knowledge exists (the config/initializer). But the point at which I need to generate a URL, I do not know the model. <br />
I have a method that returns a collection of models. Models of different types. I want to display a list of links to those models. The code that iterates over the models doesn&#8217;t know, and shouldn&#8217;t have to care, what the models actually are. It just wants to ask Rails for their URL. That is what url_for is supposed to do. But it doesn&#8217;t in some circumstances, like nested resources.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jak Charlton</title>
		<link>http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-245</link>
		<dc:creator>Jak Charlton</dc:creator>
		<pubDate>Wed, 28 Mar 2012 04:11:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-245</guid>
		<description>In what example wouldn&#039;t you know what the model was?

Your solution knows, you just moved it into a helper

If (and I have no idea why you would want to) you wanted to avoid knowing what the parent object was, you could just implement a method on comment and use that

class Comment
 def parent
    post
 end
end

url_for [@comment.parent, @@comment:disqus ]

</description>
		<content:encoded><![CDATA[<p>In what example wouldn&#8217;t you know what the model was?</p>
<p>Your solution knows, you just moved it into a helper</p>
<p>If (and I have no idea why you would want to) you wanted to avoid knowing what the parent object was, you could just implement a method on comment and use that</p>
<p>class Comment<br />
 def parent<br />
    post<br />
 end<br />
end</p>
<p>url_for [@comment.parent, @@comment:disqus ]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous</title>
		<link>http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-244</link>
		<dc:creator>Anonymous</dc:creator>
		<pubDate>Wed, 28 Mar 2012 04:07:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-244</guid>
		<description>I don&#039;t know, I&#039;m asking you!
But the example you provided and linked to is NOT a solution to my problem. You know that you have a comment, so you know you have to also pass a post to get the url. I&#039;m referring to a situation where you don&#039;t know what the model is. The problem I&#039;m trying to solve is when you have generic code that needs to be able to handle any model, and generate a url.</description>
		<content:encoded><![CDATA[<p>I don&#8217;t know, I&#8217;m asking you!<br />
But the example you provided and linked to is NOT a solution to my problem. You know that you have a comment, so you know you have to also pass a post to get the url. I&#8217;m referring to a situation where you don&#8217;t know what the model is. The problem I&#8217;m trying to solve is when you have generic code that needs to be able to handle any model, and generate a url.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jak Charlton</title>
		<link>http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-243</link>
		<dc:creator>Jak Charlton</dc:creator>
		<pubDate>Wed, 28 Mar 2012 03:56:00 +0000</pubDate>
		<guid isPermaLink="false">http://lostechies.com/joshuaflanagan/2012/03/27/a-smarter-rails-url_for-helper/#comment-243</guid>
		<description>Aren&#039;t you trying to recreate built in logic?

url_for [@project, @comment]

http://ryanbigg.com/2012/03/polymorphic-routes/

</description>
		<content:encoded><![CDATA[<p>Aren&#8217;t you trying to recreate built in logic?</p>
<p>url_for [@project, @comment]</p>
<p><a href="http://ryanbigg.com/2012/03/polymorphic-routes/" rel="nofollow">http://ryanbigg.com/2012/03/polymorphic-routes/</a></p>
]]></content:encoded>
	</item>
</channel>
</rss>
