I am working on a project that uses quite a bit of conditional logic
to display information about groups in a list. The partial that
renders the group attributes will display different info based on
whether the user is a) logged in, b) the creator of the group c) a
member of the group, etc.
I find myself using a lot of if/else conditions to render things as
they should be. The thing I dislike the most is that I'm writing the
same logic in different places. For instance, if a user is an admin
I'm doing:
<% if current_user.admin? %>
something...
<% end %>
...and I do that two or three times in the same template. What are
some other strategies. Can I use respond_to? How do people make
their view code more readable? If there are any good resources people
know about for this, please respond!
Definitely take a look at Florian Hanke's representer-plugin at http://github.com/floere/representer. It gives you the ability to have
'object oriented' views. You put all the logic (conditionals etc.)
inside methods of the view-object. Much much easier to test and it
results in extra clean views.
If you need more info (in case the documentation is not enough), let
me know.