14

This is probably a stupid question, but I'm new to Ruby on Rails and I could use a little guidance. I want to have a helper/utility class that performs a group of network operations and returns results. Where do I put that class and how do I use it.

I've created network_helper.rb in my app/modulename/helpers directory. In my controller when I try to do

  myNetworkHelper = ModuleName::NetworkHelper.new
  results = myNetworkHelper.getResults

I get an error

 undefined method `new' for MyModule::NetworkHelper:Module

I'm sure this is just a misunderstanding of how ruby on rails works. Can I get some clarification?

Would it be better to make this a class instead of a module and put it in libs? And can I add subfolders in libs and have them automatically loaded?

codeetcetera
  • 3,113
  • 4
  • 33
  • 60

4 Answers4

7

Lib or Classes

Little utility classes like this typically go in the lib folder, though some people prefer to create a folder called classes. Whichever you choose, make sure you import the folder in config/application.rb, as the lib folder is not autoloaded:

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

Concerns

If instead of a utility class, you want to extend some of your models with reusable code, you may also wish to look at the new Rails 4 concerns folders which encourage you to extract reusable modules:

see: How to use concerns in Rails 4

Community
  • 1
  • 1
superluminary
  • 38,944
  • 21
  • 142
  • 143
3

To use new, the thing your calling it on must be a class, not a module. You're using a module. Change module to class in lib/utilities/network_utility.rb.

Linuxios
  • 31,993
  • 12
  • 82
  • 110
  • How do I include this in my controller in a ruby friendly manner? – codeetcetera Mar 10 '13 at 04:26
  • 2
    @btate: Using `require_relative`. If your main file is in, say, `bin/main.rb` (whatever), use `require_relative '../lib/utilities/network_utility.rb'`. – Linuxios Mar 10 '13 at 04:27
  • Strictly speaking, you *can* call `new` on a Module, you’d just have to define it. Though it probably wouldn’t do what you want. Nothing particularly special about `new`. – Andrew Marshall Mar 10 '13 at 04:40
  • @AndrewMarshall: True. But once you do that, well, your just going to confuse people. – Linuxios Mar 10 '13 at 14:10
1

I cannot verify this at the moment, however I believe one place you can store your custom modules and classes is the lib directory. Alternatively, you should be able to store them in the app directory in the manner you have indicated by adding the following line to your environment.rb:

config.load_paths << File.join(Rails.root, "app", "modulename")

Also, check out Yehuda Katz's answer, which I think not only answers your question better, but also contains some very interesting and useful information and concepts relating to your situation. Hope that helps!

Community
  • 1
  • 1
Paul Richter
  • 10,414
  • 8
  • 47
  • 77
0

Add your class to app/lib folder instead of lib, so that you don't change autoload paths!

Explanations:

The accepted answer suggests adding the classes to lib.

But according to this discussion:

The lib folder does not belong to the autoload paths since Rails 3.

So it's discouraged to add lib under autoload path. Use app/lib instead.

user10375
  • 453
  • 1
  • 4
  • 9