@@ -399,8 +399,8 @@ def exposures
399
399
end
400
400
401
401
def valid_exposures
402
- exposures . reject { | _ , options | options [ :nested ] } . select do |attribute , exposure_options |
403
- valid_exposure? ( attribute , exposure_options )
402
+ exposures . select do |attribute , exposure_options |
403
+ ! exposure_options [ :nested ] && valid_exposure? ( attribute , exposure_options )
404
404
end
405
405
end
406
406
@@ -432,7 +432,7 @@ def serializable_hash(runtime_options = {})
432
432
output [ self . class . key_for ( attribute ) ] =
433
433
if partial_output . respond_to? ( :serializable_hash )
434
434
partial_output . serializable_hash ( runtime_options )
435
- elsif partial_output . is_a? ( Array ) && ! partial_output . map { |o | o . respond_to? ( :serializable_hash ) } . include? ( false )
435
+ elsif partial_output . is_a? ( Array ) && partial_output . all? { |o | o . respond_to? ( :serializable_hash ) }
436
436
partial_output . map ( &:serializable_hash )
437
437
elsif partial_output . is_a? ( Hash )
438
438
partial_output . each do |key , value |
@@ -518,13 +518,24 @@ def self.key_for(attribute)
518
518
exposures [ attribute . to_sym ] [ :as ] || name_for ( attribute )
519
519
end
520
520
521
- def self . nested_exposures_for ( attribute )
522
- nested_exposures [ attribute ] || { }
521
+ def self . nested_exposures_for? ( attribute )
522
+ nested_exposures . key? ( attribute )
523
+ end
524
+
525
+ def nested_value_for ( attribute , options )
526
+ nested_exposures = self . class . nested_exposures [ attribute ]
527
+ nested_attributes =
528
+ nested_exposures . map do |nested_attribute , nested_exposure_options |
529
+ if conditions_met? ( nested_exposure_options , options )
530
+ [ self . class . key_for ( nested_attribute ) , value_for ( nested_attribute , options ) ]
531
+ end
532
+ end
533
+
534
+ Hash [ nested_attributes . compact ]
523
535
end
524
536
525
537
def value_for ( attribute , options = { } )
526
538
exposure_options = exposures [ attribute . to_sym ]
527
- nested_exposures = self . class . nested_exposures_for ( attribute )
528
539
529
540
if exposure_options [ :using ]
530
541
exposure_options [ :using ] = exposure_options [ :using ] . constantize if exposure_options [ :using ] . respond_to? :constantize
@@ -551,15 +562,8 @@ def value_for(attribute, options = {})
551
562
instance_exec ( delegate_attribute ( attribute ) , &format_with )
552
563
end
553
564
554
- elsif nested_exposures . any?
555
- nested_attributes =
556
- nested_exposures . map do |nested_attribute , nested_exposure_options |
557
- if conditions_met? ( nested_exposure_options , options )
558
- [ self . class . key_for ( nested_attribute ) , value_for ( nested_attribute , options ) ]
559
- end
560
- end
561
-
562
- Hash [ nested_attributes . compact ]
565
+ elsif self . class . nested_exposures_for? ( attribute )
566
+ nested_value_for ( attribute , options )
563
567
else
564
568
delegate_attribute ( attribute )
565
569
end
@@ -585,8 +589,7 @@ def delegate_attribute(attribute)
585
589
end
586
590
587
591
def valid_exposure? ( attribute , exposure_options )
588
- nested_exposures = self . class . nested_exposures_for ( attribute )
589
- ( nested_exposures . any? && nested_exposures . all? { |a , o | valid_exposure? ( a , o ) } ) || \
592
+ ( self . class . nested_exposures_for? ( attribute ) && self . class . nested_exposures [ attribute ] . all? { |a , o | valid_exposure? ( a , o ) } ) || \
590
593
exposure_options . key? ( :proc ) || \
591
594
!exposure_options [ :safe ] || \
592
595
object . respond_to? ( self . class . name_for ( attribute ) ) || \
0 commit comments