5

Ok, I've got this Ruby script that is supposed to be included in many Rails apps. So I don't want to break it down to pieces and jam it into a particular app, but would rather keep it in 1 piece and have Rails app load it instead. The script would be required mostly from models, mailers and rarely controllers.

So if my script is tools.rb, where in my Rails file tree should I put it and how/where in my Rails app should I include it? Also the script comes with YAML file.

Its my second day learning Rails, so please bear with me.

3 Answers 3

7

You can keep all the extra stuff in either /app/modules or /lib. And i prefer lib. After putting in the lib folder, require it in any initializer (or create one)

require "./lib/tools" in /config/initializers/tools.rb

And tadaa !! You can use that corresponding class/module anywhere in the rails application !!

And all the YAML files should be put in /config/.

*** fix syntax in '/lib/tools'

Sign up to request clarification or add additional context in comments.

Comments

5

You can include your own .rb files on lib folder. You can include, modules, class...etc under your own rb files.

If you want autoload or autorequire your custom library code, you must open your config/application.rb and add the next line for example:

config.autoload_paths += %W(#{config.root}/extras #{config.root}/lib)

You can take a look to:

http://reefpoints.dockyard.com/ruby/2012/02/14/love-your-lib-directory.html

You yaml files should be inside /config/ folder.

Regards!

Comments

2

If you have any module or class defined in script and use that module or class in app you just put it in lib . It will be accessed anywhere in app. Load the file if you need to initialize something which needs your application. Unless needed don't load things. If you think it must be loaded before app starts. Then you can put into config/initializers

yml files can be loaded like below in some initializers file (may be in your tools.rb) :

     MY_TOOLS = YAML.load_file("#{RAILS_ROOT}/config/tools.yml")

Then you can use MY_TOOLS anywhere in app.

4 Comments

Okay, I've put tools.rb in /lib, Yaml file in /config and restarted the server. But now when I try to create instance of class in tools.rb from Model, it goes: ModelName::ClassImTryingToCall not found. How do I make it so it gets to my class in tools.rb?
You will have access to all model of your app in lib.. its not a problem.
Ok I figured out what was wrong. I guess its a convention. It wanted to have a class/module with same name as file. So in tools.rb had to make class Tools, now it works.
Ya u should have same filename as class name. Thats how rails find classes. If u don't specify same filename as class name rails can't find class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.