In rails 5.2 I have a small lib
which is under app/lib/itunes (everything under app/lib should be autoloaded right?).
Nevertheless I get a load error when I start sidekiq
LoadError: Unable to autoload constant Itunes::ItunesClient,
expected /app/lib/itunes/itunes_client.rb to define it
2018-06-06T19:38:49.560Z 46606 TID-ov5d572iq WARN:
.rvm/gems/ruby-2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:503:in `load_missing_constant'
.rvm/gems/ruby-2.5.0/gems/activesupport-5.2.0/lib/active_support/dependencies.rb:193:in `const_missing'
/app/interactors/fetch_itunes_app_service.rb:4:in `call'
class FetchItunesAppService
include Interactor
def call
#client = Itunes::ItunesClient.new
...
end
end
#app/lib/itunes/itunes_client.rb
class Itunes::ItunesClient < Itunes::ItunesBaseClient
...
end
#app/lib/itunes/itunes_base_client.rb
class Itunes::ItunesBaseClient
...
end
Related
I'm trying to update this gem to work with rails 7 and I think that the error is due to zeitwerk. The error that's popping up is the following
/Users/nate/.rbenv/versions/3.0.2/lib/ruby/gems/3.0.0/gems/impressionist-2.0.0/lib/impressionist/engine.rb:15:in `block (2 levels) in <class:Engine>': uninitialized constant Impressionist::Engine::ImpressionistController (NameError)
and It's originating from lib/impressionist/engine.rb here from the lines below
module Impressionist
class Engine < ::Rails::Engine
attr_accessor :orm
initializer 'impressionist.model' do |app|
#orm = Impressionist.orm
include_orm
end
initializer 'impressionist.controller' do
require "impressionist/controllers/mongoid/impressionist_controller" if orm == :mongoid.to_s
ActiveSupport.on_load(:action_controller) do
include ImpressionistController::InstanceMethods <--- HERE
extend ImpressionistController::ClassMethods <--- HERE
end
end
private
def include_orm
require "#{root}/app/models/impressionist/impressionable.rb"
require "impressionist/models/#{orm}/impression.rb"
require "impressionist/models/#{orm}/impressionist/impressionable.rb"
end
end
end
I think that this first include is trying to call app/controllers/impressionist_controller.rb, but for some reason doesn't load with rails 7. Any ideas on how to approach fixing this? I suspect that I have to somehow incorporate zeitwerk here.
I’m using Rails 4.2.3. I’ve created a file at lib/app_config/aws_secrets.rb, which looks like
require 'yaml'
module mycoAppConfig
class AwsSecrets
def self.load
…
end
end
end
Then in my config/application.rb file, I have invoked the above method using
AppConfig::AwsSecrets.load
But when I run a rake task, I’m getting this uninitialized constant error
NameError: uninitialized constant MycoWeb::Application::AppConfig
/Users/myuser/Documents/workspace/myco/myapp/config/application.rb:120:in `block in <class:Application>'
/Users/myuser/.rvm/gems/ruby-2.4.5#myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
/Users/myuser/.rvm/gems/ruby-2.4.5#myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:28:in `block in on_load'
/Users/myuser/.rvm/gems/ruby-2.4.5#myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:27:in `each'
/Users/myuser/.rvm/gems/ruby-2.4.5#myapp/gems/activesupport-4.2.10/lib/active_support/lazy_load_hooks.rb:27:in `on_load'
/Users/myuser/.rvm/gems/ruby-2.4.5#myapp/gems/railties-4.2.10/lib/rails/railtie/configuration.rb:53:in `before_configuration'
/Users/myuser/Documents/workspace/myco/myapp/config/application.rb:113:in `<class:Application>'
/Users/myuser/Documents/workspace/myco/myapp/config/application.rb:9:in `<module:mycoWeb>'
What’s the proper way to include/name my library?
according to the naming convention you should use module name as AppConfig
require 'yaml'
module AppConfig
class AwsSecrets
def self.load
…
end
end
end
application.rb
config.eager_load_paths += %W( #{config.root}/lib/**/*.rb )
I want to add helper from test/helpers/auth_request_helpers.rb into test/test_helper.rb to have it available in all tests. I thought all I had to do was to include this helper inside of the test_helper like below:
module ActiveSupport
class TestCase
include AuthRequestHelpers
# some other things
end
end
But when I running a minitest I'm getting an error:
uninitialized constant ActiveSupport::TestCase::AuthRequestHelpers (NameError)
Did I missed something? I'm not using RSpec
Helper which I tried to add below:
module AuthRequestHelpers
def hmac_code(data, secret_key)
Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', secret_key, data))
end
end
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
Rails 4.2.4, Ruby 2.1.7
I have a module inside lib/ directory.
lib/BLL/user_feed.rb
module BLL
class UserFeed
def initialize
logger.debug "Class has been initialized"
end
def get_user_feed(user_id)
# logic here
return {
# object
}
end
end
end
When I try to include that in my controller to use my user_Feed logic,
class UserfeedController < ApplicationController
include BLL
before_action :authenticate_user!
def show
# some logic
end
end
In my config/application.rb
config.autoload_paths << Rails.root.join('lib')
This runs fine locally, however, it breaks when I deploy it on Heroku.
it's throwing
ActionController::RoutingError (uninitialized constant UserfeedController::BLL):
error.
2015-10-20T13:45:13.791457+00:00 app[web.1]: /app/app/controllers/api/v1/userfeed_controller.rb:1:in `<top (required)>': uninitialized constant Bll (NameError)
2015-10-20T13:45:13.791457+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:472:in `block (2 levels) in eager_load!'
2015-10-20T13:45:13.791458+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:471:in `each'
2015-10-20T13:45:13.791459+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:471:in `block in eager_load!'
2015-10-20T13:45:13.791460+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:469:in `each'
2015-10-20T13:45:13.791462+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:469:in `eager_load!'
2015-10-20T13:45:13.791463+00:00 app[web.1]: from /app/vendor/bundle/ruby/2.1.0/gems/railties-4.2.4/lib/rails/engine.rb:346:in `eager_load!'
Any suggestions?
I think you're missing a module BLL; end in lib/bll.rb
But also, play with naming the module Bll, but I don't think that's it
try
config.autoload_paths += %W(#{config.root}/lib/BLL)
and dont forget to restart the server
Edit1
Moreover; changing the name of Dir BLL to bll also works
Ruby defaults to CamelCase, you'll have to use the following:
#vendor/bll/user_feed.rb
module Bll
class UserFeed
...
end
end
As a second, the vendor dir is autoloaded (to the best of my knowledge), so the above code should work to fix the UnrecognizedConstant error.
https://softwareengineering.stackexchange.com/questions/123305/what-is-the-difference-between-the-lib-and-vendor-folders
Rails in fact loads your app directory when the app loads. So actually no need to mention your app/bll in autoload paths.
However, what I am doing wrong here in this case is adding module on top of the class.
So, my app is looking for app/bll/bll/Whatever
module Bll - there is not need for this module to be declared
class Whatever
# some logic.
end
end
All you need to do is.
class Whatever
end
After this, your class is available to use.