undefined method in Module - ruby-on-rails

I have a /lib/custom
inside I have custom.rb and custom_page.rb
custom.rb
require 'custom_page.rb'
module Custom
def self.name(params)
# logic
end
end
I've added in application.rb config.autoload_paths += %W(#{config.root}/lib)
I can't seem to call in my controllers to Custom.name(params)
NoMethodError: undefined method `name' for Custom:Module
I've tried with define the method as def Custom.name, using class << self and method_function :name yet nothing helps..
Am I missing something?

It's because of the Rails naming convention. In your rails console, try
irb(main):001:0> Custom::Custom
LoadError: Expected lib/custom/custom.rb to define Custom::Custom
Rais expects you do define module Custom::Custom (not module Custom) in lib/custom/custom.rb.
Rails sees a folder lib/custom and created an empty module Custom (doesn't respond to name method) based on convention, if you want to define module Custom, you have to write a file lib/custom.rb
The convention is
lib/custom.rb #define module Custom
lib/custom/deeper.rb #define module Custom::Deeper
lib/empty_folder/ # rails provides you an empty module EmptyFolder
BTW you don't have to require 'custom_page' in your custom.rb, if Rails sees CustomPage in your code, it will try to load the class definition file based on naming convention, given that your custom_page.rb file path follows the convention.

You can also use ActiveSupport::Concern to extend both class and instance methods. In the module just to this:
module Custom
extend ActiveSupport::Concern
included do
# everything but the class methods go here
end
module ClassMethods
# define class methods here
def name(params)
#logic
end
end
end

Related

Accessing helpers and models from rails engine initializer

I'm trying to make a Ruby on Rails engine, and I want the initializer to be able to have access to the helpers and models.
I'll write below an example, part of the code, and the error that I have. It may not be the recommended way, because I can see that in some cases I'm repeating myself, but it's the first engine I make.
file lib/my_engine/engine.rb
module MyEngine
require 'my_engine/functions'
class Engine < ::Rails::Engine
isolate_namespace MyEngine
config.autoload_paths += %W( #{config.root}/lib )
end
class GlobalVars
attr_accessor :foo
def initialize
#foo = MyEngine::Functions.new
end
end
class << self
mattr_accessor :GLOBAL
mattr_accessor :USER_CONFIG
self.GLOBAL = MyEngine::GlobalVars.new
# add default values of more config vars here
self.USER_CONFIG = 'default config'
end
def self.setup(&block)
yield self
end
end
file lib/my_engine/functions.rb
module MyEngine
require '../../app/helpers/my_engine/options_helper'
class Functions
include MyEngine::OptionsHelper
attr_accessor :some_link
def initialize
#some_link = get_option('dummy')
end
end
end
There is also a controller named OptionsController in app/controllers/my_engine, and OptionsHelper in app/helpers/my_engine/options_helper.rb:
module MyEngine
module OptionsHelper
def get_option(name)
MyEngine::Option.new
end
end
end
When I try to run the dummy application, this error occurs:
/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant MyEngine::Option (NameError)
If I change to just Option.new, I have this error:
/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant MyEngine::OptionsHelper::Option (NameError)
For ::MyEngine::Option.new, I have:
/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant MyEngine::Option (NameError)
For ::Option.new, I have:
/app/helpers/my_engine/options_helper.rb:4:in `get_option': uninitialized constant Option (NameError)
The dummy application has nothing in it. All helpers and models defined above are in the engine.
Before this, I had other errors because it couldn't access the helper, or the Functions class. I had to add require and include to make it work even if they are placed in the same directory. Also, to work, I had to move GlobalVars from its own file inside engine.rb.
Can somebody show me what I'm doing wrong?
After I used required for every class, I ended with ActiveRecord::ConnectionNotEstablished, and it seems that not everything is loaded and available at that point when the GLOBAL object is created.
So I moved the code that was using the models in a separate init method. Then, I added an after initialize event:
config.after_initialize do
MyEngine.GLOBAL.init
end
I see a possible problem: because you are inside module MyEngine it might be possible that actually rails is looking for MyEngine::MyEngine::Option, so I see two approaches:
just write Option: this will look for MyEngine::Option
write ::MyEngine::Option this will look in the global namespace and find MyEngine::Option
Secondly, if that does not help, even though your path seems correct, but you can always explicitly require "my_engine/option" at the top of the file. I am not entirely sure the autoloading in an engine works in quite the same way, and I tend to, in my engine file, require almost everything (to make sure it works).
In my engine.rb I do
require_relative '../../app/models/my_engine/option'
maybe this will help, but it is not a nice solution.

How to include a module in a class

A module in my gem is included in a class in another gem, which is extended by a custom class in a Rails app:
My gem:
module MyGem
def my_method
end
end
AnotherGem.send :include, MyGem
Another gem:
class AnotherGem
end
Class in Rails app:
class ClassInRailsApp < AnotherGem
end
Running this leads to the following behavior:
$ rails c
Loading development environment (Rails 5.1.4)
irb(main):004:0> MyGem.method_defined? :my_method
=> true
irb(main):005:0> AnotherGem.method_defined? :my_method
=> true
irb(main):006:0> ClassInRailsApp.method_defined? :my_method
NoMethodError: undefined method `my_method' for ClassInRailsApp:Class
How can I make sure my module is included before the class is extended?
EDIT:
I tried to directly include MyGem in ClassInRailsApp and the specified instance method is still not available. Could the issue be related to that?
In your thinking, you're just calling a method, e.g. #object.my_method. In reality, you're calling Class level method, e.g. Object.my_method, but have it defined as an instance level method. The correct way to do what you're trying would be Object.new.my_method, however, don't do that.
To call a method like this you'd have to define it as a method on the class. See this page, for a better understanding. Specifically the section "A Common Idiom" on how to define Class level methods via a module.

Calling a module's methods on a Gem's object?

I've generated an object via a ruby gem (Koala) and I've written a few modules with helper methods. What do I need to do in order to be able to use the methods within the modules on the object?
If I, model_object = Model.new, model_object will have access to all the instance variables but object does not (see below).
Ruby 2.1, Rails 4.1
config/application.rb - Autoloading modules in folder
config.autoload_paths << Rails.root.join('lib/module_folder')
Model
class Model < ActiveRecord::Base
include Module
include Module::Module2
include Module::Module3
def self.create_account(token)
object = Module.module_class_method(token) #this works and generates the Koala object
ERROR: object.module2_instance_method # Error: NoMethodError Exception: undefined method
end
end
Module
module Module
extend ActiveSupport::Concern
end
Module2
module Module
module Module2
def module2_instance_method
end
end
end
SOLVED MYSELF
- the issue was the include statements being within the class, if I moved them outside it worked.
I believe if you include your modules somewhere under the app/ directory - they will be included automatically. Otherwise, you actually have to require them in your rails code explicitly with a require statement
Without seeing the actual code, I think the problem with Module2 in your code snippet is the self. method.
Because you are calling module2_instance_method on an instance of your object, the method in the module cannot have the self. because that designates a class method and, as such, would have to be called as Module::Module2.module2_instance_but_not_really_because_I_am_a_class_method.
I believe if you change def self.module2_instance_method ... end to def module2_instance_method ... end, you should no longer receive the NoMethodError exception.
Apologies if I've misread or misunderstand the OP.
Moved the include statements from inside to above the class declaration and all methods began to work. My assumption is that when they are within the statement they are only available to objects of that class.

Reuse methods in a Rails generator

I'm writing a series of Rails generators that will share several of the same methods. I would like to abstract these methods into a module or class of their own to be reused (but not automatically fired) within each of my generators.
My latest attempt was to autoload a helper file and later include it:
lib/my_gem/engine.rb
module MyGem
class Engine < Rails::Engine
config.autoload_paths += Dir["#{config.root}/lib/helpers/**"]
end
end
lib/helpers/generators_helper.rb
module MyGem
module GeneratorsHelper
def some_method
# ...
end
end
end
lib/generators/my_gem/my_generator.rb
# ...
include MyGem::GeneratorsHelper
# ...
But I'll see something like Error: uninitialized constant MyGem::GeneratorsHelper.
I was able to accomplish this by manually requiring the file and then including the module. It's a little ugly, but keeps me from duplicating helper methods:
lib/my_gem/generators/my_generator.rb
require "#{Gem::Specification.find_by_name("my_gem").gem_dir}/lib/helpers/generators_helper.rb"
include MyGem::GeneratorsHelper

Adding functionality to Rails

I'm working on a Rails app and am looking to include some functionality from "Getting the Hostname or IP in Ruby on Rails" that I asked.
I'm having problems getting it to work. I was under the impression that I should just make a file in the lib directory, so I named it 'get_ip.rb', with the contents:
require 'socket'
module GetIP
def local_ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true # turn off reverse DNS resolution temporarily
UDPSocket.open do |s|
s.connect '64.233.187.99', 1
s.addr.last
end
ensure
Socket.do_not_reverse_lookup = orig
end
end
I had also tried defining GetIP as a class but when I do the usual ruby script/console, I'm not able to use the local_ip method at all. Any ideas?
require will load a file. If that file contains any class/module definitions, then your other code will now be able to use them. If the file just contains code which is not in any modules, it will get run as if it were in the same place as your 'require' call (like PHP include)
include is to do with modules.
It takes all the methods in the module, and adds them to your class. Like this:
class Orig
end
Orig.new.first_method # no such method
module MyModule
def first_method
end
end
class Orig
include MyModule
end
Orig.new.first_method # will now run first_method as it's been added.
There's also extend which works like include does, but instead of adding the methods as instance methods, adds them as class methods, like this:
Note above, how when I wanted to access first_method, I created a new object of Orig class. That's what I mean by instance method.
class SecondClass
extend MyModule
end
SecondClass.first_method # will call first_method
Note that in this example I'm not making any new objects, just calling the method directly on the class, as if it had been defined as self.first_method all along.
So there you go :-)
You haven't described how you're trying to use the method, so I apologize in advance if this is stuff you already know.
The methods on a module never come into use unless the module is included into a class. Instance methods on a class require there to be an instance of the class. You probably want a class method instead. And the file itself should be loaded, generally through the require statement.
If the following code is in the file getip.rb,
require 'socket'
class GetIP
def self.local_ip
orig, Socket.do_not_reverse_lookup = Socket.do_not_reverse_lookup, true
UDPSocket.open do |s|
s.connect '64.233.187.99', 1
s.addr.last
end
ensure
Socket.do_not_reverse_lookup = orig
end
end
Then you should be able to run it by saying,
require 'getip'
GetIP.local_ip
require and include are two different things.
require is to strictly load a file once from a load path. The loadpath is a string and this is the key used to determine if the file has already been loaded.
include is used to "mix-in" modules into other classes. include is called on a module and the module methods are included as instance methods on the class.
module MixInMethods
def mixed_in_method
"I'm a part of #{self.class}"
end
end
class SampleClass
include MixInMethods
end
mixin_class = SampleClass.new
puts my_class.mixed_in_method # >> I'm a part of SampleClass
But many times the module you want to mix in is not in the same file as the target class. So you do a require 'module_file_name' and then inside the class you do an include module.

Resources