On Thu, Nov 17, 2011 at 20:22, Fearless Fool <[email protected]> wrote:

> What do the experienced railers suggest?

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.  :-)

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

-- 
LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages.
Where: Northern Virginia, Washington DC (near Orange Line), and remote work.
See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence).
Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to