tarquinn 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -7
  3. data/.github/copilot-instructions.md +188 -0
  4. data/Dockerfile +1 -1
  5. data/Gemfile +7 -7
  6. data/Makefile +21 -0
  7. data/README.md +108 -5
  8. data/config/check_specs.yml +1 -0
  9. data/config/yardstick.yml +26 -4
  10. data/docker-compose.yml +1 -1
  11. data/lib/tarquinn/class_methods.rb +96 -7
  12. data/lib/tarquinn/condition/action_checker.rb +21 -0
  13. data/lib/tarquinn/condition/method_caller.rb +21 -0
  14. data/lib/tarquinn/condition/proc_runner.rb +18 -0
  15. data/lib/tarquinn/condition.rb +8 -0
  16. data/lib/tarquinn/controller.rb +10 -7
  17. data/lib/tarquinn/exception.rb +20 -0
  18. data/lib/tarquinn/redirection_config/options.rb +58 -0
  19. data/lib/tarquinn/redirection_config.rb +90 -7
  20. data/lib/tarquinn/redirection_config_builder.rb +88 -0
  21. data/lib/tarquinn/redirection_handler.rb +106 -9
  22. data/lib/tarquinn/request_handler.rb +43 -15
  23. data/lib/tarquinn/request_handler_builder.rb +11 -5
  24. data/lib/tarquinn/version.rb +4 -1
  25. data/lib/tarquinn.rb +39 -16
  26. data/spec/dummy/app/controllers/tarquinn/dummy_controller.rb +1 -1
  27. data/spec/lib/tarquinn/exception/redirection_already_defined_spec.rb +13 -0
  28. data/spec/lib/tarquinn/redirection_config/options_spec.rb +45 -0
  29. data/spec/lib/tarquinn/redirection_config_builder_spec.rb +57 -0
  30. data/spec/lib/tarquinn/redirection_config_spec.rb +1 -1
  31. data/spec/lib/tarquinn/redirection_handler_spec.rb +23 -4
  32. data/spec/lib/tarquinn/request_handler_builder_spec.rb +30 -0
  33. data/spec/lib/tarquinn/request_handler_spec.rb +2 -2
  34. data/spec/lib/tarquinn_spec.rb +26 -0
  35. data/tarquinn.gemspec +2 -1
  36. metadata +24 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 40a2cd722ae376e627aba7f4d4e10b69df45b74593d89dbafd68b9f6e4cb635a
4
- data.tar.gz: 7c29d8a9763a3f0ae3e71945ea16b690628b0a3fef4f3603d891dbe791a4a0ca
3
+ metadata.gz: 472e75e26a2d619f0284c9eb797c5e47f717a9f9a6e52757fc1c5ad7272a174f
4
+ data.tar.gz: efb31ef0021491a3b99c48cb8c3a34026493a10500636449195c1070d281dc91
5
5
  SHA512:
6
- metadata.gz: 5b63bfd5ba62e690b145bc4394e1913f28e5756b325b8301fb1b467d1386b282950095d36638ee407c2404e30d098a79de7868285a390b643fb2e84b275f16c6
7
- data.tar.gz: '0286cd856bbaa74643cf9174952aff220ed75e3b81ff22f07c451b95bf226a78919ac8e306bbde78b9ab2f0577cd7b707bf4b0f4d25da69830b5340d58bfc5d9'
6
+ metadata.gz: 9391c038495ea739fee48b43a9c104bcb0dfb887aa87ecb75be3d1378ee2c11649cc68725fd8beea215036065b5deb1aeca08babea956d77b42628b887ead261
7
+ data.tar.gz: 59ccde1cade0e2b76b82f89073409cf9ced274d7346214ae08f2d4e6f06e11269feb9b768c219064bcec0e748205c1ce6a5eb45515e21dbe404a53459cb6643b
data/.circleci/config.yml CHANGED
@@ -18,7 +18,7 @@ workflows:
18
18
  only: /\d+\.\d+\.\d+/
19
19
  branches:
20
20
  only:
21
- - master
21
+ - main
22
22
  jobs:
23
23
  test:
24
24
  docker:
@@ -27,18 +27,12 @@ jobs:
27
27
  PROJECT: tarquinn
28
28
  steps:
29
29
  - checkout
30
- - run:
31
- name: Prepare Coverage Test Report
32
- command: cc-test-reporter before-build
33
30
  - run:
34
31
  name: Bundle Install
35
32
  command: bundle install
36
33
  - run:
37
34
  name: RSpec
38
35
  command: bundle exec rspec
39
- - run:
40
- name: Coverage Test Report
41
- command: cc-test-reporter after-build --exit-code $?
42
36
  checks:
43
37
  docker:
44
38
  - image: darthjee/circleci_ruby_331:1.0.1
@@ -0,0 +1,188 @@
1
+ # GitHub Copilot Instructions for Tarquinn
2
+
3
+ ## Project Overview
4
+
5
+ Tarquinn is a Ruby gem that provides generic redirection control for Rails controllers.
6
+ It is implemented as an `ActiveSupport::Concern` that adds `before_action` hooks and
7
+ class-level DSL methods (`redirection_rule`, `skip_redirection`, `skip_redirection_rule`)
8
+ to any Rails controller.
9
+
10
+ Key components:
11
+
12
+ - **`Tarquinn`** – The main concern included in controllers; sets up `before_action :perform_redirection`.
13
+ - **`Tarquinn::ClassMethods`** – DSL methods added to controller classes.
14
+ - **`Tarquinn::RequestHandlerBuilder`** – Accumulates redirection configurations for a controller class.
15
+ - **`Tarquinn::RequestHandler`** – Processes a single request, iterating over configs to find the first matching redirect.
16
+ - **`Tarquinn::RedirectionHandler`** – Evaluates one redirection config (conditions + skip rules) for a request.
17
+ - **`Tarquinn::RedirectionConfig`** – Holds the data for a single redirection rule (blocks, skip blocks, redirect target).
18
+ - **`Tarquinn::Condition`** – Wraps a single condition (method name or block) used in redirection/skip evaluation.
19
+ - **`Tarquinn::Controller`** – Thin wrapper around the Rails controller instance, used by handlers.
20
+
21
+ ## Redirection Rules Data Flow
22
+
23
+ ### How It Works
24
+
25
+ 1. A controller includes `Tarquinn` (an `ActiveSupport::Concern`), which:
26
+ - Extends the controller with `Tarquinn::ClassMethods`, making the DSL methods available.
27
+ - Registers a `before_action :perform_redirection` that runs on every request.
28
+
29
+ 2. During a request, `perform_redirection` delegates to `Tarquinn::RequestHandler`, which
30
+ iterates over all registered `Tarquinn::RedirectionConfig` objects (in definition order)
31
+ and applies the **first** rule whose conditions are satisfied.
32
+
33
+ 3. Each rule is evaluated by `Tarquinn::RedirectionHandler`:
34
+ - All condition methods and/or the optional block are evaluated against the current
35
+ controller instance.
36
+ - If **any** condition returns a truthy value the rule fires and the controller redirects
37
+ to the path returned by the rule's target method.
38
+ - Skip conditions (registered via `skip_redirection` or `skip_redirection_rule`) are
39
+ checked first; if any skip condition is truthy the rule is bypassed entirely.
40
+
41
+ ### Defining a Redirection Rule — `redirection_rule`
42
+
43
+ ```ruby
44
+ redirection_rule <rule_name>, *<condition_methods>, &<block>
45
+ ```
46
+
47
+ | Argument | Description |
48
+ |---------------------|----------------------------------------------------------------------------------------------|
49
+ | `rule_name` | Symbol. Also the name of the controller method that returns the redirect path. |
50
+ | `condition_methods` | Zero or more symbol method names. Each is called on the controller; truthy → rule fires. |
51
+ | `block` | Optional. Evaluated in the context of the controller; truthy → rule fires. |
52
+
53
+ **Evaluation**: the rule fires when **any** condition method **or** the block returns truthy.
54
+
55
+ #### Example 1 — block-based rule
56
+
57
+ ```ruby
58
+ redirection_rule :redirect_home { params[:secret_key] != '12345' }
59
+
60
+ def redirect_home
61
+ home_path
62
+ end
63
+ ```
64
+
65
+ Redirects to `home_path` if `secret_key` is absent or different from `'12345'`.
66
+
67
+ #### Example 2 — method-based rule
68
+
69
+ ```ruby
70
+ redirection_rule :redirect_home, :misses_secret_key
71
+
72
+ def redirect_home
73
+ home_path
74
+ end
75
+
76
+ def misses_secret_key
77
+ params[:secret_key] != '12345'
78
+ end
79
+ ```
80
+
81
+ Same behaviour as Example 1, but the condition is extracted into a dedicated method.
82
+
83
+ ### Skipping a Rule for Specific Actions — `skip_redirection`
84
+
85
+ ```ruby
86
+ skip_redirection <rule_name>, *<actions>
87
+ ```
88
+
89
+ Prevents the named rule from running for the listed controller actions.
90
+
91
+ ```ruby
92
+ skip_redirection :redirect_home, :index
93
+ ```
94
+
95
+ The `index` action is not affected by the `redirect_home` rule; all other actions still are.
96
+
97
+ ### Skipping a Rule Under a Condition — `skip_redirection_rule`
98
+
99
+ ```ruby
100
+ skip_redirection_rule <rule_name>, *<condition_methods>, &<block>
101
+ ```
102
+
103
+ Registers a skip condition for a rule. When the condition is truthy the rule is bypassed,
104
+ regardless of the action.
105
+
106
+ ```ruby
107
+ skip_redirection_rule :redirect_home { params[:admin_key] == '9999' }
108
+ ```
109
+
110
+ The `redirect_home` rule does not fire when the correct admin key is provided.
111
+
112
+ ### Passing Options to Redirection Rules
113
+
114
+ You can pass options to `redirection_rule` to control advanced behavior. Currently, the main supported option is `domain`, which enables cross-domain redirection.
115
+
116
+ | Option | Description |
117
+ |----------|---------------------------------------------------------------------------------------------|
118
+ | `domain` | String. If set, allows redirection to the specified domain (cross-domain). If not set, only same-host redirection is allowed. |
119
+
120
+ #### Example — domain-based rule
121
+
122
+ ```ruby
123
+ redirection_rule :redirect_external, domain: 'example.com' do
124
+ params[:should_redirect]
125
+ end
126
+
127
+ def redirect_external
128
+ '/external_path'
129
+ end
130
+ ```
131
+
132
+ This will only allow redirection to `https://example.com/external_path` because the domain option is set to `'example.com'`.
133
+
134
+ ## Language
135
+
136
+ All code, comments, documentation, commit messages, and PR descriptions must be written in **English**.
137
+
138
+ ## Coding Standards
139
+
140
+ ### General Style
141
+
142
+ - Follow the existing RuboCop configuration (`.rubocop.yml` / `.rubocop_todo.yml`).
143
+ - Use `# frozen_string_literal: true` at the top of every Ruby file.
144
+ - Keep methods small and focused (Sandi Metz style): aim for methods under 5 lines.
145
+ - Keep classes small with clear, single responsibilities and explicit object boundaries.
146
+ - Avoid deeply nested conditionals; extract guard clauses or helper methods.
147
+ - Use `private` / `attr_reader` to hide implementation details.
148
+ - Use `delegate` (ActiveSupport) instead of manual forwarding where it improves readability.
149
+
150
+ ### Documentation
151
+
152
+ - Add **YARD** documentation to all public classes, modules, and methods.
153
+ - Non-obvious private methods should also have short YARD comments.
154
+ - Always include `@param`, `@return`, and `@api public` / `@api private` tags where applicable.
155
+ - Ensure documentation coverage stays at or above the threshold in `config/yardstick.yml`.
156
+
157
+ ### Tests
158
+
159
+ - Write **RSpec** tests using the patterns established in `spec/`.
160
+ - Keep tests small and focused — one behaviour per example.
161
+ - Use `let` definitions that are **reusable** and **avoid duplication** across contexts.
162
+ - Prefer `context` blocks to describe different input/state scenarios.
163
+ - Avoid `before` blocks that do more than one thing; split them if needed.
164
+ - Use `shared_examples` or `shared_context` when the same setup appears in multiple groups.
165
+
166
+ ## CI Checks
167
+
168
+ The CI pipeline (`.circleci/config.yml`) runs the following checks on every push.
169
+ Reproduce them locally before opening a PR:
170
+
171
+ | CI Step | Local Command |
172
+ |-------------------------------|--------------------------------------------------|
173
+ | Run tests | `bundle exec rspec` |
174
+ | RuboCop lint | `bundle exec rubocop` |
175
+ | YARD documentation coverage | `bundle exec rake verify_measurements` |
176
+ | RubyCritic quality check | `rubycritic.sh` (or `bundle exec rubycritic`) |
177
+
178
+ All checks must pass before a PR can be merged.
179
+
180
+ ## Docker / Local Development
181
+
182
+ To run the project locally, use the Makefile targets:
183
+
184
+ - Build Docker image: `make build`
185
+ - Start development environment: `make dev`
186
+ - Run tests: `make tests`
187
+
188
+ See the [Developer Guide](../README.md#developer-guide) in the README for further instructions.
data/Dockerfile CHANGED
@@ -1,4 +1,4 @@
1
- FROM darthjee/scripts:0.4.2 as scripts
1
+ FROM darthjee/scripts:0.6.0 as scripts
2
2
 
3
3
  FROM darthjee/ruby_331:1.0.1 as base
4
4
 
data/Gemfile CHANGED
@@ -5,14 +5,14 @@ source 'https://rubygems.org'
5
5
  gem 'bundler', '~> 2.5.13'
6
6
  gem 'pry', '~> 0.14.2'
7
7
  gem 'pry-nav', '~> 1.0.0'
8
- gem 'rails', '7.0.4.3'
8
+ gem 'rails', '7.2.3'
9
9
  gem 'rake', '~> 13.1.0'
10
10
  gem 'rspec', '~> 3.12.0'
11
- gem 'rspec-rails', '6.0.2'
12
- gem 'rubycritic', '4.9.1'
13
- gem 'simplecov', '~> 0.22.0'
14
- gem 'sqlite3', '1.4.2'
15
- gem 'yard', '0.9.36'
16
- gem 'yardstick', '0.9.9'
11
+ gem 'rspec-rails', '6.1.1'
12
+ gem 'rubycritic', '4.9.1'
13
+ gem 'simplecov', '~> 0.22.0'
14
+ gem 'sqlite3', '1.4.2'
15
+ gem 'yard', '0.9.38'
16
+ gem 'yardstick', '0.9.9'
17
17
 
18
18
  gemspec
data/Makefile ADDED
@@ -0,0 +1,21 @@
1
+ .PHONY: build dev tests
2
+
3
+ PROJECT?=tarquinn
4
+ IMAGE?=$(PROJECT)
5
+ BASE_IMAGE?=$(DOCKER_ID_USER)/$(PROJECT)-base
6
+ DOCKER_FILE_BASE=Dockerfile.$(PROJECT)-base
7
+
8
+ all:
9
+ @echo "Usage:"
10
+ @echo " make build\n Build docker image for $(PROJECT)"
11
+ @echo " make dev\n Run development environment for $(PROJECT)"
12
+ @echo " make tests\n Run tests for $(PROJECT)"
13
+
14
+ build:
15
+ docker build -f Dockerfile.$(PROJECT) . -t $(IMAGE) -t $(PUSH_IMAGE):latest
16
+
17
+ tests:
18
+ docker-compose run $(PROJECT)_tests /bin/bash
19
+
20
+ dev:
21
+ docker-compose run $(PROJECT) /bin/bash
data/README.md CHANGED
@@ -2,8 +2,6 @@ Tarquinn
2
2
  ========
3
3
  [![Build Status](https://circleci.com/gh/darthjee/tarquinn.svg?style=shield)](https://circleci.com/gh/darthjee/tarquinn)
4
4
  [![Code Climate](https://codeclimate.com/github/darthjee/tarquinn/badges/gpa.svg)](https://codeclimate.com/github/darthjee/tarquinn)
5
- [![Test Coverage](https://codeclimate.com/github/darthjee/tarquinn/badges/coverage.svg)](https://codeclimate.com/github/darthjee/tarquinn/coverage)
6
- [![Issue Count](https://codeclimate.com/github/darthjee/tarquinn/badges/issue_count.svg)](https://codeclimate.com/github/darthjee/tarquinn)
7
5
  [![Gem Version](https://badge.fury.io/rb/tarquinn.svg)](https://badge.fury.io/rb/tarquinn)
8
6
  [![Inline docs](http://inch-ci.org/github/darthjee/tarquinn.svg)](http://inch-ci.org/github/darthjee/tarquinn)
9
7
 
@@ -11,13 +9,13 @@ Tarquinn
11
9
 
12
10
  Yard Documentation
13
11
  -------------------
14
- [https://www.rubydoc.info/gems/tarquinn/0.3.0](https://www.rubydoc.info/gems/tarquinn/0.3.0)
12
+ [https://www.rubydoc.info/gems/tarquinn/0.4.0](https://www.rubydoc.info/gems/tarquinn/0.4.0)
15
13
 
16
14
  This gem makes easier to controll generic redirection
17
15
 
18
- Current Release: [0.3.0](https://github.com/darthjee/tarquinn/tree/0.3.0)
16
+ Current Release: [0.4.0](https://github.com/darthjee/tarquinn/tree/0.4.0)
19
17
 
20
- [Next release](https://github.com/darthjee/tarquinn/compare/0.3.0...master)
18
+ Next Version [0.4.1](https://github.com/darthjee/tarquinn/compare/0.4.0...main)
21
19
 
22
20
  Getting started
23
21
  ---------------
@@ -60,3 +58,108 @@ Getting started
60
58
  end
61
59
  end
62
60
  ```
61
+
62
+ ---
63
+
64
+ ## Developer Guide
65
+
66
+ ### How It Works
67
+
68
+ Tarquinn is a Rails concern that adds a `before_action` to any controller it is included in.
69
+ On each request the action chain works as follows:
70
+
71
+ 1. `before_action :perform_redirection` is triggered automatically.
72
+ 2. A `RequestHandlerBuilder` (stored per controller class) holds all registered redirection
73
+ configs (`redirection_rule` / `skip_redirection` / `skip_redirection_rule` calls).
74
+ 3. At request time, `RequestHandlerBuilder` creates a `RequestHandler` for the current request.
75
+ 4. `RequestHandler` iterates over every `RedirectionConfig` and checks whether a redirect
76
+ should fire via `RedirectionHandler`.
77
+ 5. `RedirectionHandler` evaluates skip conditions first; if none match, it evaluates redirect
78
+ conditions. The first config that requires a redirect triggers `redirect_to`.
79
+
80
+ ### Running Locally with Docker Compose
81
+
82
+ > Prerequisites: Docker and Docker Compose installed.
83
+
84
+ **Build the image**
85
+
86
+ ```bash
87
+ docker compose build base_build
88
+ # or
89
+ ### Running Locally with Docker Compose and Makefile
90
+
91
+ | `make build` | Build the Docker image |
92
+
93
+ | `make dev` | Open an interactive bash shell inside the container |
94
+
95
+ | `make test` | Run RSpec tests inside the container |
96
+ make build
97
+ ```
98
+
99
+ **Run the test suite**
100
+
101
+ ```bash
102
+ make tests
103
+ ```
104
+
105
+ **Open an interactive shell inside the container**
106
+
107
+ ```bash
108
+ make dev
109
+ ```
110
+
111
+ ### Makefile Targets
112
+
113
+ | Target | Description |
114
+ |--------------|---------------------------------------------|
115
+ | `make build` | Build Docker image for the project |
116
+ | `make dev` | Run development environment (bash shell) |
117
+ | `make tests` | Run tests (RSpec) in the container |
118
+
119
+ ### CI Checks
120
+
121
+ The CircleCI pipeline (`.circleci/config.yml`) runs three jobs:
122
+
123
+ | Job | What it does |
124
+ |-----------------------|------------------------------------------------------------------------------------------|
125
+ | `test` | `bundle exec rspec` + uploads coverage report |
126
+ | `checks` | RuboCop, Yardstick coverage, README version check, RubyCritic, unit-test structure check |
127
+ | `build-and-release` | Builds and pushes the gem (tags only, on `master`) |
128
+
129
+ Run the same checks locally before opening a PR:
130
+
131
+ ```bash
132
+ # Tests
133
+ bundle exec rspec
134
+
135
+ # RuboCop
136
+ bundle exec rubocop
137
+
138
+ # YARD documentation coverage
139
+ bundle exec rake verify_measurements
140
+ ```
141
+
142
+ ### Domain Option for Cross-Domain Redirection
143
+
144
+ You 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.
145
+
146
+ **Example:**
147
+
148
+ ```ruby
149
+ class ExternalRedirectController < ApplicationController
150
+ include Tarquinn
151
+
152
+ redirection_rule :redirect_external, domain: 'example.com' do
153
+ params[:should_redirect]
154
+ end
155
+
156
+ private
157
+
158
+ def redirect_external
159
+ '/external_path'
160
+ end
161
+ end
162
+ ```
163
+
164
+ - If `domain` is not set, redirection is only allowed for the same host.
165
+ - If `domain` is set, redirection to the specified domain is allowed.
@@ -1,3 +1,4 @@
1
1
  ignore:
2
2
  - lib/tarquinn/class_methods.rb
3
3
  - lib/tarquinn/version.rb
4
+ - lib/tarquinn/exception.rb
data/config/yardstick.yml CHANGED
@@ -1,4 +1,4 @@
1
- threshold: 88.1
1
+ threshold: 100.0
2
2
  require_exact_threshold: false
3
3
  rules:
4
4
  ApiTag::Presence:
@@ -15,13 +15,35 @@ rules:
15
15
  exclude: []
16
16
  ExampleTag:
17
17
  enabled: true
18
- exclude: []
18
+ exclude:
19
+ - Tarquinn.redirection_rule
20
+ - Tarquinn.skip_redirection
21
+ - Tarquinn.skip_redirection_rule
19
22
  ReturnTag:
20
23
  enabled: true
21
- exclude: []
24
+ exclude:
25
+ - Tarquinn.redirection_rule
26
+ - Tarquinn.skip_redirection
27
+ - Tarquinn.skip_redirection_rule
28
+ - Tarquinn.redirector_builder
29
+ - Tarquinn#perform_redirection
30
+ - Tarquinn::RedirectionHandler#initialize
31
+ - Tarquinn::RequestHandler#initialize
32
+ - Tarquinn::RedirectionConfigBuilder#initialize
33
+ - Tarquinn::Condition::ProcRunner#initialize
34
+ - Tarquinn::Condition::MethodCaller#initialize
35
+ - Tarquinn::Condition::ActionChecker#initialize
36
+ - Tarquinn::Exception::RedirectionAlreadyDefined#initialize
22
37
  Summary::Presence:
23
38
  enabled: true
24
- exclude: []
39
+ exclude:
40
+ - Tarquinn::RedirectionHandler#initialize
41
+ - Tarquinn::RequestHandler#initialize
42
+ - Tarquinn::RedirectionConfigBuilder#initialize
43
+ - Tarquinn::Condition::ProcRunner#initialize
44
+ - Tarquinn::Condition::MethodCaller#initialize
45
+ - Tarquinn::Condition::ActionChecker#initialize
46
+ - Tarquinn::Exception::RedirectionAlreadyDefined#initialize
25
47
  Summary::Length:
26
48
  enabled: true
27
49
  exclude: []
data/docker-compose.yml CHANGED
@@ -17,7 +17,7 @@ services:
17
17
  depends_on: [base_build]
18
18
  command: /bin/bash -c 'rspec'
19
19
 
20
- test_all:
20
+ tarquinn_tests:
21
21
  <<: *base
22
22
  depends_on: [base_build]
23
23
  command: /bin/bash -c 'rspec && yard && rake yardstick_measure && rake verify_measurements'
@@ -3,16 +3,64 @@
3
3
  module Tarquinn
4
4
  # @api public
5
5
  #
6
- # Methods added by Tarquinn
6
+ # Methods added by {Tarquinn}
7
7
  module ClassMethods
8
8
  # Creates a redirection rule
9
9
  #
10
10
  # The rule name defines which method will be called when checking the path of redirection
11
11
  #
12
12
  # @param (see Tarquinn::RequestHandlerBuilder#add_redirection_config)
13
+ # @option (see Tarquinn::RequestHandlerBuilder#add_redirection_config)
13
14
  # @return (see Tarquinn::RequestHandlerBuilder#add_redirection_config)
14
- def redirection_rule(redirection, *methods, &)
15
- redirector_builder.add_redirection_config(redirection, *methods, &)
15
+ #
16
+ # @see Tarquinn::RequestHandlerBuilder
17
+ # @see Tarquinn::RedirectionConfig::Options
18
+ #
19
+ # @example A redirection with block style condition
20
+ # class ApplicationController < ActionController::Base
21
+ # include Tarquinn
22
+ #
23
+ # redirection_rule :redirect_old_path do |redirection|
24
+ # redirection.path == '/old_path'
25
+ # end
26
+ #
27
+ # private
28
+ #
29
+ # def redirect_old_path
30
+ # '/new_path'
31
+ # end
32
+ # end
33
+ #
34
+ # @example A redirection with method style condition
35
+ # class ApplicationController < ActionController::Base
36
+ # include Tarquinn
37
+ #
38
+ # redirection_rule :redirect_old_path, :perform_redirection_for_old_path?
39
+ #
40
+ # private
41
+ #
42
+ # def perform_redirection_for_old_path?
43
+ # request.path == '/old_path'
44
+ # end
45
+ #
46
+ # def redirect_old_path
47
+ # '/new_path'
48
+ # end
49
+ # end
50
+ #
51
+ # @example A redirection with domain options
52
+ # class ApplicationController < ActionController::Base
53
+ # include Tarquinn
54
+ # redirection_rule :redirect_external_path, domain: 'example.com' do |redirection|
55
+ # redirection.path == '/new_path'
56
+ # end
57
+ # private
58
+ # def redirect_external_path
59
+ # '/new_path'
60
+ # end
61
+ # end
62
+ def redirection_rule(redirection, *methods, **options, &block)
63
+ redirector_builder.add_redirection_config(redirection, *methods, **options, &block)
16
64
  end
17
65
 
18
66
  # Attaches a condition to skip a redirection based on route (controller action)
@@ -21,23 +69,64 @@ module Tarquinn
21
69
  #
22
70
  # @param (see Tarquinn::RequestHandlerBuilder#add_skip_action)
23
71
  # @return (see Tarquinn::RequestHandlerBuilder#add_skip_action)
72
+ #
73
+ # @example Skip a redirection for a specific group of controller actions
74
+ # class ApplicationController < ActionController::Base
75
+ # include Tarquinn
76
+ # redirection_rule :redirect_old_path
77
+ # skip_redirection :redirect_old_path, :new, :create, :update
78
+ # end
24
79
  def skip_redirection(redirection, *actions)
25
80
  redirector_builder.add_skip_action(redirection, *actions)
26
81
  end
27
82
 
28
83
  # Attaches conditions to skip a redirection
29
84
  #
30
- # Methods and blocks are ran and if any returns true, the redirec is skipped
85
+ # Methods and blocks are ran and if any returns true, the redirect is skipped
31
86
  #
32
87
  # @param (see Tarquinn::RequestHandlerBuilder#add_skip_config)
33
88
  # @return (see Tarquinn::RequestHandlerBuilder#add_skip_config)
89
+ #
90
+ # @example A redirection with method style condition
91
+ # class ApplicationController < ActionController::Base
92
+ # include Tarquinn
93
+ #
94
+ # redirection_rule :redirect_old_path
95
+ # skip_redirection_rule :redirect_old_path, :skip_redirection_for_old_path?
96
+ #
97
+ # private
98
+ #
99
+ # def skip_redirection_for_old_path?
100
+ # request.path != '/old_path'
101
+ # end
102
+ #
103
+ # def redirect_old_path
104
+ # '/new_path'
105
+ # end
106
+ # end
107
+ #
108
+ # @example A redirection with block style condition
109
+ # class ApplicationController < ActionController::Base
110
+ # include Tarquinn
111
+ #
112
+ # redirection_rule :redirect_old_path do |redirection|
113
+ # redirection.path == '/old_path'
114
+ # end
115
+ #
116
+ # private
117
+ #
118
+ # def redirect_old_path
119
+ # '/new_path'
120
+ # end
121
+ # end
34
122
  def skip_redirection_rule(redirection, *methods, &block)
35
- redirector_builder.add_skip_config(redirection, *methods, block)
123
+ redirector_builder.add_skip_config(redirection, *methods, &block)
36
124
  end
37
125
 
38
- # Retruns the RequestHandlerBuilder
126
+ # @api private
127
+ # Returns the RequestHandlerBuilder
39
128
  #
40
- # RequestHandlerBuilder will Carry all the configurations and will create
129
+ # RequestHandlerBuilder will carry all the configurations and will create
41
130
  # one {RequestHandler} for each request
42
131
  #
43
132
  # @return [RequestHandlerBuilder]
@@ -6,14 +6,29 @@ module Tarquinn
6
6
  #
7
7
  # Checks condition based on route action
8
8
  class ActionChecker < Tarquinn::Condition
9
+ # @param routes [Array<Symbol>] controller actions that trigger the condition
10
+ #
11
+ # @return [Tarquinn::Condition::ActionChecker]
9
12
  def initialize(routes)
10
13
  @routes = [routes].flatten.map(&:to_s)
11
14
  end
12
15
 
16
+ # Checks if the current request action matches any of the configured routes
17
+ #
18
+ # @param controller [Tarquinn::Controller] the controller with the request params
19
+ #
20
+ # @return [TrueClass] when the current action is in the routes list
21
+ # @return [FalseClass] when the current action is not in the routes list
13
22
  def check?(controller)
14
23
  routes.include? controller.params[:action]
15
24
  end
16
25
 
26
+ # Checks equality with another object
27
+ #
28
+ # @param other [Object] the object to compare with
29
+ #
30
+ # @return [TrueClass] when the other object is of the same class and has the same routes
31
+ # @return [FalseClass] otherwise
17
32
  def ==(other)
18
33
  return false unless other.class == self.class
19
34
 
@@ -22,6 +37,12 @@ module Tarquinn
22
37
 
23
38
  protected
24
39
 
40
+ # @api private
41
+ # @private
42
+ #
43
+ # Controller actions that trigger the condition
44
+ #
45
+ # @return [Array<String>]
25
46
  attr_reader :routes
26
47
  end
27
48
  end