Sha256: a8bb5f786a33acaec75ec3dcad15e10e87d26223c61f73f092ad8f7041cf9c12

Contents?: true

Size: 1.48 KB

Versions: 138

Compression:

Stored size: 1.48 KB

Contents

module ObfuscatesId
  extend ActiveSupport::Concern

  class_methods do
    def hashids
      # ⚠️ Changing anything in this method will invalidate any URLs with obfuscated IDs that are in the wild.
      # You should not change any of these settings after going live unless you're OK breaking links in sent emails,
      # breaking bookmarks to pages within your application, etc. For this reason, we don't want the Hashids salt
      # configurable via an ENV value.

      # We don't include digits in our alphabet because it sometimes results in fully numeric strings being generated,
      # and we can't differentiate those from normal IDs that we still need to be able to deal with.
      @hashids ||= Hashids.new "Default ID Obfuscation Salt for #{name}", 6, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
    end

    def encode_id(id)
      hashids.encode(id)
    end

    def decode_id(id)
      return id if id.to_i > 0
      hashids.decode(id).first
    end

    def find(*ids)
      super(*ids.map { |id| decode_id(id) })
    end

    def relation
      super.tap { |r| r.extend ClassMethods }
    end

    def has_many(*args, &block)
      options = args.extract_options!
      options[:extend] = Array(options[:extend]).push(ClassMethods)
      super(*args, **options, &block)
    end

    def find_by_obfuscated_id(id)
      find_by(id: decode_id(id))
    end
  end

  def obfuscated_id
    @obfuscated_id ||= self.class.encode_id(id)
  end

  def to_param
    obfuscated_id
  end
end

Version data entries

138 entries across 138 versions & 1 rubygems

Version Path
bullet_train-obfuscates_id-1.10.0 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.9.0 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.8.5 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.8.4 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.8.3 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.8.2 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.8.1 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.8.0 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.23 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.22 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.21 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.20 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.19 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.18 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.17 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.16 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.15 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.14 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.13 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.7.12 app/models/concerns/obfuscates_id.rb