Module: Hashid::Rails::ClassMethods
- Defined in:
- lib/hashid/rails.rb
Instance Method Summary collapse
- #decode_id(ids) ⇒ Object
- #encode_id(ids) ⇒ Object
- #find(hashid) ⇒ Object
-
#find_by_hashid(hashid) ⇒ Object
Calls ‘find` with decoded hashid.
- #hashids ⇒ Object
Instance Method Details
#decode_id(ids) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/hashid/rails.rb', line 57 def decode_id(ids) if ids.is_a?(Array) ids.map { |id| hashid_decode(id) } else hashid_decode(ids) end end |
#encode_id(ids) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/hashid/rails.rb', line 49 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 |
# File 'lib/hashid/rails.rb', line 65 def find(hashid) model_reload? ? super(hashid) : super( decode_id(hashid) || hashid ) end |
#find_by_hashid(hashid) ⇒ Object
Calls ‘find` with decoded hashid
70 71 72 |
# File 'lib/hashid/rails.rb', line 70 def find_by_hashid(hashid) find_by!(id: hashid_decode(hashid)) end |
#hashids ⇒ Object
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/hashid/rails.rb', line 38 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 |