module Permalinkable def self.included(base) base.extend ClassMethods end module ClassMethods def acts_as_permalinkable(options = {}) send :cattr_accessor, :permalink_options self.permalink_options = { permalinkable_attribute: :name, permalink_field_name: :permalink, length: 200, allow_change: false } self.permalink_options.update(options) if options.is_a?(Hash) send :include, InstanceMethods send :after_save, :save_permalink end def permalink_cipher cipher = OpenSSL::Cipher.new('RC4-40') cipher.encrypt cipher.key = Base64.urlsafe_decode64(Configure.key) cipher end end module InstanceMethods def to_param existing_permalink = send(permalink_options[:permalink_field_name]) (existing_permalink.present? && existing_permalink) || id.to_s end private def generate_permalink sanitized = self.send(permalink_options[:permalinkable_attribute]).gsub(/[^[:alnum:]]/, ' ').strip.gsub(/\W+/, '-') "#{fpe}-#{sanitized}"[0..permalink_options[:length]] end def save_permalink generated_permalink = generate_permalink if self.send(permalink_options[:permalink_field_name]).blank? || \ ( permalink_options[:allow_change] && generated_permalink != self.send(permalink_options[:permalink_field_name]) ) self.update_column(permalink_options[:permalink_field_name].to_s, generated_permalink) end end def fpe @fpe ||= Base64.urlsafe_encode64(self.class.permalink_cipher.update('%11s' % self.id)) end end end