Module: EncodedId::Rails::FinderMethods

Defined in:
lib/encoded_id/rails/finder_methods.rb

Instance Method Summary collapse

Instance Method Details

#find_by_encoded_id(slugged_encoded_id, with_id: nil) ⇒ Object

Find by encoded ID and optionally ensure record ID is the same as constraint (can be slugged)



7
8
9
10
11
12
13
14
# File 'lib/encoded_id/rails/finder_methods.rb', line 7

def find_by_encoded_id(slugged_encoded_id, with_id: nil)
  decoded_id = decode_encoded_id(slugged_encoded_id)
  return if decoded_id.blank?
  record = find_by(id: decoded_id)
  return unless record
  return if with_id && with_id != record.send(:id)
  record
end

#find_by_encoded_id!(slugged_encoded_id, with_id: nil) ⇒ Object

Raises:

  • (ActiveRecord::RecordNotFound)


16
17
18
19
20
21
22
23
24
# File 'lib/encoded_id/rails/finder_methods.rb', line 16

def find_by_encoded_id!(slugged_encoded_id, with_id: nil)
  decoded_id = decode_encoded_id(slugged_encoded_id)
  raise ActiveRecord::RecordNotFound if decoded_id.blank?
  record = find_by(id: decoded_id)
  if !record || (with_id && with_id != record.send(:id))
    raise ActiveRecord::RecordNotFound
  end
  record
end