Interesting read. I like the idea of using blocks instead of ‘if’
statements, it makes your code more declarative. It boils down to is
another layer of abstraction that moves the contract from being
between the view and the model/controller, to being between the view
and the helper module. That is a useful abstraction, as the helper
module will tend to only change when the view does. It also provides
a single point of impact if the under lying condition logic must be
changed. Thanks for the link, I will be adopting this style on future
work.
But a condition is still a condition, and conditionally yielding to a
block to hide an ‘if’ statement is just a fancy, albeit pretty,
condition. The purpose of hiding the condition in a helper is to make
the API more stable. When it comes to APIs of your own creation,
that’s a really useful thing as they tend to change more frequently
than core APIs. But you should be able to count on some things being
their now and in the future, especially when it comes to the
foundational libraries your application is built on. When testing
those types of conditions, I wouldn’t worry about hiding an ‘if’
statement inside a helper unless it was for the sake of readability.
On the subject of a layout, the following seems like readable, non-
fragile code to me:
<% case controller.action_name -%>
<% when ‘edit’ -%>
<%= render :partial => ‘shared/edit’ %>
<% when ‘show’ -%>
<%= render :partial => ‘shared/show’ %>
<% end -%>
Thinking about it further, I think I would put all of that in a
helper. That would make my layout look like this:
<%= shared_action_partial %>
In application_helper.rb I’d have:
def shared_action_partial
render :partial => ‘shared/’ + controller.action_name
rescue
#ignore error if partial does not exist
#re-throw error if it is some other type of error
end
This would seem to be very much in the Rails spirit, convention over
configuration. It adds another convention. If you have a shared/
partial, it will use it, otherwise nothing happens.
Again, thanks for the link.
Ryan
On Sep 13, 3:25 am, Anthony G. [email protected]