How to add middleware in Rails 4.2 application - ruby-on-rails

I am trying to learn Middlewares and been practising how to mount it in the Rails application. I have followed the railscast
So far I have implemented these steps:
1) Created a new Rails 4.2 application called: Blog
2) Added a file in the lib folder named as response_timer.rb.
class ResponseTimer
def initialize(app)
#app = app
end
def call(env)
[200, {"Content-Type" => "text/html"}, "Hello World"]
end
end
3) Added config.middleware.use "ResponseTimer" in application.rb.
config.middleware.use "ResponseTimer"
But as i'm hitting the command rake middleware in the terminal, it is reporting this error:
rake aborted!
NameError: uninitialized constant ResponseTimer
I tried also to add the config.middleware.use "ResponseTimer" in the development.rb but again facing the same error.
What am i missing here?
Please help.
Referenced article: http://guides.rubyonrails.org/rails_on_rack.html

Middleware has to have an accompanying module / class, and needs to be loaded in the app before it can be referenced. The way to do this in Rails is with autoloading (lib files aren't autoloaded by default):
#config/application.rb
config.autoload_paths += Dir["#{config.root}/lib/**/"]
config.middleware.use "ResponseTimer"
The above should work for you.

I followed this answer: https://stackoverflow.com/a/24122424
I tried it before but maybe missed something before.
In appliation.rb
require 'rails/all'
require_relative '../lib/response_timer'
module Blog
class Application < Rails::Application
...
config.middleware.use ResponseTimer
end
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.

Rails 5.2 autoload is not working in development

I have a project on Rails 5.2 with the following structure:
app/
app/auth
app/auth/oauth_controller.rb
app/auth/oauth.rb
oauth_controller.rb
require_relative "./oauth.rb"
module Auth
class OauthController < Infra::BaseController
include ActionController::Cookies
def start
oauth = Auth::OAuth.new(session: session)
...
end
def callback
oauth = Auth::OAuth.new(session: session)
...
end
end
end
oauth.rb
module Auth
class OAuth
...
end
end
To have the Auth::Oauth working I have to require the oauth.rb file, so I think the eager loading or autoload are not working. But, even with the require(), when I change the file, I get this error again and I have to restart the server again and again.
uninitialized constant Auth::OAuth
Here is my application.rb
config.middleware.use ActionDispatch::Cookies
config.api_only = false
config.eager_load_paths += %W(#{config.root}/app)
config.time_zone = 'Etc/UTC'
config.reload_controllers = !Rails.env.production?
The development.rb was not changed.
It's because of naming convention rails expects. Rails is expecting a file name o_auth.rb to match OAuth. You need to add an infection to support OAuth as oauth.rb
In config/initializers/inflections.rb add
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'OAuth'
end
OR you need to change your file name to o_auth.rb
In both cases you do not need require_relative "./oauth.rb"
In addition, if this is a controller it should live in app/controllers/auth/o_auth and not directly in app/
In addition to the other answer, the folders under app are not interpreted as modules and are for organization only. So app/auth/oauth.rb Should look like
class Oauth
Not
module Auth
class Oauth

Rails: Loading custom class from lib folder in controller

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')

Correct way to load middleware for rails and rspec

So... I have a bit of a conundrum.
I've written some rack middleware, it's stored on disk at app/middleware/eat_bacon.rb, it looks something like
module Middleware
class EatBacon
def initialize(app)
#app = app
end
def call(env)
Thread.current[:mouth] = 'Bacon'
#app.call(env)
end
end
end
I'm trying to load / use some middleware in a rails-api (rails 3.2.19)
In my config/application.rb I've gotten the middleware to load in two different ways, one way works when running the app, the other way works for rspec
Works for running app
config.middleware.insert_before 0, 'Middleware::EatBacon'
but when I run rspec, I get
/Volumes/HardDrive/Me/.rvm/gems/ruby-2.0.0-p451#bacon/gems/activesupport-3.2.19/lib/active_support/inflector/methods.rb:230:in `block in constantize': uninitialized constant Middleware (NameError)
Works for RSpec
config.after_initialize do
config.middleware.insert_before 0, 'Middleware::EatBacon'
end
But now the middleware is not loaded when the application runs, ie Thread.current[:mouth] never gets bacon
Seems weird to have to implement this differently in config/environments/test.rb and config/environments/production.rb, but I guess thats what I'll end up doing unless someone has a better idea
The way I solved this was to first move the middleware to the lib folder:
lib/middleware/eats_bacon.rb
AND Then, in config/application.rb I added a require_relative, so that file now looks like
...
Bundler.require(*Rails.groups)
require_relative '../lib/middleware/eats_bacon.rb'
module BaconEaterApp
class Application < Rails::Application
...
Then at the bottom of that file
...
# Middleware
config.middleware.insert_before 0, 'Middleware::EatBacon'
...

Rails 3 does not see my class in "lib" directory

I've placed the file rack_app.rb with simple Rack application in the lib directory:
class RackApp
def call env
[200, {}, 'Hello']
end
end
Then I've added this route:
match 'rack' => RackApp
And when I try to launch the rails server I get the following error:
config/routes.rb:65: uninitialized constant RackApp (NameError)
Rails 3 has no more autoloading by default. So you need require your file
require 'lib/rack_app.rb'
Or come back the autoloading in application.rb
config.autoload_paths += %W( #{config.root}/lib )
Include require 'email_format_validator' in the model.

Resources