Module: Obfuscated::ClassMethods

Defined in:
lib/obfuscated.rb

Instance Method Summary collapse

Instance Method Details

#has_obfuscated_id(options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/obfuscated.rb', line 30

def has_obfuscated_id( options={} )
  class_eval do

    include Obfuscated::InstanceMethods

    # Uses an 12 character string to find the appropriate record
    def self.find_by_hashed_id( hash, options={} )
      # Don't bother if there's no hash provided.
      return nil if hash.blank?
      
      # If Obfuscated isn't supported, use ActiveRecord's default finder
      return find_by_id(hash, options) unless Obfuscated::supported?
      
      # Update the conditions to use the hash calculation
      options.update(:conditions => ["SUBSTRING(SHA1(CONCAT('---',#{self.table_name}.id,'-WICKED-#{self.table_name}-#{Obfuscated::salt}')),1,12) = ?", hash])
      
      # Find it!
      first(options) or raise ActiveRecord::RecordNotFound, "Couldn't find #{self.class.to_s} with Hashed ID=#{hash}"
    end
  end
end