0

I have a status on one of the models, and want to make it right. Here is a current code version:

  self::TEST_NONE = 0
  self::TEST_PENDING = 1
  self::TEST_DONE = 2

  def self.test_status_name name
    case name
      when self::TEST_NONE
        'None'
      when self::TEST_PENDING
        'Pending'
      when self::TEST_DONE
        'Done'
      else
        'None'
    end
  end

And in the view

<%= Device::test_status_name @device.test_status %>

Which works in general but feels wrong. I want to implement something like a default to_s method for test_status. Additional class could work (not sure how to implement it right), but I don't need and want AR-backed model there, just several constant statuses.

1 Answer 1

2

Why not something like

class Device
  TEST_STATUSES = %w{ None Pending Done }

  def test_status_name
    test_status ? TEST_STATUSES[test_status] : 'None'
  end
end

<%= @device.test_status_name %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.