Decidim::UrlAliases
is a Decidim module to allow admins to create url aliases for resources within a Decidim::ParticipatorySpace
.
The module is based on the Redirector gem and creates an interface for admins to manage redirect rules. Redirect rules have two parts: the source defines how to match the incoming request path and the destination is where to send the visitor if the match is made.
The gem enforces the following restrictions to redirect rules:
- Only paths that match public routes within participatory spaces are allowed as destination. Exception will be made for paths starting with "/uploads" (see configuration).
- Paths that can conflict with existing decidim routes are not allowed as source. These paths are computed dinamically to allow for changes in decidim. After installation, take a look at
config/url_aliases/reserved_paths.yml
to see which ones they are in your application or take a look at default_reserved_paths.yml to make yourself an idea. You can expand this list at installation level (see configuration). - Redirect rules will only have effect when visiting the host of the
Decidim::Organization
in which they were created.
Add this line to your application's Gemfile:
gem "decidim-url_aliases", git: "https://github.com/OpenSourcePolitics/decidim-urlaliases.git"
And then execute:
bundle
bundle exec rake url_aliases:init
Inside your configuration in config/application.rb
of your Rails application you can set the following:
# This option silences the logging of Redirector related SQL queries in your log file
config.redirector.silence_sql_logs = true
By overriding the following constants you can:
- Add exeptions to destination validations. Any string that starts with a whitelisted prefix will be allowed as destination.
- Add restrictions for source validation. Any string which is equal to a reserved path will not be allowed as source.
# config/initializers/url_aliases.rb
module Decidim
module UrlAliases
RouteRecognizer::NEW_RESERVED_PATHS = %w(/custom_path) # default: []
RouteRecognizer::WHITELISTED_PREFIXES = %w(/pages) # default: ["/uploads"]
end
end
Once the gem installed, login to the application as an administrator and go to the Admin panel, click on the tab "Url Aliases".
There you'll see the list of aliases you created already.
Create a new redirect rule by clicking on "Add". Choose a custom path as a source, copy the path to a resource in your app as destination and make sure to check "Active" so that the redirection is effective. Disabling the "active" active checkbox will render the redirection inopperant. Check case sensitive if your source contains caps.
Test your alias by inputing in your navigator and see yourself redirected to the destination that you chose.
Note: the redirect response status is HTTP 302 to prevent caching.
-
Run
bundle exec rake test_app
. -
Run tests with
bundle exec rspec
Decidim::UrlAliases
depends directly on Decidim::Core
in 0.16.1
version.
See Decidim.
This engine is distributed under the GNU AFFERO GENERAL PUBLIC LICENSE.