@@ -430,140 +430,138 @@ def using_limitable_reflections?(reflections)
430
430
reflections . none? ( &:collection? )
431
431
end
432
432
433
- private
433
+ def find_with_ids ( *ids )
434
+ raise UnknownPrimaryKey . new ( @klass ) if primary_key . nil?
434
435
435
- def find_with_ids ( * ids )
436
- raise UnknownPrimaryKey . new ( @klass ) if primary_key . nil ?
436
+ expects_array = ids . first . kind_of? ( Array )
437
+ return ids . first if expects_array && ids . first . empty ?
437
438
438
- expects_array = ids . first . kind_of? ( Array )
439
- return ids . first if expects_array && ids . first . empty?
439
+ ids = ids . flatten . compact . uniq
440
440
441
- ids = ids . flatten . compact . uniq
442
-
443
- case ids . size
444
- when 0
445
- raise RecordNotFound , "Couldn't find #{ @klass . name } without an ID"
446
- when 1
447
- result = find_one ( ids . first )
448
- expects_array ? [ result ] : result
449
- else
450
- find_some ( ids )
451
- end
452
- rescue ::RangeError
453
- raise RecordNotFound , "Couldn't find #{ @klass . name } with an out of range ID"
441
+ case ids . size
442
+ when 0
443
+ raise RecordNotFound , "Couldn't find #{ @klass . name } without an ID"
444
+ when 1
445
+ result = find_one ( ids . first )
446
+ expects_array ? [ result ] : result
447
+ else
448
+ find_some ( ids )
454
449
end
450
+ rescue ::RangeError
451
+ raise RecordNotFound , "Couldn't find #{ @klass . name } with an out of range ID"
452
+ end
455
453
456
- def find_one ( id )
457
- if ActiveRecord ::Base === id
458
- raise ArgumentError , <<-MSG . squish
459
- You are passing an instance of ActiveRecord::Base to `find`.
460
- Please pass the id of the object by calling `.id`.
461
- MSG
462
- end
463
-
464
- relation = where ( primary_key => id )
465
- record = relation . take
466
-
467
- raise_record_not_found_exception! ( id , 0 , 1 ) unless record
468
-
469
- record
454
+ def find_one ( id )
455
+ if ActiveRecord ::Base === id
456
+ raise ArgumentError , <<-MSG . squish
457
+ You are passing an instance of ActiveRecord::Base to `find`.
458
+ Please pass the id of the object by calling `.id`.
459
+ MSG
470
460
end
471
461
472
- def find_some ( ids )
473
- return find_some_ordered ( ids ) unless order_values . present?
462
+ relation = where ( primary_key => id )
463
+ record = relation . take
464
+
465
+ raise_record_not_found_exception! ( id , 0 , 1 ) unless record
474
466
475
- result = where ( primary_key => ids ) . to_a
467
+ record
468
+ end
476
469
477
- expected_size =
478
- if limit_value && ids . size > limit_value
479
- limit_value
480
- else
481
- ids . size
482
- end
470
+ def find_some ( ids )
471
+ return find_some_ordered ( ids ) unless order_values . present?
483
472
484
- # 11 ids with limit 3, offset 9 should give 2 results.
485
- if offset_value && ( ids . size - offset_value < expected_size )
486
- expected_size = ids . size - offset_value
487
- end
473
+ result = where ( primary_key => ids ) . to_a
488
474
489
- if result . size == expected_size
490
- result
475
+ expected_size =
476
+ if limit_value && ids . size > limit_value
477
+ limit_value
491
478
else
492
- raise_record_not_found_exception! ( ids , result . size , expected_size )
479
+ ids . size
493
480
end
481
+
482
+ # 11 ids with limit 3, offset 9 should give 2 results.
483
+ if offset_value && ( ids . size - offset_value < expected_size )
484
+ expected_size = ids . size - offset_value
494
485
end
495
486
496
- def find_some_ordered ( ids )
497
- ids = ids . slice ( offset_value || 0 , limit_value || ids . size ) || [ ]
487
+ if result . size == expected_size
488
+ result
489
+ else
490
+ raise_record_not_found_exception! ( ids , result . size , expected_size )
491
+ end
492
+ end
498
493
499
- result = except ( :limit , :offset ) . where ( primary_key => ids ) . records
494
+ def find_some_ordered ( ids )
495
+ ids = ids . slice ( offset_value || 0 , limit_value || ids . size ) || [ ]
500
496
501
- if result . size == ids . size
502
- pk_type = @klass . type_for_attribute ( primary_key )
497
+ result = except ( :limit , :offset ) . where ( primary_key => ids ) . records
503
498
504
- records_by_id = result . index_by ( &:id )
505
- ids . map { |id | records_by_id . fetch ( pk_type . cast ( id ) ) }
506
- else
507
- raise_record_not_found_exception! ( ids , result . size , ids . size )
508
- end
509
- end
499
+ if result . size == ids . size
500
+ pk_type = @klass . type_for_attribute ( primary_key )
510
501
511
- def find_take
512
- if loaded?
513
- records . first
514
- else
515
- @take ||= limit ( 1 ) . records . first
516
- end
502
+ records_by_id = result . index_by ( &:id )
503
+ ids . map { |id | records_by_id . fetch ( pk_type . cast ( id ) ) }
504
+ else
505
+ raise_record_not_found_exception! ( ids , result . size , ids . size )
517
506
end
507
+ end
518
508
519
- def find_take_with_limit ( limit )
520
- if loaded?
521
- records . take ( limit )
522
- else
523
- limit ( limit ) . to_a
524
- end
509
+ def find_take
510
+ if loaded?
511
+ records . first
512
+ else
513
+ @take ||= limit ( 1 ) . records . first
525
514
end
515
+ end
526
516
527
- def find_nth ( index )
528
- @offsets [ offset_index + index ] ||= find_nth_with_limit ( index , 1 ) . first
517
+ def find_take_with_limit ( limit )
518
+ if loaded?
519
+ records . take ( limit )
520
+ else
521
+ limit ( limit ) . to_a
529
522
end
523
+ end
530
524
531
- def find_nth_with_limit ( index , limit )
532
- if loaded?
533
- records [ index , limit ] || [ ]
525
+ def find_nth ( index )
526
+ @offsets [ offset_index + index ] ||= find_nth_with_limit ( index , 1 ) . first
527
+ end
528
+
529
+ def find_nth_with_limit ( index , limit )
530
+ if loaded?
531
+ records [ index , limit ] || [ ]
532
+ else
533
+ relation = if order_values . empty? && primary_key
534
+ order ( arel_attribute ( primary_key ) . asc )
534
535
else
535
- relation = if order_values . empty? && primary_key
536
- order ( arel_attribute ( primary_key ) . asc )
537
- else
538
- self
539
- end
540
-
541
- relation = relation . offset ( offset_index + index ) unless index . zero?
542
- relation . limit ( limit ) . to_a
536
+ self
543
537
end
538
+
539
+ relation = relation . offset ( offset_index + index ) unless index . zero?
540
+ relation . limit ( limit ) . to_a
544
541
end
542
+ end
545
543
546
- def find_nth_from_last ( index )
547
- if loaded?
548
- records [ -index ]
544
+ def find_nth_from_last ( index )
545
+ if loaded?
546
+ records [ -index ]
547
+ else
548
+ relation = if order_values . empty? && primary_key
549
+ order ( arel_attribute ( primary_key ) . asc )
549
550
else
550
- relation = if order_values . empty? && primary_key
551
- order ( arel_attribute ( primary_key ) . asc )
552
- else
553
- self
554
- end
555
-
556
- relation . to_a [ -index ]
557
- # TODO: can be made more performant on large result sets by
558
- # for instance, last(index)[-index] (which would require
559
- # refactoring the last(n) finder method to make test suite pass),
560
- # or by using a combination of reverse_order, limit, and offset,
561
- # e.g., reverse_order.offset(index-1).first
551
+ self
562
552
end
563
- end
564
553
565
- def find_last ( limit )
566
- limit ? records . last ( limit ) : records . last
554
+ relation . to_a [ -index ]
555
+ # TODO: can be made more performant on large result sets by
556
+ # for instance, last(index)[-index] (which would require
557
+ # refactoring the last(n) finder method to make test suite pass),
558
+ # or by using a combination of reverse_order, limit, and offset,
559
+ # e.g., reverse_order.offset(index-1).first
567
560
end
561
+ end
562
+
563
+ def find_last ( limit )
564
+ limit ? records . last ( limit ) : records . last
565
+ end
568
566
end
569
567
end
0 commit comments