Autoload from namespace inside user folder in application

We are currently developing our own CMS engine for ROR 3.2. In this process, several types of classes have arisen that we want to be first-class citizens in our rails application, that is, they must be in the application’s application folder and plugins.

Currently we have the following types:

  • Data source
  • Datatype
  • View

I created multi-user directories in the application folder to save them:

  • application / data _source
  • application / data_type
  • application / view

Other types will appear later, and I'm a little concerned about the pollution of the application folder with so many directories. Thus, I want to move them to a subdirectory / module that contains all the types defined by cms.

All classes should be inside the MyCms namespace, and the catalog layout should look like this:

  • application / my _cms / data_source
  • application / my_cms / data_type
  • application / my_cms / view

But now I'm having problems with startup, because the default startup for rails will look like this:

  • application / data _source / my_cms
  • application / data_type / my_cms
  • app / view / my_cms

But in this way I would not group all types of objects in one directory.

What I want is a bit like viewing a group of isolated engines. For example, in Devise, all views are grouped in the subdirectory views / dev.

Any idea how this can be achieved without a great user implementation?

+4
source share
1 answer

You will need to add app / my_cms to your startup path in the config / application.rb file:

config.autoload_paths << "#{config.root}/app/my_cms" 

provided that your classes are defined without a namespace, for example:

 class DataSource ... end 

If you write them like this in app / my_cms / data_source.rb:

 class MyCms::DataSource ... end 

you can add the application folder to the download path:

 config.autoload_paths << "#{config.root}/app" 

Alternatively, you can do this manually, but you will lose the reload for these classes in Rails development:

in app / my_cms.rb (and with automatic loading for the application):

 module MyCms autoload :AnotherDataSource, 'my_cms/data_source/one_data_source' autoload :AnotherDataSource, 'my_cms/data_source/another_data_source' ... end 
+8
source

All Articles