Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the Rails initializer go in a gem?

I'm trying to extract some functionality from my existing app into a gem. The existing functionality uses an initializer to load up a config file when Rails starts up...

config/initalizers/myinitializer.rb

Where should this initializer go in the gem? Do I mirror the path structure inside the gem or put it somewhere else? This will be my first gem.

like image 736
ideasasylum Avatar asked Feb 19 '10 10:02

ideasasylum


People also ask

What is a Rails initializer?

An initializer is any file of ruby code stored under /config/initializers in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.

Is Ruby on Rails a gem?

Some developers prefer using their code for the purpose; others prefer using Ruby gems for authentication. DEVISE gem ruby on rails turns out one of the best rails gems 2021. It makes work effective and easy.

What is the purpose of environment RB and application RB files?

In the environment. rb file you configure these run-levels. For example, you could use it to have some special settings for your development stage, which are usefull for debugging. The purpose of this file is to configure things for the whole application like encoding.

What is a Railtie?

Rails::Railtie is the core of the Rails framework and provides several hooks to extend Rails and/or modify the initialization process. Every major component of Rails (Action Mailer, Action Controller, Active Record, etc.) implements a railtie. Each of them is responsible for their own initialization.


1 Answers

When the Gem is loaded, Rails first looks for a file called init.rb in the main Gem folder and requires it if available.

This can be a super-simple base skeleton for a Rails plugin packages as Gem:

name
name/lib
name/lib/name.rb
name/test
name/init.rb
like image 165
Simone Carletti Avatar answered Oct 27 '22 10:10

Simone Carletti