README.md in arel_toolkit-0.4.0 vs README.md in arel_toolkit-0.4.1

- old
+ new

@@ -1,10 +1,10 @@ # ArelToolkit ## Overview -- [![Build Status](https://travis-ci.com/mvgijssel/arel_toolkit.svg?branch=master)](https://travis-ci.com/mvgijssel/arel_toolkit) +- [![](https://github.com/mvgijssel/arel_toolkit/workflows/CI%20-%20master/badge.svg)](https://github.com/mvgijssel/arel_toolkit/actions) - [![Maintainability](https://api.codeclimate.com/v1/badges/3ef13d1649a00a98562d/maintainability)](https://codeclimate.com/github/mvgijssel/arel_toolkit/maintainability) - [![Test Coverage](https://api.codeclimate.com/v1/badges/3ef13d1649a00a98562d/test_coverage)](https://codeclimate.com/github/mvgijssel/arel_toolkit/test_coverage) - [![Gem Version](https://badge.fury.io/rb/arel_toolkit.svg)](https://badge.fury.io/rb/arel_toolkit) - [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) - ![](http://ruby-gem-downloads-badge.herokuapp.com/arel_toolkit?type=total) @@ -82,36 +82,39 @@ Middleware sits between ActiveRecord and the database, it allows you to alter the Arel (the SQL query) before it's send to the database. Multiple middlewares are supported by passing the results from a finished middleware to the next. Next to the arel object, a context object is used that acts as a intermediate storage between middlewares. The middleware works out of the box in combination with Rails. If using ActiveRecord standalone you need to run the following **after** setting up the database connection: ```ruby -Arel::Middleware::Railtie.insert_postgresql +Arel::Middleware::Railtie.insert ``` ### Example -Create some middleware (this can be any Ruby object as long as it responds to `call`). In this example, we're creating a middleware that will reorder any query. Next to reordering, we're adding an additional middleware that prints out the result of the reorder middleware. +Create middleware which can be any Ruby object as long as it responds to `call`. Middleware accepts 2 or 3 arguments, context is optional. Calling `.call` on `next_middleware` invokes the next middleware in the chain, returning the response from the database. +In this example, we're creating a middleware that will reorder any query. Next to reordering, we're adding an additional middleware that prints out the result of the reorder middleware. + ```ruby class ReorderMiddleware - def self.call(arel, _context) + def self.call(arel, next_middleware) enhanced_arel = Arel.enhance(arel) enhanced_arel.query(class: Arel::Nodes::SelectStatement).each do |node| arel_table = node.child_at_path(['cores', 0, 'source', 'left']).object node['orders'].replace([arel_table[:id].asc]) end - arel.order(Post.arel_table[:id].asc) + new_arel = arel.order(Post.arel_table[:id].asc) + next_middleware.call(new_arel) end end class LoggingMiddleware - def self.call(arel, context) + def self.call(arel, next_middleware, context) puts "User executing query: `#{context[:current_user_id]}`" puts "Original SQL: `#{context[:original_sql]}`" puts "Modified SQL: `#{arel.to_sql}`" - - arel + + next_middleware.call(arel) end end ``` Now that we've defined our middelwares, it's time to see them in action: