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