Module: ActiveRecord::HashidsUri::ClassMethods

Defined in:
lib/hashids_uri.rb,
lib/hashids_uri.rb

Instance Method Summary collapse

Instance Method Details

#find(*args) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


31
32
33
34
35
36
# File 'lib/hashids_uri.rb', line 31

def find(*args)
  id = args.first
  return super if args.count != 1 || (Integer(id) rescue false)
  find_by_hash(id).tap { |result| return result unless result.nil? }
  raise ActiveRecord::RecordNotFound, "cannot find record with hash id: #{id.inspect}"
end

#find_by_hash(hash) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/hashids_uri.rb', line 22

def find_by_hash(hash)
  find(
    ::Hashids.new(
      ActiveRecord::HashidsUri.salt,
      ActiveRecord::HashidsUri.min_length,
    ).decode(hash).first
  )
end

#has_hashids_uri(options = {}) ⇒ Object



13
14
15
16
17
18
# File 'lib/hashids_uri.rb', line 13

def has_hashids_uri(options = {})
  ActiveRecord::HashidsUri.salt       = options.fetch(:salt, '')
  ActiveRecord::HashidsUri.min_length = options.fetch(:min_length, 6)

  include InstanceMethods
end