Szilard Magyar Szilard Magyar - 6 months ago 14
Ruby Question

ruby string formatting in rails

So the goal is to turn for instance

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

I used to have this:

notification.notifiable.model_name.human.downcase


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

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

Options:

Initializer

/your_app/config/initializers/my_initializer.rb

module MyModule
  def human_model_name
    self.class.to_s.tableize.singularize.humanize.downcase
  end
end

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.

notification.try(:notifiable).try(:human_model_name)

A cleaner way can be use delegate

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

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
    self.notifiable.class.to_s.tableize.singularize.humanize.downcase
  end
end

Then...

notification.human_notifable_name

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
    instance.class.to_s.tableize.singularize.humanize.downcase
  end
end

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)