Module: HashIds::Obfuscate::ClassMethods

Defined in:
lib/hash_ids/obfuscate.rb

Instance Method Summary collapse

Instance Method Details

#find(*args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/hash_ids/obfuscate.rb', line 20

def find(*args)
  return super if block_given? || super_able?(args)

  scope = args
  if scope.kind_of?(Array)
    scope.map! do |id|
      hash_ids?(id) ? hash_ids_decode(id).first : id
    end
    scope = scope.length==1 ? scope.first : scope
  end
  super(scope)
end

#hash_ids?(ids) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
# File 'lib/hash_ids/obfuscate.rb', line 41

def hash_ids? ids
  ids = ids || ''
  ds = HashIds::hasher.decode ids.to_s
  return true unless ds.empty?
end

#hash_ids_decode(ids) ⇒ Object



37
38
39
# File 'lib/hash_ids/obfuscate.rb', line 37

def hash_ids_decode ids
  HashIds::hasher.decode ids
end

#hash_ids_encode(ids) ⇒ Object



33
34
35
# File 'lib/hash_ids/obfuscate.rb', line 33

def hash_ids_encode ids
  HashIds::hasher.encode ids
end

#super_able?(args) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
15
16
17
18
# File 'lib/hash_ids/obfuscate.rb', line 12

def super_able? args
  primary_key.nil? ||
  default_scopes.any? ||
  args.first.kind_of?(Hash) ||
  args.first.kind_of?(Symbol) ||
  columns_hash.include?(inheritance_column)
end