|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Hashable Concern |
| 4 | +# Adds HashID encoding/decoding capabilities to ActiveRecord models with UUID primary keys |
| 5 | +# |
| 6 | +# @see config/initializers/hashid.rb |
| 7 | + |
| 8 | +module Hashable |
| 9 | + extend ActiveSupport::Concern |
| 10 | + |
| 11 | + included do |
| 12 | + def hashid |
| 13 | + return nil if id.blank? |
| 14 | + |
| 15 | + numeric_id = uuid_to_numeric(id) |
| 16 | + |
| 17 | + hashids_instance.encode(numeric_id) |
| 18 | + rescue StandardError => e |
| 19 | + Rails.logger.error "[HASHID] Failed to encode #{self.class.name}##{id}: #{e.message}" |
| 20 | + Rails.logger.error e.backtrace.first(3).join("\n") |
| 21 | + nil |
| 22 | + end |
| 23 | + |
| 24 | + # Alternative shorter method name |
| 25 | + alias_method :to_hashid, :hashid |
| 26 | + |
| 27 | + # @example |
| 28 | + # vod.public_hashid_url # => "https://prostaff.gg/vod-reviews/Zx1U3mA7caXq" |
| 29 | + def public_hashid_url |
| 30 | + return nil unless hashid.present? |
| 31 | + return nil unless ENV['FRONTEND_URL'].present? |
| 32 | + |
| 33 | + "#{ENV['FRONTEND_URL']}/#{self.class.name.underscore.pluralize}/#{hashid}" |
| 34 | + end |
| 35 | + |
| 36 | + private |
| 37 | + |
| 38 | + # Get Hashids instance with proper configuration |
| 39 | + # @return [Hashids] Configured Hashids instance |
| 40 | + def hashids_instance |
| 41 | + salt = ENV.fetch('HASHID_SALT', 'development_fallback_salt') |
| 42 | + min_length = ENV.fetch('HASHID_MIN_LENGTH', '6').to_i |
| 43 | + alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' |
| 44 | + |
| 45 | + full_salt = "#{salt}#{self.class.table_name}" |
| 46 | + |
| 47 | + Hashids.new(full_salt, min_length, alphabet) |
| 48 | + end |
| 49 | + |
| 50 | + # Convert UUID to numeric value |
| 51 | + # @param uuid [String] UUID string (e.g., "5c8d9b3e-3155-4871-a419-e72ad5f21c19") |
| 52 | + # @return [Integer] Numeric representation (128-bit integer) |
| 53 | + def uuid_to_numeric(uuid) |
| 54 | + uuid.delete('-').to_i(16) |
| 55 | + end |
| 56 | + end |
| 57 | + |
| 58 | + class_methods do |
| 59 | + # Find record by HashID |
| 60 | + # @param hashid [String] The HashID to decode |
| 61 | + # @return [ActiveRecord::Base, nil] The found record or nil |
| 62 | + # @example |
| 63 | + # VodReview.find_by_hashid("mA7cXq") # => #<VodReview:0x00...> |
| 64 | + def find_by_hashid(hashid) |
| 65 | + return nil if hashid.blank? |
| 66 | + |
| 67 | + numeric_id = hashids_instance.decode(hashid).first |
| 68 | + return nil if numeric_id.nil? |
| 69 | + |
| 70 | + uuid = numeric_to_uuid(numeric_id) |
| 71 | + find_by(id: uuid) |
| 72 | + rescue StandardError => e |
| 73 | + Rails.logger.error "[HASHID] Failed to decode hashid '#{hashid}' for #{name}: #{e.message}" |
| 74 | + Rails.logger.error e.backtrace.first(3).join("\n") |
| 75 | + nil |
| 76 | + end |
| 77 | + |
| 78 | + def find_by_hashid!(hashid) |
| 79 | + find_by_hashid(hashid) or raise ActiveRecord::RecordNotFound, "Couldn't find #{name} with hashid=#{hashid}" |
| 80 | + end |
| 81 | + |
| 82 | + private |
| 83 | + |
| 84 | + # Get Hashids instance with proper configuration (class method) |
| 85 | + # @return [Hashids] Configured Hashids instance |
| 86 | + def hashids_instance |
| 87 | + salt = ENV.fetch('HASHID_SALT', 'development_fallback_salt') |
| 88 | + min_length = ENV.fetch('HASHID_MIN_LENGTH', '6').to_i |
| 89 | + alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' |
| 90 | + |
| 91 | + # Add table name as pepper |
| 92 | + full_salt = "#{salt}#{table_name}" |
| 93 | + |
| 94 | + Hashids.new(full_salt, min_length, alphabet) |
| 95 | + end |
| 96 | + |
| 97 | + # Convert numeric value to UUID string |
| 98 | + # @param numeric [Integer] Numeric representation of UUID (128-bit integer) |
| 99 | + # @return [String] UUID string (e.g., "5c8d9b3e-3155-4871-a419-e72ad5f21c19") |
| 100 | + def numeric_to_uuid(numeric) |
| 101 | + # Convert to hex and pad to 32 characters |
| 102 | + hex = numeric.to_s(16).rjust(32, '0') |
| 103 | + |
| 104 | + # Format as UUID: 8-4-4-4-12 |
| 105 | + "#{hex[0..7]}-#{hex[8..11]}-#{hex[12..15]}-#{hex[16..19]}-#{hex[20..31]}" |
| 106 | + end |
| 107 | + end |
| 108 | +end |
0 commit comments