I am following this tutorial
https://www.youtube.com/watch?v=fgn12-Yc-Sg
I have in my application.rb
config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')
config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]
Then on my app folder I have an api folder and inside of that the module folder converter
/app/api/converter/currency.rb
module Converter
class Currency
resource :converter do
get :exchange do
params
end
end
end
end
Now rails finds the Converter module on the console but it gives error for the Currency class
Related
i have a rails 5 app and some of my directories in app/ dont autoload. how do i set the app to automatically load stuff in directories like:
app/workflows
app/validators
whether it be specs or a real server?
i tried:
config.autoload_paths << Rails.root.join('app/*')
or
config.autoload_paths << Rails.root.join('app/validators')
but it doesnt work. how can i just load every file in app/ directory?
EDIT
one of the classes that i need to have manually loaded in specs:
module Validator
class Token < Base
validate :date_correctness
def initialize(decoded_auth_token: decoded_auth_token)
#expiration_date = decoded_auth_token[:expiration_date]
end
private
attr_reader :expiration_date
def date_correctness
return true if Date.parse(expiration_date) >= Date.today
errors.add(:token, 'is expired')
end
end
end
app/validators/token.rb
Try something like this in application.rb
config.autoload_paths += Dir[Rails.root.join('app', 'workflows', '{*/}')]
config.autoload_paths += Dir[Rails.root.join('app', 'validators', '{*/}')]
I have a standard Rails 5.2 application and I would like to add a method to an Array class.
So I have created a file in lib/core_extensions/array/use_slugs.rb with this code:
module CoreExtensions
module Array
def use_slugs
binding.pry
end
end
end
Array.include CoreExtensions::Array
and in my config/application.rb file I have added:
class Application < Rails::Application
...
config.eager_load_paths << Rails.root.join('lib')
config.eager_load_paths << Rails.root.join('lib', 'core_extensions', '**/')
...
end
But still when I call [].use_slugs I get undefined method 'use_slugs' for []:Array
Why?
Thanks
Ok, for someone else. I have decided no to load the entire lib folder in the application.rb file at all, because generally you don't want to load all rake tasks etc. (so I have deleted the config.eager_load_paths << Rails.root.join('lib') and config.eager_load_paths << Rails.root.join('lib', 'core_extensions', '**/') lines from application.rb)
I have kept the folder structure in the lib folder same, but I'm loading only particular files and lib subfolders in the config/initializers/lib_init.rb file I have created, like this:
require Rails.root.join('lib', 'sp_form_builder.rb')
Dir[ Rails.root.join('lib', 'core_extensions', '**') ].each { |f| require f }
I've created a file as lib/services/my_service.rb.
# /lib/services/my_service.rb
class MyService
...
end
I want to use it in app/controllers/my_controller
class MyController < ApplicationController
def method
service = MyService.new()
end
I'm getting an error that MyService is an uninitialized constant. I've tried to import it with
require '/lib/services/my_service.rb'
But I'm getting
cannot load such file -- /lib/services/my_service.rb
Edit: I have tried autoloading from application.rb using
config.autoload_paths << Rails.root.join('lib')
But no dice. Still getting uninitialized constant MyController::MyService
Ruby on Rails requires following certain naming conventions to support autoloading.
Rails can autoload a file located at lib/services/my_service.rb if the model/class structure was Services::MyService.
Change your lib/services/my_service.rb to:
module Services
class MyService
# ...
end
end
And use that class like this in your controller:
service = Services::MyService.new
Please note that depending on your Ruby on Rails version, you might need to add the lib folder to the list of folders which are queried when looking for a file to autoload:
# add this line to your config/application.rb:
config.autoload_paths << "#{Rails.root}/lib"
Read more about autoloading in the Rails Guides.
You probably need to enable the autoload from the files in the lib/ folder:
# config/application.rb
config.autoload_paths << "#{Rails.root}/lib"
If you prefer to do it "manually", then you can only require such file in the same file:
# config/application.rb
require './lib/my_service'
After this a restart is necessary.
there is a setting in config/application.rb in which you can specify directories that contain files you want autoloaded.
From application.rb:
# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)
or
config.autoload_paths += Dir["#{config.root}/lib/**/"]
rails 3
Dir["lib/**/*.rb"].each do |path|
require_dependency path
end
Add this in your application.rb
config.eager_load_paths << Rails.root.join('lib/services')
In rails project, I made api folder and I added this code to my application.rb file:
config.paths.add File.join('app', 'api'), glob: File.join('**', '*.rb')
config.autoload_paths += Dir[Rails.root.join('app', 'api', '*')]
In my api folder I have created game_server.rb file:
module GameServer
module Entities
class Test < Grape::Entity
expose :id
end
end
class API < Grape::API
version 'v1', using: :path
prefix :api
format :json
get :details do
present Basis.all, with: GameServer::Entities::Test
end
end
end
All code inside GameServer module. When I hit http://localhost:3000/api/v1/details in my browser I het this error:
uninitialized constant Grape::Entity.
I even tried to put my Entities module in other file, still does not work.
WHY?
You are using old version of grape, change your grape version:
gem 'grape', '~> 0.11.0'
You can refer to this repository: https://github.com/philcallister/rails-grape-entity
Just add
gem 'grape'
gem 'grape-entity'
into your Gemfile
Adding lib to config autoload paths does not autoload my module in Rails 3.
I add in my config/application.rb file.
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
In my controller I added
require 'lib_util' (or)
include LibUtil #both doesn't work
In my lib/lib_util.rb file, I have the following module
module LibUtil
module ClassMethods
def p_key(a,b)
//mycode
end
end
def self.included(receiver)
receiver.extend ClassMethods
end
end
I get the error undefined method `p_key'. The important thing to be noted is I ve called the same module in my model it works fine. But in my controller it does not identify the module.
Can anybody guide me??
Did you try including both the modules ?
include LibUtil::ClassMethods