On Sat, Jun 6, 2009 at 6:34 PM, Charlie B.[email protected]
I consider an if statement in the view layer a bug.
Perhaps we can consider it a possible code smell? It’s not really a
bug unless it’s producing incorrect or unexpected result in the
I often need to
conditionally render a partial and a helper is a great way to construct that
condition. Im currently stubing the call to render but I only like stub
when absolutely necessary.
Helpers can be a great way to organize some of the view’s
implementation, although my goal isn’t to move every conditional out
of the view, it’s to avoid having unnecessary logic in the view. When
I work outside-in I’m able to push logic that doesn’t belong in the
view into a presenter or further down to a model with a good method
name. The logic that is left in the view is very minimal and
ultra-simple. I find it easy to spec and it doesn’t impede the life or
maintainability of the view.
For example, if I need to display a piece of information for an admin,
but not a normal user then I have no problem doing the “if
current_user.admin?” check in a view:
<% if current_user.admin? %>
Foo bar baz
<% end %>
The check itself is already ultra-simple and it reads very well. What
value would we get from moving this to a helper method? It’s currently
easy to open the view and know that “Foo bar baz” will only be
rendered for an admin. Does moving this to
“display_foo_bar_baz_for_admin” really give us anything?
There’s a cost to creating a helper method for every one-off
condition. There’s also a cost for turning every snippet of markup
that shows up in a simple view condition into a partial. The cost is
in the disconnect and separation that comes from doing separating
these pieces that go together as well as the organization nightmare of
a plethora of helper methods and partials.
I like extracting helpers when it provides more value than burden.
When it doesn’t I will leave the small, ultra-simple logic in the
rspec-users mailing list
http://www.mutuallyhuman.com (hire me)
http://ideafoundry.info/behavior-driven-development (first rate BDD