Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails 4: ActiveRecord-style I18n on a non-ActiveRecord object?

I have a Plain Old Ruby Class (PORO) that behaves a lot like an ActiveRecord model, although it is not persisted to the database.

To make the internationalization(I18n) as painlessly as possible, I would like to also use the SomeModel.model_name.human and SomeModel.human_attribute_name(:attribute) methods on this PORO.

What module do I need to include to include above methods on my PORO?

like image 468
Qqwy Avatar asked Mar 08 '16 17:03

Qqwy


People also ask

What is ActiveRecord in Ruby on Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.

What is ORM in ROR?

1.2 Object Relational Mapping Object Relational Mapping, commonly referred to as its abbreviation ORM, is a technique that connects the rich objects of an application to tables in a relational database management system.

What is ActiveRecord base?

ActiveRecord::Base indicates that the ActiveRecord class or module has a static inner class called Base that you're extending.


1 Answers

Extend your class with the ActiveModel::Translation module:

class Widget
  extend ActiveModel::Translation
end

Widget.model_name.human
=> "Widget"

Widget.human_attribute_name :my_attribute
=> "My attribute"
like image 106
infused Avatar answered Oct 06 '22 10:10

infused