Module: EncodedId::Rails::ActiveRecordFinders::ClassMethodsPrepend

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

Overview

Class methods for overriding ActiveRecord's finder methods to decode encoded IDs.

Instance Method Summary collapse

Instance Method Details

#find(*args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/encoded_id/rails/active_record_finders.rb', line 37

def find(*args)
  first_arg = args.first
  return super unless args.size == 1 && first_arg.is_a?(String)

  decoded_ids = decode_encoded_id(first_arg)

  if decoded_ids.blank?
    raise ::ActiveRecord::RecordNotFound
  elsif decoded_ids.size == 1
    super(decoded_ids.first)
  else
    super(decoded_ids)
  end
end

#find_by_id(id) ⇒ Object

Override find_by_id to handle encoded IDs



54
55
56
57
58
59
60
61
# File 'lib/encoded_id/rails/active_record_finders.rb', line 54

def find_by_id(id)
  if id.is_a?(String)
    decoded_ids = decode_encoded_id(id)
    return nil if decoded_ids.blank?
    return super(decoded_ids.first)
  end
  super
end