Need a thumbs up or a thumbs down...

I have a very simple idea, and need a simple "yes thats a good idea" or "no, thats a bad idea, heres why". I want to create a disp helper method to display ActiveRecord objects. Quick example:

<%= disp(@user, :type => :multi_line_html) %>

This would produce:

<strong><%= link_to "benj0323", user_profile_url(:id => user.id) %></strong><br /> Ben Johnson<br /> Registered on: Jan 1, 2008

or I could do:

<%= disp(@user, :type => :single_line_text) %>

This would produce:

benj0323 - Ben Johnson - Registered on: Jan 1, 2008

All that I want is a helper method to display the state / information about an object. I simply specify a type depending on how I want the data formatted. This is nice because I can instantly change how users are displayed throughout the software if I need. A good example is if we add privacy featured to the application, and a user can specify that they do NOT want their name show. So I would go to that one file and add in the conditional check to display their name or not, not this feature is effective throughout the whole software.

What do you think? Good idea or bad idea? Or is there a better way to conquer the situation I described above?

Thanks for your help.

It sounds like you just need two view helpers (or partials). I'm not a big fan of passing in a 'type' to a helper in this case. Mainly because you'll need to have a conditional in the helper, which could just as well be two separate helpers. I'm not a big fan of using much html in helpers and usually delegate that to a partial.

Robby

I have a very simple idea, and need a simple "yes thats a good idea" or "no, thats a bad idea, heres why". I want to create a disp helper method to display ActiveRecord objects. Quick example:

<%= disp(@user, :type => :multi_line_html) %>

[...]

or I could do:

<%= disp(@user, :type => :single_line_text) %>

[...]

What do you think? Good idea or bad idea? Or is there a better way to conquer the situation I described above?

It's a common idea, but there are two better approaches. One is having a set of modules that parallel one's models, much the way helpers parallel one's controllers, that provide methods on the model so it knows how to render itself. I swear there is a plugin to handle this and automatically extend models before views run, but I can't remember the name. (It also wouldn't be that hard to write, though it could be a challenge to make it both dependable and efficient.)

The other, much simpler, approach is to have appropriate partials for each, e.g.:

<%= render :partial => '/shared/user/multi_line_html', :object => @user %>

or

<%= render :partial => '/shared/user/single_line_text', :object => @user %>

Thanks for your help.

--Greg