https://www.rubydoc.info/gems/tarquinn/0.5.0
This gem makes easier to controll generic redirection
Current Release: 0.5.0
Next Version 0.5.1
- Add Tarquinn to your
Gemfileandbundle install:
gem 'tarquinn'- Include Tarquinn to your controller or to your base controller
ApplicationController < ActionController::Base
include Tarquinn
end- Program your redirection on your controllers
BaseController < ApplicationController
redirection_rule :redirect_login, :loggin_needed?
private
def redirect_login
login_path
end
def loggin_needed?
user.nil?
end
end
StaticController < BaseController
skip_redirection_rule :redirect_login, :is_home?
private
def is_home?
params[:action] == 'home'
end
endTarquinn is a Rails concern that adds a before_action to any controller it is included in.
On each request the action chain works as follows:
before_action :perform_redirectionis triggered automatically.- A
RequestHandlerBuilder(stored per controller class) holds all registered redirection configs (redirection_rule/skip_redirection/skip_redirection_rulecalls). - At request time,
RequestHandlerBuildercreates aRequestHandlerfor the current request. RequestHandleriterates over everyRedirectionConfigand checks whether a redirect should fire viaRedirectionHandler.RedirectionHandlerevaluates skip conditions first; if none match, it evaluates redirect conditions. The first config that requires a redirect triggersredirect_to.
Prerequisites: Docker and Docker Compose installed.
Build the image
docker compose build base_build
# or
### Running Locally with Docker Compose and Makefile
| `make build` | Build the Docker image |
| `make dev` | Open an interactive bash shell inside the container |
| `make test` | Run RSpec tests inside the container |
make buildRun the test suite
make testsOpen an interactive shell inside the container
make dev| Target | Description |
|---|---|
make build |
Build Docker image for the project |
make dev |
Run development environment (bash shell) |
make tests |
Run tests (RSpec) in the container |
The CircleCI pipeline (.circleci/config.yml) runs three jobs:
| Job | What it does |
|---|---|
test |
bundle exec rspec + uploads coverage report |
checks |
RuboCop, Yardstick coverage, README version check, RubyCritic, unit-test structure check |
build-and-release |
Builds and pushes the gem (tags only, on master) |
Run the same checks locally before opening a PR:
# Tests
bundle exec rspec
# RuboCop
bundle exec rubocop
# YARD documentation coverage
bundle exec rake verify_measurementsYou can specify a domain option in your redirection_rule to allow cross-domain redirection. When set, the redirection will be allowed to external hosts and the specified domain will be used for validation.
The domain: option accepts either a String (static domain) or a Symbol (dynamic domain resolved at runtime by calling the named method on the controller).
Static domain (String):
class ExternalRedirectController < ApplicationController
include Tarquinn
redirection_rule :redirect_external, domain: 'example.com' do
params[:should_redirect]
end
private
def redirect_external
'/external_path'
end
endDynamic domain (Symbol):
class ExternalRedirectController < ApplicationController
include Tarquinn
redirection_rule :redirect_external, domain: :current_domain do
params[:should_redirect]
end
private
def redirect_external
'/external_path'
end
def current_domain
# resolved at request time
request.subdomain == 'admin' ? 'admin.example.com' : 'example.com'
end
end- If
domainis not set, redirection is only allowed for the same host. - If
domainis a String, it is used directly as the domain prefix. - If
domainis a Symbol and the controller responds to that method, the method is called and its return value is used as the domain. - If
domainis a Symbol but the controller does not respond to it, the Symbol's string representation is used as the domain.