@@ -25,6 +25,125 @@ def self.name
25
25
@instance_options = { }
26
26
end
27
27
28
+ def test_reflection_value
29
+ serializer_class = Class . new ( ActiveModel ::Serializer ) do
30
+ has_one :blog
31
+ end
32
+ serializer_instance = serializer_class . new ( @model , @instance_options )
33
+
34
+ # Get Reflection
35
+ reflection = serializer_class . _reflections . fetch ( :blog )
36
+
37
+ # Assert
38
+ assert_nil reflection . block
39
+ assert_equal Serializer . config . include_data_default , reflection . instance_variable_get ( :@_include_data )
40
+ assert_equal true , reflection . instance_variable_get ( :@_include_data )
41
+
42
+ include_slice = :does_not_matter
43
+ assert_equal @model . blog , reflection . value ( serializer_instance , include_slice )
44
+ end
45
+
46
+ def test_reflection_value_block
47
+ serializer_class = Class . new ( ActiveModel ::Serializer ) do
48
+ has_one :blog do
49
+ object . blog
50
+ end
51
+ end
52
+ serializer_instance = serializer_class . new ( @model , @instance_options )
53
+
54
+ # Get Reflection
55
+ reflection = serializer_class . _reflections . fetch ( :blog )
56
+
57
+ # Assert
58
+ assert_respond_to reflection . block , :call
59
+ assert_equal Serializer . config . include_data_default , reflection . instance_variable_get ( :@_include_data )
60
+ assert_equal true , reflection . instance_variable_get ( :@_include_data )
61
+
62
+ include_slice = :does_not_matter
63
+ assert_equal @model . blog , reflection . value ( serializer_instance , include_slice )
64
+ end
65
+
66
+ def test_reflection_value_block_with_explicit_include_data_true
67
+ serializer_class = Class . new ( ActiveModel ::Serializer ) do
68
+ has_one :blog do
69
+ include_data true
70
+ object . blog
71
+ end
72
+ end
73
+ serializer_instance = serializer_class . new ( @model , @instance_options )
74
+
75
+ # Get Reflection
76
+ reflection = serializer_class . _reflections . fetch ( :blog )
77
+
78
+ # Assert
79
+ assert_respond_to reflection . block , :call
80
+ assert_equal Serializer . config . include_data_default , reflection . instance_variable_get ( :@_include_data )
81
+ assert_equal true , reflection . instance_variable_get ( :@_include_data )
82
+
83
+ include_slice = :does_not_matter
84
+ assert_equal @model . blog , reflection . value ( serializer_instance , include_slice )
85
+ end
86
+
87
+ def test_reflection_value_block_with_include_data_false_mutates_the_reflection_include_data
88
+ serializer_class = Class . new ( ActiveModel ::Serializer ) do
89
+ has_one :blog do
90
+ include_data false
91
+ object . blog
92
+ end
93
+ end
94
+ serializer_instance = serializer_class . new ( @model , @instance_options )
95
+
96
+ # Get Reflection
97
+ reflection = serializer_class . _reflections . fetch ( :blog )
98
+
99
+ # Assert
100
+ assert_respond_to reflection . block , :call
101
+ assert_equal true , reflection . instance_variable_get ( :@_include_data )
102
+ include_slice = :does_not_matter
103
+ assert_nil reflection . value ( serializer_instance , include_slice )
104
+ assert_equal false , reflection . instance_variable_get ( :@_include_data )
105
+ end
106
+
107
+ def test_reflection_value_block_with_include_data_if_sideloaded_included_mutates_the_reflection_include_data
108
+ serializer_class = Class . new ( ActiveModel ::Serializer ) do
109
+ has_one :blog do
110
+ include_data :if_sideloaded
111
+ object . blog
112
+ end
113
+ end
114
+ serializer_instance = serializer_class . new ( @model , @instance_options )
115
+
116
+ # Get Reflection
117
+ reflection = serializer_class . _reflections . fetch ( :blog )
118
+
119
+ # Assert
120
+ assert_respond_to reflection . block , :call
121
+ assert_equal true , reflection . instance_variable_get ( :@_include_data )
122
+ include_slice = { }
123
+ assert_nil reflection . value ( serializer_instance , include_slice )
124
+ assert_equal :if_sideloaded , reflection . instance_variable_get ( :@_include_data )
125
+ end
126
+
127
+ def test_reflection_value_block_with_include_data_if_sideloaded_excluded_mutates_the_reflection_include_data
128
+ serializer_class = Class . new ( ActiveModel ::Serializer ) do
129
+ has_one :blog do
130
+ include_data :if_sideloaded
131
+ object . blog
132
+ end
133
+ end
134
+ serializer_instance = serializer_class . new ( @model , @instance_options )
135
+
136
+ # Get Reflection
137
+ reflection = serializer_class . _reflections . fetch ( :blog )
138
+
139
+ # Assert
140
+ assert_respond_to reflection . block , :call
141
+ assert_equal true , reflection . instance_variable_get ( :@_include_data )
142
+ include_slice = { blog : :does_not_matter }
143
+ assert_equal @model . blog , reflection . value ( serializer_instance , include_slice )
144
+ assert_equal :if_sideloaded , reflection . instance_variable_get ( :@_include_data )
145
+ end
146
+
28
147
def test_reflection_block_with_link_mutates_the_reflection_links
29
148
serializer_class = Class . new ( ActiveModel ::Serializer ) do
30
149
has_one :blog do
0 commit comments