Module: Hashid::Rails::ClassMethods
- Defined in:
- lib/hashid/rails.rb
Instance Method Summary collapse
- #decode_id(ids) ⇒ Object
- #encode_id(ids) ⇒ Object
- #find(*ids) ⇒ Object
- #find_by_hashid(hashid) ⇒ Object
- #find_by_hashid!(hashid) ⇒ Object
-
#has_many(*args, &block) ⇒ Object
rubocop:disable Style/PredicateName.
- #relation ⇒ Object
Instance Method Details
#decode_id(ids) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/hashid/rails.rb', line 55 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
47 48 49 50 51 52 53 |
# File 'lib/hashid/rails.rb', line 47 def encode_id(ids) if ids.is_a?(Array) ids.map { |id| hashid_encode(id) } else hashid_encode(ids) end end |
#find(*ids) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/hashid/rails.rb', line 63 def find(*ids) expects_array = ids.first.is_a?(Array) uniq_ids = ids.flatten.compact.uniq uniq_ids = uniq_ids.first unless expects_array || uniq_ids.size > 1 if Hashid::Rails.configuration.override_find super(decode_id(uniq_ids)) else super end end |
#find_by_hashid(hashid) ⇒ Object
76 77 78 |
# File 'lib/hashid/rails.rb', line 76 def find_by_hashid(hashid) find_by(id: decode_id(hashid)) end |
#find_by_hashid!(hashid) ⇒ Object
80 81 82 |
# File 'lib/hashid/rails.rb', line 80 def find_by_hashid!(hashid) find_by!(id: decode_id(hashid)) end |
#has_many(*args, &block) ⇒ Object
rubocop:disable Style/PredicateName
41 42 43 44 45 |
# File 'lib/hashid/rails.rb', line 41 def has_many(*args, &block) # rubocop:disable Style/PredicateName = args. [:extend] = Array([:extend]).push(ClassMethods) super(*args, , &block) end |
#relation ⇒ Object
37 38 39 |
# File 'lib/hashid/rails.rb', line 37 def relation super.tap { |r| r.extend ClassMethods } end |