Can not call method of other class from static method - ruby-on-rails

At first, i have a class which in app/workes/ like this:
class SendMailTask
include Resque::Plugins::Status
require 'mail'
def perform
...
end
And as a controller, i have class UsersController and a static method like bellow:
class UsersController < ApplicationController
def self.check
...
::SendMailTask.create(to: [] << #to_addresses, subject: #subject, body: #body)
end
When i call method UsersController.check() from other file, i received the error: "in `block in check': uninitialized constant SendMailTask (NameError)"
But from other controller, i can call SendMailTask normally:
class ErrorController < ApplicationController
def index
...
::SendMailTask.create(to: [] << #to_addresses, subject: #subject, body: #body)
end
I try to add this line:
config.autoload_paths += %W(#{config.root}/app/workers)
to application.rb and try to add
require './SendMailTask'
at the begin of file users_controller.rb but it does not work.
Please help me resolve this error. Thanks you

NameError means the your SendMailTask isn't loaded. so you will have to load that. so couple of things.
I noticed a typo workes, so please verify the file name is correct. By Convention, it should be located at app/workers/send_mail_task.rb. so kindly double-triple check the same.
About require './SendMailTask', this is wrong. Instead it would be send_mail_task as requires works on filenames & not class names.
if still get an error, then please post your $LOAD_PATH to see you are requiring the file relative to the defined $LOAD_PATH
Instead of require, I prefer to use require_dependency as it works with code-reloading etc. so if you have trouble with auto-loading, just stick that require_dependency on top of the file, this will hint rails to load the file BEFORE running the controller.

Related

Rails global function available to all objects

I'd like to make a function current_order_week that would be available globally throughout my app and could be called similarly to something like current_user. I don't want to have to include it in a specific model / controller, I just want it available everywhere.
I've modified my /lib folder to include a lib_extensions.rb file and added to that file:
class Object
def current_order_week
end
end
I've modified my application.rb to include:
config.autoload_paths << Rails.root.join('lib')
config.eager_load_paths << Rails.root.join('lib')
But when I attempt to call current_order_week from the console or from a test, I still see:
NameError: undefined local variable or method 'current_order_week'
for main:Object
What else do I need to do?
You should add this function in the application_helper.rb file. All controllers extend from ApplicationController and ApplicationController includes the ApplicationHelper.
module ApplicationHelper
def current_order_week
end
end
This will be available to use in views and controllers
Monkey-patching core classes like Object is usually not a good idea, this may interfere with some gems etc. and in general can lead to painful debugging in the future.
If you absolutely want to do this - autoloading will not pick up Object from lib, because it is already defined. Create an initializer in config/initializers, then it will be loaded, but it will not reload on code changes.
But better way is including such code in ApplicationHelper, ApplicationRecord and ApplicationController
autoload_paths and eager_load_paths don't include modules, they only require files in which modules are defined. To use current_order_week you need to specify the name of the module:
module Foo
def current_order_week
.
.
.
end
end
Foo.current_order_week()
In order to use current_order_week without prepending the name of the module to it, you need to include Foo inside your controllers and models:
class ApplicationController < ActionController::Base
include Foo
def some_action
current_order_week()
end
end
class ApplicationRecord < ActiveRecord::Base
include Foo
end

can i access Module class method from controller just by calling the method?

I have the following module inside lib/api_client:
request.rb
module APIClient
class Request
require 'digest/sha1'
require "net/http"
require "uri"
def self.venues_response
...
end
end
end
wich i include in my controller class like this:
class VenuesController < ApplicationController
include APIClient
I'd like to access it's methods from inside venues_controller like this:
venues_response
But i get the following error:
undefined method `venues_response' for #<VenuesController:0x007f9ee058ad00>
And if i try this:
Request.venues_response
I get:
uninitialized constant VenuesController::Request
The only way seems to be this:
APIClient::Request.venues_response
Is there a way to make the call simpler? Am i missing something?
EDIT:
Forgot to mention autoloading is already added on application.rb:
config.autoload_paths += %W(#{config.root}/lib)
Rename request.rb to api_client.rb.

Uninitialized Constant in Rails Controller

I have the following in my controller:
class SurveysController < ApplicationController
def index
survey_provider = FluidSurveysProviders::SurveyProvider.new
contact_lists = survey_provider.get_lists()
#survey = Survey.new(contact_lists)
end
And I'm receiving this error:
NameError in SurveysController#index
uninitialized constant SurveysController::FluidSurveysProviders
Excuse my Rails noobiness, I'm sure I'm leaving out something important here. But it seems to me that I am trying to "initialize" the constant with this line:
survey_provider = FluidSurveysProviders::SurveyProvider.new
But that's the same line that's throwing an error because it's not initialized. Where should I be "initializing" the Provider?
Once you require fluid_surveys_providers (or similar) then do this:
include FluidSurveysProviders
Make sure SurveyProvider is wrapped with module FluidSurveysProviders. It may look like this
module FluidSurveysProviders
class SurveyProvider
...
end
end
if its an ActiveRecord object try this
class FluidSurveysProviders::SurveyProvider < ActiveRecord::Base
...
end
The SurveyProvider was not loaded correctly.
For a quick fix, move the class file into app directory, e.g. app/lib/survey_provider.rb. Then all code inside app will be auto-loaded by Rails.
Or make sure the path to class SurveyProvider is included in the autoload_path of Rails. In config/application.rb
config.autoload_paths += %W(#{config.root}/lib) # where lib is directory to survery_provider
If you use Rails 5, be careful that autoload is disabled in production environment. Check this link for more info.

Display a Variable from a Method in a File in the /lib/ directory on a View in Rails

In a file called foo.rb in my /lib/ directory it reads:
module Foo
def some_method
#text_1 = "Hello!"
end
end
How can I get the results of this method to show up in a View?
I've seen that I need to include the following line in the /config/application.rb file:
config.autoload_paths += %W(#{config.root}/lib
However, I do not completely understand how to pass a variable from a module in a file saved in the /lib/ directory - to show up in a View. I appreciate any advice.
In order to get that value to show up in the view, you'll need to understand how modules are used in Ruby. Typically modules are mixed into other classes either by including or extending them. This would then make that method available to another class which could then be referenced in the view. In your case you might want to include it so it becomes available to instances of whatever class you put it in. Say you have an ActiveRecord model called MyClass and you include Foo. You can then call my_method on instances of that model as demonstrated below:
class MyClass < ActiveRecord::Base
include Foo
end
In your controller:
class MyController
def new
#my_class = MyClass.new
end
end
In your view:
#my_class.some_method
Having said all that, it seems like there might be a better way to do whatever it is you're trying to do :)
Yes.I agree with
Beerlington.
You can do it in an other way,
It is not mandatory to add config.autoload_paths += %W(#{config.root}/lib to application file.Because by default the files which are located in /lib directory won't be executed at first when we run an application using rails s.
In order to make those files to be loaded,we need to include that line in application.rb.
Otherwise,we can directly write it as below,
In model,
require 'Filename'
class MyClass < ActiveRecord::Base
include Foo
end
In controller,
require 'foobar'
class BuyerController < ApplicationController
include Foobar
end
In foobar.rb,
module Foobar
def Foobar.foobar
"Hello world!"
end
end
In view,
<%= Foobar.foobar %> (You can directly call the method by using Modulenmae.Methodname)

undefined method `new' for Test:Module

Hello I have a problem of conflict of the namespace. I have a model: Test and controller TestsController. server displays an error
undefined method `new' for Test:Module
I read this question rails models
added to the model Test in module UserTest
module UserTest
class Test < ActiveRecord::Base
....
end
end
and added to the controller
class TestsController < ApplicationController
def new
#test = UserTest::Test.new
#test.questions.build
#title = "New test"
end
...
end
server shows an error: uninitialized constant TestsController::UserTest
after reading more I realized that probably need to add require or include a controller. Only I do not understand how to do it. please tell me.
Never rename a model to the same name of the project. You will get a message like this:
undefined method `new' for Example:Module
The project module priority precedes on the call.
The convention in Rails is to convert your Class name in file and your module name in directory. So if you put your UserTest::Test class in test.rb file in your app/model directory, the autoload failed to get your class. Because search on app/model/user_test/test.rb file.
So you can "force" the require in your Controller by adding a require in top of your file. The require if you put your class in your test.rb is : require 'test.rb'
To know how define your require is to think the LOAD_PATH of your application add app/model directory. So all inside can be add directly by requiring the directory name and file name.

Resources