Szilard Magyar Szilard Magyar - 2 years ago 124
Ruby Question

ruby string formatting in rails

So the goal is to turn for instance

, which comes from the class, into
"product customer"

I used to have this:


It didn't work out of course, since if the
it breaks. I don't
want to use
or something similar since it can be solved with using

So now I changed to this:

notification.notifiable_type.split(/(?=[A-Z])/).join(' ').downcase

But this is way too complex to use every time in the view. So either I would like to define this as a view helper or using some ruby formatting method if there is a simple one.

Can somebody tell me what the Rails convention is in this case? If it's a helper, how does the method looks like and where should I put it?

Answer Source




module MyModule
  def human_model_name

ActiveRecord::Base.send(:include, MyModule)

Including MyModule in ActiveRecord::Base will add human_model_name in all ActiveRecord instances. So, you will be able to do...

user.human_model_name #=> user
notification.human_model_name #=> notification
notification.notifiable.human_model_name #=> product customer
any_active_record_instance.human_model_name #=> etc.

To avoid exceptions when notifiable is nil, you can use try method.


A cleaner way can be use delegate

class Notification < ActiveRecord::Base
  delegate :human_model_name, to: :notifiable, prefix: true, allow_nil: true

Then, you can do:

notification.notifiable_human_model_name # if notifiable is nil will return nil as result instead of an exception

A simple method in your Notification model

class Notification < ActiveRecord::Base
  def human_notifable_name
    return unless self.notifiable # to avoid exception with nil notifiable



View Helper (If you think this is a view related method only)

module ApplicationHelper # or NotificationHelper
  def human_model_name(instance)
     return unless instance # to avoid exception with nil instance

Then, in your view...

<%= human_model_name(notification.notifiable) %>

Either option is fine. I would use one or the other depending on the case. In this case, I would use the first option. I think you are adding behaviour that can be useful in any model. I mean your method is not directly related with something about notifications. In a more generic way you want a method to return the class name of an ActiveRecord's instance. Today you want the model name of the notifiable ActiveRecord's instance. But, tomorrow you may want the model name of any ActiveRecord model.

To answer the question "Where should I put a method?" I suggest to break (without fear) a little bit the MVC pattern and read about:

(a little bit old, but you can get the idea)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download