generating styled text in a model

I want to convert analytical results (i.e. numbers) into prose (i.e. English sentences). In addition to generating the words, I also want to be able to style bits of the text (presumably by decorating them with css classes).

For example, assume something contrived like:

def cholesterol_sentence(my_model)    "Your total cholesterol level is     #{my_model.total_cholesterol}, which is considered     #{cholesterol_style(my_model.total_cholesterol)}." end

def cholesterol_style(total_cholesterol)   if (total_cholesterol < 200)     content_tag(:div, "low", :class => 'safe')   elsif (total_cholesterol < 240)     content_tag(:div, "average")   else     content_tag(:div, "high", :class => 'danger')   end end

Note that I need to generate several paragraphs of text, not just a single sentence.

I don't think the logic belongs in a view -- the logic is rather complex and it would be unwieldy to write it in erb or the DSL du jour. It could go in a helper, but that seems mighty heavy-weight for what's supposed to be short, simple methods.

The only other option I see is to put the logic in a model, which seems okay except that models don't have access to ActionView helpers (e.g. content_tag).

What do the experienced railers suggest?

- ff

I'm not all THAT experienced a railer, but offhand I'd say:

- The logic of what is high or low would indeed belong in a model.

- The controller could then just pass that to the view, which could then decide how to style it.

Yes this means that the determination of the range is decoupled from the *naming* of that range. That's actually good for localization anyway. :slight_smile:

So maybe in the Cholesterol model:

  # if this were C this would be an enum;   # given that we're just using them to index into hashes,   # it doesn't really matter if they're numbers, strings, symbols, whatever.   # There may be some more canonical Rubyish way to do this....   LOW = :low   AVERAGE = :average   HIGH = :high

  def self.range(level)     if (level < 200)       LOW     elif (level < 240)       AVERAGE     else       HIGH     end   end

  # one could argue that this belongs in the view   def self.range_names = {     LOW: 'low',     AVERAGE: 'average',     HIGH: 'high'   }

which would be called in the controller as:

  @level = my_model.total_cholesterol   @range = Cholesterol.range @level   @range_word = Cholesterol.range_names[@range]

which would be passed to the view, which would do something like:

  <% cholesterol_div_classes = {     Cholesterol::LOW: 'safe',     Cholesterol::AVERAGE: '',     Cholesterol::HIGH: 'danger'    } %>

  <div class="<%= cholesterol_div_classes[@range] %>">     Your total cholesterol level is <%= @level %>,     which is considered <%= @range_word %>.   </div>

And of course the "safe" and "danger" classes of div would then trigger stuff in the CSS.

I'd still like to see what the gurus think of this.

Thanks, Dave