diff --git a/.yourbase.yml b/.yourbase.yml index 760a392..9f2ddd7 100644 --- a/.yourbase.yml +++ b/.yourbase.yml @@ -1,6 +1,5 @@ dependencies: build: - - node:12.19.0 - ruby:2.7.2 build_targets: @@ -8,6 +7,10 @@ build_targets: commands: - gem build friendly_uuid - gem install ./friendly_uuid-*.gem + - name: release + commands: + - gem install bundler + - bash release.sh exec: environment: @@ -16,3 +19,6 @@ ci: builds: - name: default build_target: default + - name: release + build_target: release + when: tagged IS true diff --git a/README.md b/README.md index 5044242..6e54992 100644 --- a/README.md +++ b/README.md @@ -57,33 +57,33 @@ problems: - Where should `twos.dev/users/a` lead now? To address this nuance, FriendlyUUID cleverly truncates URLs _past_ -uniqueness. It truncates URLs _as long as the URL remains the eldest of its -uniqueness siblings_. +uniqueness. It keeps truncating characters _as long as the URL remains the +eldest among its collisions_. In other words, after the above example, `A`'s URL continues to be `twos.dev/users/a`, because when its UUID is truncated to `a` it remains the _eldest of all records whose UUIDs start with `a`_. And when `twos.dev/users/a` is accessed, FriendlyUUID knows that the record -being asked for is the _eldest record that starts with `a`_. +being asked for is the _eldest record_ that starts with `a`. With this tweak, records always have the shortest possible URL, their URLs never change, and one URL always points to one record—all conventional rules -about URLs are followed. +about URLs are followed, and no extra state was stored! [2]: https://www.w3.org/Provider/Style/URI #### Disadvantages There are two disadvantages to this approach. -As with many stateless algorithms, you pay in machinepower. In this case, the +1. As with many stateless algorithms, you pay in machinepower. In this case, the queries to discover the shortest-possible URL are quite expensive compared to O(1) lookups. (The query to discover a record _given_ a URL remains cheap.) -As well, the hard removal of the `A` record in the example above will cause -`twos.dev/users/a` to point to `B`, rather than to 404 as a user might -expect. This can be worked around by soft-deleting records rather than -hard-deleting them. +2. The hard removal of `A` in the above example will cause `twos.dev/users/a` +to point to `B`, rather than to 404 as a user might expect. This can be +worked around by choosing to soft-delete records whenever you would normally +hard-delete them. ### Why use UUIDs? There are a few of reasons you might want to use UUIDs as primary keys over @@ -130,16 +130,25 @@ is still considered a valid "shortening" of itself. This will mean that each resource may have multiple valid URLs. FriendlyUUID will never change the URL it generates for a given resource, but if you have old links floating around in the wild that you want to be canonicalized you -can optionally decide to forward each valid URL to the canonically valid one: +can optionally decide to forward each valid URL to the canonical one: + ```ruby -FriendlyUUID.configure do |config| - config.enforce_shortened = true +class UsersController < ApplicationController + before_action :canonicalize_path, only: [:show, :edit] + + # ... + + private + + def canonicalize_path + canonical_path = user_path(@user) + redirect_to canonical_path if canonical_path != request.fullpath + end end ``` -### Why doesn't FriendlyUUID override `id` and `id=` methods? -`id` and `id=` are sensitive methods to override, as they are used as much by -Rails internals as by your application. +Whether or not you take this step, no links will be broken by adding +FriendlyUUID to your app. ## Contributing See [CONTRIBUTING.md][contributing] diff --git a/friendly_uuid.gemspec b/friendly_uuid.gemspec index 70987ef..bb25742 100644 --- a/friendly_uuid.gemspec +++ b/friendly_uuid.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |s| s.name = "friendly_uuid" - s.version = "0.1.0" + s.version = `git tag --list HEAD | head -n 1`.strip.sub("v", "") s.date = "2020-10-11" s.summary = "Make UUIDs pretty enough for use in URLs" s.description = "FriendlyUUID shortens every UUID to have only as many characters as it needs to be unique." diff --git a/release.sh b/release.sh new file mode 100644 index 0000000..097dddc --- /dev/null +++ b/release.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +mkdir -p ~/.gem +echo "---" > ~/.gem/credentials +echo ":rubygems_api_key: $RUBYGEMS_API_KEY" >> ~/.gem/credentials +echo ":github: Bearer $GITHUB_PERSONAL_ACCESS_TOKEN" >> ~/.gem/credentials +chmod 0600 ~/.gem/credentials +rm -f *.gem +gem build friendly_uuid.gemspec +gem push *.gem +gem push *.gem --key github --host https://rubygems.pkg.github.com/glacials