The Railtie, the Gem and the Middleware
Tags: ruby, rails
Published: 2023-09-26 04:34:01

I had the luck to interview Alexis Bernard about his works for my podcast. You should check this out and test one of his gems RoRvsWild.

I was surprised when I installed the gem. The middleware was directly available in the rails Middlewares list without any action except: `bundle install` and `rails s`.

I wanted to understand how this is possible in Rails.

## Create our Gem

Since a part the blog post's subject is creating a gem, let's create one. This is simple. You can follow the following simple steps : 

1. Create a gemspec file like the following

```ruby
#middlwar.gemspec
	Gem::Specification.new do |s|
  s.name        = "middlwar"
  s.version     = "0.0.0"
  s.summary     = "middl"
  s.authors     = ["Lucas Barret"]
  s.files       = ["lib/middlwar.rb"]
end
```

2. Create the code of your gem
We will cover this in the second part of the article :)

3. Build the gem with : 
```shell
gem build my_middlware.gemspec
```

If you want to have more information and dig even deeper, you can check in the guides of [rubygems website](https://guides.rubygems.org/make-your-own-gem/).

Okay, since we know how to create the gem, we can know focus in the second part of the article how to create a middleware, use it as gem and fin insert it in rails Middlewares list.

## Our Gem code

This is the problem of the eggs or the chicken. But here I will begin with my middleware code.

To create a middleware for a rails app. All you have to do is creating a class that looks like this one : 

```ruby
class MyMiddleware
	def initialize(app)
	  @app = app
	end

	def call(env)
	  return [200, {}, ['I am a middleware']]
	end
end
```

This middleware get the HTTP request and always send back a response with HTTP code 200 and 'I am a middleware' as a body.

But this won't appear in your app as is, you need to something more. Initialize your middleware and insert it in your rails app.

```ruby
#lib/middlwar.rb
class MyRailtie < Rails::Railtie
	def self.insert_middleware
	  initializer "middle.configure_rails_initialization" do |app|
	    app.middleware.unshift MyMiddleware
	  end
    end 
	insert_middleware
end

```

What I want to put stress on is the Middleware part and especially the Railties.

As usual, going through documentation cannot make anything worse—and going to the Rails [documentation](https://api.rubyonrails.org/classes/Rails/Railtie.html) espcially.

We can learn that Railties are the core of the Rails Framework and can modify or extend the initialization process of Rails. 

Every significant component of Rails, like ActiveRecord, implements a Railtie. 
And they are responsible for their initialization.

Could we say that Rails is a collection of useful gem that makes sense all together ? 

Whatever but here thanks to the railtie we initiatilize how own middleware/gem. 

I have define a class level method and then call it directly from inside my class. (Yeah remember even class code is executed when parsed by ruby interpreter).

And that's it my middleware is instantiate and thanks to the `unshift` method put at the top of the middleware list.

## Put it in the Rails app

This is maybe the simplest thing in this article : 
We will not push this gem on the Rubygems website, but we can include our gem code locally. 

In your gem file, you can add a line like this : 

```ruby
gem "my_middlware", path: "../my_middlware"
```

Now go for a `bundle install`.

After that, you can load your middleware by restarting your server or just listing your server' Middlewares with `rails middleware`

## Wrap all the things up

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p36zq8rp2sufg4l0543o.jpg)


That's all folks, we have done a lot today. Creating a middleware extract it as a gem, and insert it in our Rails app as the first middleware. Eventually we even learn how to use local gem. 

I hope to see you for another article, in the mean time if you have question do not hesitate to comment :).