Module: EncodedId::Rails::FinderMethods
- Defined in:
- lib/encoded_id/rails/finder_methods.rb
Overview
Provides finder methods for locating records by their encoded IDs.
Instance Method Summary collapse
- #find_all_by_encoded_id(encoded_id) ⇒ Object
- #find_all_by_encoded_id!(encoded_id) ⇒ Object
-
#find_by_encoded_id(encoded_id, with_id: nil) ⇒ Object
Find by encoded ID and optionally ensure record ID is the same as constraint (can be slugged).
- #find_by_encoded_id!(encoded_id, with_id: nil) ⇒ Object
Instance Method Details
#find_all_by_encoded_id(encoded_id) ⇒ Object
36 37 38 39 40 |
# File 'lib/encoded_id/rails/finder_methods.rb', line 36 def find_all_by_encoded_id(encoded_id) decoded_ids = decode_encoded_id(encoded_id) return if decoded_ids.blank? where(id: decoded_ids).to_a end |
#find_all_by_encoded_id!(encoded_id) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/encoded_id/rails/finder_methods.rb', line 43 def find_all_by_encoded_id!(encoded_id) decoded_ids = decode_encoded_id(encoded_id) raise ActiveRecord::RecordNotFound if decoded_ids.nil? || decoded_ids.blank? records = where(id: decoded_ids).to_a raise ActiveRecord::RecordNotFound if records.blank? || records.size != decoded_ids.size records end |
#find_by_encoded_id(encoded_id, with_id: nil) ⇒ Object
Find by encoded ID and optionally ensure record ID is the same as constraint (can be slugged)
15 16 17 18 19 20 21 22 |
# File 'lib/encoded_id/rails/finder_methods.rb', line 15 def find_by_encoded_id(encoded_id, with_id: nil) decoded_id = decode_encoded_id(encoded_id) return if decoded_id.nil? || decoded_id.blank? record = find_by(id: decoded_id.first) return unless record return if with_id && with_id != record.send(:id) record end |
#find_by_encoded_id!(encoded_id, with_id: nil) ⇒ Object
25 26 27 28 29 30 31 32 33 |
# File 'lib/encoded_id/rails/finder_methods.rb', line 25 def find_by_encoded_id!(encoded_id, with_id: nil) decoded_id = decode_encoded_id(encoded_id) raise ActiveRecord::RecordNotFound if decoded_id.nil? || decoded_id.blank? record = find_by(id: decoded_id.first) if !record || (with_id && with_id != record.send(:id)) raise ActiveRecord::RecordNotFound end record end |