@@ -77,7 +77,7 @@ class BogusEntity < Grape::Entity
77
77
78
78
it 'references an instance of the entity without any options' do
79
79
subject . expose ( :size ) { |_ | self }
80
- expect ( subject . represent ( Hash . new ) . send ( :value_for , :size ) ) . to be_an_instance_of fresh_class
80
+ expect ( subject . represent ( { } ) . send ( :value_for , :size ) ) . to be_an_instance_of fresh_class
81
81
end
82
82
end
83
83
@@ -91,10 +91,10 @@ class BogusEntity < Grape::Entity
91
91
end
92
92
93
93
expect ( subject . exposures ) . to eq (
94
- awesome : { } ,
95
- awesome__nested : { nested : true } ,
96
- awesome__nested__moar_nested : { as : 'weee' , nested : true } ,
97
- awesome__another_nested : { using : 'Awesome' , nested : true }
94
+ awesome : { } ,
95
+ awesome__nested : { nested : true } ,
96
+ awesome__nested__moar_nested : { as : 'weee' , nested : true } ,
97
+ awesome__another_nested : { using : 'Awesome' , nested : true }
98
98
)
99
99
end
100
100
@@ -105,8 +105,8 @@ class BogusEntity < Grape::Entity
105
105
end
106
106
107
107
expect ( subject . represent ( { } ) . send ( :value_for , :awesome ) ) . to eq (
108
- nested : 'value' ,
109
- another_nested : 'value'
108
+ nested : 'value' ,
109
+ another_nested : 'value'
110
110
)
111
111
end
112
112
@@ -129,13 +129,13 @@ class BogusEntity < Grape::Entity
129
129
end
130
130
131
131
expect ( subject . represent ( { } ) . serializable_hash ) . to eq (
132
- awesome : {
133
- nested : 'value' ,
134
- another_nested : 'value' ,
135
- second_level_nested : {
136
- deeply_exposed_attr : 'value'
137
- }
138
- }
132
+ awesome : {
133
+ nested : 'value' ,
134
+ another_nested : 'value' ,
135
+ second_level_nested : {
136
+ deeply_exposed_attr : 'value'
137
+ }
138
+ }
139
139
)
140
140
end
141
141
@@ -162,22 +162,22 @@ class Parent < Person
162
162
end
163
163
164
164
expect ( ClassRoom . represent ( { } ) . serializable_hash ) . to eq (
165
- parents : [
166
- {
167
- user : { in_first : 'value' } ,
168
- children : [
169
- { user : { in_first : 'value' , user_id : 'value' , display_id : 'value' } } ,
170
- { user : { in_first : 'value' , user_id : 'value' , display_id : 'value' } }
171
- ]
172
- } ,
173
- {
174
- user : { in_first : 'value' } ,
175
- children : [
176
- { user : { in_first : 'value' , user_id : 'value' , display_id : 'value' } } ,
177
- { user : { in_first : 'value' , user_id : 'value' , display_id : 'value' } }
178
- ]
179
- }
180
- ]
165
+ parents : [
166
+ {
167
+ user : { in_first : 'value' } ,
168
+ children : [
169
+ { user : { in_first : 'value' , user_id : 'value' , display_id : 'value' } } ,
170
+ { user : { in_first : 'value' , user_id : 'value' , display_id : 'value' } }
171
+ ]
172
+ } ,
173
+ {
174
+ user : { in_first : 'value' } ,
175
+ children : [
176
+ { user : { in_first : 'value' , user_id : 'value' , display_id : 'value' } } ,
177
+ { user : { in_first : 'value' , user_id : 'value' , display_id : 'value' } }
178
+ ]
179
+ }
180
+ ]
181
181
)
182
182
end
183
183
@@ -259,7 +259,7 @@ class Parent < Person
259
259
end
260
260
261
261
it 'formats an exposure with a :format_with lambda that returns a value from the entity instance' do
262
- object = Hash . new
262
+ object = { }
263
263
264
264
subject . expose ( :size , format_with : lambda { |_value | self . object . class . to_s } )
265
265
expect ( subject . represent ( object ) . send ( :value_for , :size ) ) . to eq object . class . to_s
@@ -270,7 +270,7 @@ class Parent < Person
270
270
self . object . class . to_s
271
271
end
272
272
273
- object = Hash . new
273
+ object = { }
274
274
275
275
subject . expose ( :size , format_with : :size_formatter )
276
276
expect ( subject . represent ( object ) . send ( :value_for , :size ) ) . to eq object . class . to_s
@@ -383,8 +383,8 @@ class Parent < Person
383
383
end
384
384
385
385
expect ( subject . exposures [ :awesome_thing ] ) . to eq (
386
- if : { awesome : false , less_awesome : true } ,
387
- if_extras : [ :awesome , match_proc ]
386
+ if : { awesome : false , less_awesome : true } ,
387
+ if_extras : [ :awesome , match_proc ]
388
388
)
389
389
end
390
390
@@ -408,8 +408,8 @@ class Parent < Person
408
408
end
409
409
410
410
expect ( subject . exposures [ :awesome_thing ] ) . to eq (
411
- unless : { awesome : false , less_awesome : true } ,
412
- unless_extras : [ :awesome , match_proc ]
411
+ unless : { awesome : false , less_awesome : true } ,
412
+ unless_extras : [ :awesome , match_proc ]
413
413
)
414
414
end
415
415
@@ -461,7 +461,7 @@ class Parent < Person
461
461
end
462
462
463
463
it 'returns a single entity if called with a hash' do
464
- expect ( subject . represent ( Hash . new ) ) . to be_kind_of ( subject )
464
+ expect ( subject . represent ( { } ) ) . to be_kind_of ( subject )
465
465
end
466
466
467
467
it 'returns multiple entities if called with a collection' do
@@ -506,6 +506,47 @@ class Parent < Person
506
506
subject . represent ( Object . new , serializable : true )
507
507
end . to raise_error ( NoMethodError , /missing attribute `awesome'/ )
508
508
end
509
+
510
+ context 'with specified fields' do
511
+ it 'returns only specified fields with only option' do
512
+ subject . expose ( :id , :name , :phone )
513
+ representation = subject . represent ( OpenStruct . new , only : [ :id , :name ] , serializable : true )
514
+ expect ( representation ) . to eq ( id : nil , name : nil )
515
+ end
516
+
517
+ it 'can specify children attributes' do
518
+ user_entity = Class . new ( Grape ::Entity )
519
+ user_entity . expose ( :id , :name , :email )
520
+
521
+ subject . expose ( :id , :name , :phone )
522
+ subject . expose ( :user , using : user_entity )
523
+
524
+ representation = subject . represent ( OpenStruct . new ( user : { } ) , only : [ :id , :name , { user : [ :name , :email ] } ] , serializable : true )
525
+ expect ( representation ) . to eq ( id : nil , name : nil , user : { name : nil , email : nil } )
526
+ end
527
+
528
+ context 'specify attribute with exposure condition' do
529
+ it 'returns only specified fields' do
530
+ subject . expose ( :id , :name )
531
+ subject . with_options ( if : { condition : true } ) do
532
+ subject . expose ( :name )
533
+ end
534
+
535
+ representation = subject . represent ( OpenStruct . new , condition : true , only : [ :id , :name ] , serializable : true )
536
+ expect ( representation ) . to eq ( id : nil , name : nil )
537
+ end
538
+ end
539
+
540
+ context 'attribute with alias' do
541
+ it 'returns only specified fields' do
542
+ subject . expose ( :id )
543
+ subject . expose ( :name , as : :title )
544
+
545
+ representation = subject . represent ( OpenStruct . new , condition : true , only : [ :id , :title ] , serializable : true )
546
+ expect ( representation ) . to eq ( id : nil , title : nil )
547
+ end
548
+ end
549
+ end
509
550
end
510
551
511
552
describe '.present_collection' do
0 commit comments