Skip to content

Commit

Permalink
[CIOPS-1082] Dynamic routing (#2)
Browse files Browse the repository at this point in the history
### Description

This pull request changes the way the Engine is mounted, using an
initializer or default settings. This will allow us to change the
settings in case there's a conflict with the selected routing.

### Changes

* Mount the engine dynamically
* Reload routes for testing
* Updated the documentation

### Ticket

[CIOPS-1082](https://customink.atlassian.net/browse/CIOPS-1082)
  • Loading branch information
Arne De Herdt authored Sep 15, 2022
2 parents b2db6c4 + 148afeb commit 1d5ad36
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Is It Ready? CHANGELOG

## 0.0.3
* Make the mounting of the engine dynamic based upon the configuration

## 0.0.2
* Enabled the CircleCI Pipeline
* Added the test matrix
Expand Down
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ end
With the above snippet, the health check will be available under:

* https://your-domain/is_it_ready
* https://your-domain/is_it_ready/is_it_ready

## Configuration

Expand All @@ -68,8 +67,7 @@ or conflicts with another plugin. In this case, creating an initializer under `c
# Overwrite the endpoint that's used for listening to the required calls from the ReadinessProbe.
# Setting this value, changes the second entry to be the path defined here, as well as the path under which
# the application has been mounted:
# * https://your-domain/<mount-path>
# * https://your-domain/<mount-path>/something_else
# * https://your-domain/something_else
# This is more for cosmetic purposes, or when mountain multiple engines under the same endpoint with distinct routes.
::IsItReady.endpoint = '/something_else'
```
Expand Down
1 change: 0 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,5 @@
# This follows the same pattern as standard Ruby on Rails routing, but is scoped to
# just the Rails Engine.
::IsItReady::Engine.routes.draw do
get ::IsItReady.endpoint => 'application#is_it_ready'
root :to => 'application#is_it_ready'
end
8 changes: 8 additions & 0 deletions lib/is_it_ready/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,13 @@ module IsItReady
# how users will use this Engine in their applications.
class Engine < ::Rails::Engine
isolate_namespace IsItReady

# Initialize the engine dynamically based upon the configuration.
# This allows us to specify the configuration for the engine in the host application instead.
initializer 'add_routing_paths' do |app|
app.routes.append do
mount ::IsItReady::Engine => ::IsItReady.endpoint
end
end
end
end
2 changes: 1 addition & 1 deletion lib/is_it_ready/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module IsItReady
VERSION = '0.0.2'
VERSION = '0.0.3'
end
1 change: 0 additions & 1 deletion test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
Rails.application.routes.draw do

mount IsItReady::Engine => "/is_it_ready"
end
30 changes: 30 additions & 0 deletions test/integration/custom_navigation_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'test_helper'

module IsItReady
class CustomNavigationTest < ActionDispatch::IntegrationTest
include Engine.routes.url_helpers

setup do
::IsItReady.endpoint = '/something_else'
Rails.application.reload_routes!
end

test('it returns the correct response status on the root') do
get root_url

assert_response :success
end

test('it returns the correct output on the root') do
get root_url

response = ::JSON.parse(@response.body, symbolize_names: true)

assert_equal({ :status => "ok", :code => 200 }, response)
end

test('it returns the not found response status on the default path') do
assert_raises(ActionController::RoutingError) { get DEFAULT_PATH }
end
end
end
14 changes: 0 additions & 14 deletions test/integration/navigation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,5 @@ class NavigationTest < ActionDispatch::IntegrationTest

assert_equal({ :status => "ok", :code => 200 }, response)
end

test('it returns the correct response status on the default path') do
get DEFAULT_PATH

assert_response :success
end

test('it returns the correct output on the default path') do
get DEFAULT_PATH

response = ::JSON.parse(@response.body, symbolize_names: true)

assert_equal({ :status => "ok", :code => 200 }, response)
end
end
end

0 comments on commit 1d5ad36

Please sign in to comment.