How to require and auto load files properly - ruby-on-rails

I'm running into quite a few errors around how to require files property. Hoping for some insight.
There are files as so:
app/models
model.rb
app/workers
parent_worker.rb
app/workers/directory_1
directory_worker.rb
foo_worker.rb
bar_worker.rb
class DirectoryWorker < ParentWorker
end
class FooWorker < DirectoryWorker
def method_called_by_model
end
end
When I call the method, method_called_by_model I get the following error:
NameError: uninitialized constant Model::FooWorker
I've added the following to application.rb, didn't add app/workers since it should be loaded automatically according to the documentation.
config.autoload_paths << "#{Rails.root}/app/workers/directory_1"
When I require_relative the worker files in the model I get the following error referring to the inherited class being unknown:
NameError: uninitialized constant DirectoryWorker
from project/app/workers/directory_1/FooWorker.rb:2:in `<top (required)>'
Any have any ideas what I can do?

You need to namespace those workers since they are inside a directory.
First remove the autoload call you added.
Here's how the files should be named and what they should look like inside.
# app/workers/parent_worker.rb
class ParentWorker
end
# app/workers/directory_1/directory_worker.rb
class Directory1::DirectoryWorker < ParentWorker
end
# app/workers/directory_1/foo_worker.rb
class Directory1::FooWorker < Directory1::DirectoryWorker
def method_called_by_model
end
end
# app/workers/directory_1/bar_worker.rb
class Directory1::BarWorker < Directory1::DirectoryWorker
end

Related

undefined local variable or method for method located in lib directory file

I have some code i've inherited and am in the process of upgrading it to Rails 3.1. I'm suuuuper close to done but I got a bug.
In Rails Console I run User.first and I get this error
undefined local variable or method `acts_as_userstamp' for #<Class:0x000000046bef50>
Now acts_as_userstamp is a method located on line two inside my User model
class User < ActiveRecord::Base
#TODO /lib is not loading??? or is it??? why this method not work in browser?
acts_as_userstamp
And is defined in a file called app/lib/model_modifications.rb.
Now I recently discovered that my app/lib folder was not being autoloaded in my application.rb file and I think that's been fixed...or has it? Is this file correct? Or no?
require File.expand_path('../boot', __FILE__)
require 'rails/all'
# evil outdated soap middleware, TODO: kill it with fire
# Does this have to be loaded BEFORE the first line???
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', "vendor", "soap4r"))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', "vendor", "plugins", "soap4r-middleware", "lib"))
# evil outdated soap middleware, TODO: kill it with fire
require 'soap4r-middleware'
require File.join(File.dirname(__FILE__), '..', 'app', 'lib', 'soap.rb')
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require *Rails.groups(:assets => %w(development test))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module MyappDev
class Application < Rails::Application
# startup the lib directory goodies <-- IS THIS CORRECT???
# config.autoload_paths << "#{Rails.root}/lib"
# config.autoload_paths += %W( lib/ )
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
# Configure the default encoding used in templates for Ruby 1.9.
config.encoding = "utf-8"
# Configure sensitive parameters which will be filtered from the log file.
config.filter_parameters += [:password]
config.middleware.use MyAPIMiddleware
end
end
I'm trying to debug this file as I post this now. Here is a peak at it's internal structure...(i've just included the overall structure for the sake of brevity)
app/lib/model_modificatons.rb
class Bignum
...
end
class Fixnum
...
end
class ProcessorDaemon
...
end
module ActiveRecord
module UserMonitor
...
end
module MyLogger
...
end
end
class Object
...
end
class Struct
...
end
class String
...
end
class Fixnum
...
end
class OpenStruct
...
end
class ActiveRecord::Base
def self.visible_columns
...
end
...
def self.acts_as_userstamp
logger.info "HI fonso - acts_as_userstamp is called"
include ActiveRecord::UserMonitor
end
...
protected
def self.range_math(*ranges)
...
end
end
class Array
...
end
class DB
...
end
If you can spot a problem with the overall structure or anywhere else please let me know.
So why is this method not found? I'm trying to debug it as I'm posting this and I'm getting nothing.
I suspect the file app/lib/model_modifications.rb is not being loading. That nothing in the /lib directory is being loaded..but how do I confirm this?
Thank you for reading this far, I hope I've not rambled on too much.
autoload_path configuration does not load all the given files on the boot but defines folders where rails will be searching for defined constants.
When your application is loaded, most of the constants in your application are not there. Rails have a "clever" way of delaying loading the files by using a constant_missing method on Module. Basically, when Ruby encounters a constant in the code and fails to resolve it, it executes said method. THe sntandard implementation of this method is to raise UndefinedConstant exception, but rails overrides it to search all of its autoload_paths for a file with a name matching the missing constant, require it and then check again if the missing constant is now present.
So, in your code everything works as expected and you need to load this extension file manually. If you want to have some code that executes on the application boot, put your file within config/initializers folder.
Aside: Try avoiding monkey patching whenever possible. It might be looking clever, but adding more methods to already overpopulated classes will not make them easier to use.

Autoload custom exceptions files with RoR

In my app/ directory I have a folder services. To autoload my files in the service folder, I added 'app/services' in my 'config/spring.rb'
%w(
.ruby-version
.rbenv-vars
tmp/restart.txt
tmp/caching-dev.txt
app/services
).each { |path| Spring.watch(path) }
It's work fine. But now, I want to create a sub dir for my custom exceptions (in app/services/errors/). But when I try to add a new line in my spring.rb :
%w(
.ruby-version
.rbenv-vars
tmp/restart.txt
tmp/caching-dev.txt
app/services
app/services/errors
).each { |path| Spring.watch(path) }
It's not working, I got some :
NameError:
uninitialized constant SixError
Here my custom error file (app/services/errors/six_error.rb):
class SixError < RuntimeError
end
class NilTokenError < SixError
end
class SixRequestFailed < SixError
end
class NoAliasRequestPendingError < SixError
end
class AmountLessThanZeroError < SixError
end
class NilAliasError < SixError
end
And this is how I called this :
SixError::SixRequestFailed
Rails is loading class in subdir with a namespace corresponding to the dirname.
If you have this structure :
-app
--services
---errors
----six_errors.rb
Rails expecting the file six_errors.rb to declare class upon the namespace Errors.
In your case you should declare all your errors class in a module named Errors.
module Errors
class SixError < RuntimeError; end
end

Cannot require file in application.rb

I'm trying to simplify our configuration by creating small configuration classes that can be included in our application.rb.
lib/logging.rb
class << Logging
def configure(config)
# ... configure logging stuff
end
end
application.rb
require 'lib'
module MyApp
class Application < Rails::Application
Logging.configure(config)
end
end
The problem is if I don't use require "lib" then I get an Undefined Constant Logging error. But if I try to require it I get:
bin/rails:6: warning: already initialized constant APP_PATH
/opt/qtip/bin/rails:6: warning: previous definition of APP_PATH was here
The only way I've been able to get it to work is by doing this which is very limiting.
config.autoload_paths = %w(lib)
config.after_initialize do
::Logging.configure(config)
end
You have wrong class declaration.
Instead
class << Logging
you should use
class Logging
class << self
def configure(config)
end
end
end

Rspec failing with load_missing_constant when classes live in subdirectories and are subclassed

I'm having some troubles running a bunch of RSpec tests due to something I suspect is related to autoloading. Here is the error:
/usr/share/ruby-rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:490:in `load_missing_constant': Expected /var/lib/jenkins/.../portfolios/base_manage_controller.rb to define Portfolios::BaseManageController (LoadError)
from /usr/share/ruby-rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:181:in `block in const_missing'
from /usr/share/ruby-rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:179:in `each'
from /usr/share/ruby-rvm/gems/ruby-1.9.2-p320/gems/activesupport-3.1.4/lib/active_support/dependencies.rb:179:in `const_missing'
from /usr/share/ruby-rvm/gems/ruby-1.9.2-p320#global/gems/rake-0.9.2.2/lib/rake/ext/module.rb:36:in `const_missing'
from /var/lib/jenkins/jobs/.../app/controllers/portfolios/customize_controller.rb:1:in `<top (required)>'
Here is the header of the file:
class Portfolios::BaseManageController < ApplicationController
And it lives in app/controllers/portfolios/base_manage_controller.rb
And its subclass:
class Portfolios::CustomizeController < Portfolios::BaseManageController
And it lives in app/controllers/portfolios/customize_controller.rb
Finally here are a bunch of autoloads:
config.autoload_paths += Dir["#{config.root}/lib", "#{config.root}/lib/**/"]
config.autoload_paths += %W(#{config.root}/app/models/statistics)
#Any test/dev specific load paths
if not Rails.env.production?
config.autoload_paths += %W(#{config.root}/spec/support)
config.autoload_paths += %W(#{config.root}/spec/support/builders)
config.autoload_paths += %W(#{config.root}/spec/support/modules)
config.autoload_paths += %W(#{config.root}/spec/support/utils)
end
Any help would be much appreciated!
This error sometimes occurs when there is an exception raised when the class is defined. In this case, a runtime error may be occurring when attempting to define the class found in base_manage_controller.rb.
To verify this is the case, try stripping out everything from base_manage_controller.rb, except the class declaration:
class Portfolios::BaseManageController < ApplicationController
end
It should result in the specs running, but failing.
To find the runtime error, put everything back into the class, and load it from script/console, by calling Portfolios::BaseManageController. That will attempt to dynamically load the class, and raise the exception that prevents your class from being defined.
It looks like that Portfolios is not defined when rspec is loading Portfolios::BaseManageController. Where did you define Portfolios? If it's defined as a module or class somewhere in a separate file, you need to require this file first.
there is a collision in your autoload path because of the existence of a file with the same name at /var/lib/jenkins/.../portfolios/base_manage_controller.rb
You need to either modify your load path so app/controllers comes up earlier in the list or the file specified at the path above should be renamed or removed.
Following solution will help you
create spec in following path for app/controllers/portfolios/base_manage_controller.rb
spec/controllers/portfolios/base_manage_controller_spec.rb
The spec content should be like this
describe Portfolios::BaseManageController do
-----
end
You can apply the same logic for app/controllers/portfolios/customize_controller.rb

Forcing rails to autoload class

I have several small classes that are in a single file in /app/models, similar to:
# /app/models/little_class.rb
class LittleClass; ...do stuff; end;
class AnotherLittleClass; ...do stuff; end;
Rails only seems geared to autoload classes in files reflecting the class name. So referencing AnotherLittleClass outside of the file raises "unitialized constant" errors as below until LittleClass is referenced:
irb(main):001:0> AnotherLittleClass
NameError: uninitialized constant AnotherLittleClass
irb(main):02:0> LittleClass
=> LittleClass
irb(main):03:0> AnotherLittleClass
=> LittleClass2
It would be a pain and messy to split them into individual files. Is there a way to autoload these classes, so referencing AnotherLittleClass without LittleClass doesnt raise an error?
You could put them into a module and use them within this namespace SomeLittleClasses::LittleClass.do_something
# /app/models/some_little_classes.rb
module SomeLittleClasses
class LittleClass
def self.do_something
"Hello World!"
end
end
class AnotherLittleClass
def self.do_something
"Hello World!"
end
end
end
Try this trick:
1.9.2p312 :001 > AnotherLittleClass.new
# => NameError: uninitialized constant AnotherLittleClass
1.9.2p312 :002 > autoload :AnotherLittleClass, File.dirname(__FILE__) + "/app/models/little_class.rb"
# => nil
1.9.2p312 :003 > AnotherLittleClass.new
# => #<AnotherLittleClass:0xa687d24>
These are your choices, as I see it:
split your file up into one file per class, put them in a dir named according to the rails convention (SomeClass => some_class.rb) and in a startup file (say, create a file in config/initializers), call:
autoload_paths Rails.application.config.root + "/path/to/lib"
add something like this to a startup file:
%W[
Class1 Class2
Class3 Class4 Class4
].map(&:to_sym).each dp |klass|
autoload klass,Rails.application.config.root + "/path/to/lib/file"
end
This of course will have to be updated each time a new class is added to the file.
Move all of the classes into a module/class namespace and call autoload to add it as above
just load the whole file up-front in a startup file with require. Ask yourself: does the extra effort warrant delaying the load of this file?
The following file app/models/statistic.rb is given :
class Statistic
# some model code here
end
class UsersStatistic < Statistic; end
class CommentsStatistic < Statistic; end
class ConnectionsStatistic < Statistic; end
Create a file config/initializers/autoload_classes.rb and add the following code:
# Autoloading subclasses that are in the same file
# This is the normal way to load single classes
#
# autoload :UsersStatistic, 'statistic'
# autoload :CommentsStatistic, 'statistic'
# autoload :ConnectionsStatistic, 'statistic'
# This is the most dynamic way for production and development environment.
Statistic.subclasses.each do |klass|
autoload klass.to_s.to_sym, 'statistic'
end
# This does the same but loads all subclasses automatically.
# Useful only in production environment because every file-change
# needs a restart of the rails server.
#
# Statistic.subclasses

Resources