Module: Hashid::Rails::ClassMethods

Defined in:
lib/hashid/rails.rb

Instance Method Summary collapse

Instance Method Details

#decode_id(ids) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/hashid/rails.rb', line 56

def decode_id(ids)
  if ids.is_a?(Array)
    decoded_ids = ids.map { |id| hashid_decode(id) }
    decoded_ids.any? ? decoded_ids : nil
  else
    hashid_decode(ids)
  end
end

#encode_id(ids) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/hashid/rails.rb', line 48

def encode_id(ids)
  if ids.is_a?(Array)
    ids.map { |id| hashid_encode(id) }
  else
    hashid_encode(ids)
  end
end

#find(hashid) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/hashid/rails.rb', line 65

def find(hashid)
  if model_reload? || Hashid::Rails.configuration.disable_find
    super(hashid)
  else
    super(decode_id(hashid) || hashid)
  end
end

#find_by_hashid(hashid) ⇒ Object



73
74
75
# File 'lib/hashid/rails.rb', line 73

def find_by_hashid(hashid)
  find_by!(id: hashid_decode(hashid))
end

#hashidsObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/hashid/rails.rb', line 37

def hashids
  secret = Hashid::Rails.configuration.secret
  length = Hashid::Rails.configuration.length
  alphabet = Hashid::Rails.configuration.alphabet

  arguments = ["#{table_name}#{secret}", length]
  arguments << alphabet if alphabet.present?

  Hashids.new(*arguments)
end