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.2.14 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.13 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.12 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.11 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.10 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.9 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.8 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.7 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.6 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.5 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.4 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.3 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.2 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.1 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.2.0 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.0.2 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.0.1 app/models/concerns/obfuscates_id.rb
bullet_train-obfuscates_id-1.0.0 app/models/concerns/obfuscates_id.rb