Permalinkable, inspired by permalink

Code ClimateBuild Status

Purpose & Note

This is a gem inspired from another gem called permalink. Since I often want a permalink that provides no way to tell the database ids, I finally came up with the idea about encrypting and prepending it to the original permalink. The encryption is implemented with a simple FED algorithm. For more information about FPE(Format Preserving Encryption), please consult the wikipedia page.

The encryption method of current release is a simple "RC4-40" with a configurable key. Note that "RC4-40" is NOT a strong encryption algorithm at all, and you SHOULDN'T rely on it to delivery sensitive information. Also, to prevent inconsistency of encryption, and duplication(although the chance is very low), you should keep your key as a secret and never change it in production.

The original implementation of generating permalink involves an infite loop to check the uniqueness in database. It's slow, inefficient and most importantly, it still can't prevent race condition in rails. Since we are now using a FPE algorithm based on database id, which is garanteed to be unique from database, we don't need to put ourselves in that inefficient loop any more.

Finally, in short ,what's the purpose of this gem?

It's only a gem that helps hiding your database ids.

Instalation

gem install permalinkable

Usage

Include the Permalinkable module into your model. And call acts_as_permalinkable with some parameters.

class User < ActiveRecord::Base
  include Permalinkable
  acts_as_permalinkable permalinkable_attribute: :user_name, length: 100, allow_change: true, permalink_field_name: :permalink
end

And don't forget to create the migration for you permalink column.

Usage on options:

options descriptions
permalinkable_attribute the attribute that you want your permalink to be based on.
permalink_field_name the column name that you want to store your permalink
length the maximum length for your permalink
allow_change by default it's false so that once a permalink is set, it can never be changed. Setting it to true will allow it to be changed along with the permalinkable_attribute.

License

The MIT License (MIT)

Copyright (c) 2014 Yang Ou, released under the MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.