Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert module class name to symbol in rails

In a rails initializer file, there is a line for configuring activity objects that accepts symbols like so:

config.objects += [ :room, :hotel, ..etc ]

These symbols represent classes that I want configured.

My problem is that i'm using a rails engine which defines a 'Blogit::Post' module class which I want to configure. How do I add this to the config array as a symbol?

like image 802
Anchor Avatar asked Aug 18 '13 05:08

Anchor


1 Answers

You can control camel cases and namespaces in classification of strings by adding underscores or forward slashes to your config.objects.

Forward slashes classify to a namespace:

:"blogit/post".classify #=> Blogit::Post

Underscores classify to a camelcase:

:blogit_post.classify #=> BlogitPost

So in your case, the solution would be:

config.objects += [ :room, :hotel, :"blogit/post", ..etc ]

Or, if you use ruby 2.0, you can use a much cleaner way of array symbol instantiation:

config.objects += %i(room hotel blogit/post)
like image 184
Beat Richartz Avatar answered Nov 02 '22 12:11

Beat Richartz