Strategies for streamlining view code

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!

Thanks, -A

Take a look at: http://github.com/stffn/declarative_authorization

Darian Shimy

For simple cases you could just create a helper like this: def admin_only &blk    yield if current_user.admin? end

<% admin_only do %>   some ISH <% end %>

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.

Andi

or a helper like: def show_user_info if (current_user.admin?) do ‘User is admin.’ elsif (current_user.loggedin?) do ‘User is logged in.’ else

'Another case'

end end

and on the templates simply: <%= show_user_info %>

regards