Search code examples
ruby-on-railsmethodsstates

Rails 4 - Statesman Gem -method to define current state


I'm trying to make an app in Rails 4 and using Statesman for states.

In my project model, I want to display the :current_state of an object. However, I don't want the name of the attribute to appear. Instead, I want to write a human friendly state that can be rendered if the object is in the corresponding state.

For example, I have defined a state called :request_approval.

In my projects show page, I can write:

<%= @project.current_state %>

and the output is request_approval.

How can I write something that says if project is in current_state :request_approval, render: Awaiting a response to your request for approval? Can I make some kind of method in my model to do that?


Solution

  • In your projects_helper you can write a method:

    def text_for_state(state)
      case state
      when 'request_approval'
        'Awaiting Response'
      when 'something_else'
        'Then Something Else'
      end
    end
    

    And in your view just write:

    <%= text_for_state(@project.current_state) %>
    

    This is a option there may be better solutions to do this.