You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,5 +1,6 @@
1
1
0.5.0 (Next)
2
2
============
3
+
*[#139](https://github.com/intridea/grape-entity/pull/139): Keep a track of attribute nesting path during condition check or runtime exposure - [@calfzhou](https://github.com/calfzhou).
3
4
* Your contribution here.
4
5
5
6
0.4.6 (2015-07-27)
@@ -83,4 +84,3 @@
83
84
==================
84
85
85
86
* Initial public release - [@agileanimal](https://github.com/agileanimal).
Copy file name to clipboardExpand all lines: README.md
+26Lines changed: 26 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -334,6 +334,32 @@ end
334
334
```
335
335
**Notice**: In the above code, you should pay attention to [**Safe Exposure**](#safe-exposure) yourself, for example, `instance.address` might be `nil`, in this situation, it is better to expose it as nil directly.
336
336
337
+
#### Attribute Path Tracking
338
+
339
+
Sometimes, especially when there are nested attributes, you might want to know which attribute it
340
+
is being exposed. For example, some APIs allow user provide a parameter to control which fields
341
+
will be included in (or excluded from) the response.
342
+
343
+
Grape entity can track the path of each attribute, then you could access it during condition check
344
+
or runtime exposure, via `options[:attr_path]`.
345
+
346
+
Attribute path is an array. The last item of this array is the name (alias) of current attribute.
347
+
And if the attribute is nested, the former items are names (aliases) of its ancestor attributes.
348
+
349
+
Here is an example shows what the attribute path will be.
350
+
351
+
```ruby
352
+
classStatus < Grape::Entity
353
+
expose :user# path is [:user]
354
+
expose :foo, as::bar# path is [:bar]
355
+
expose :ado
356
+
expose :b, as::xxdo
357
+
expose :c# path is [:a, :xx, :c]
358
+
end
359
+
end
360
+
end
361
+
```
362
+
337
363
### Using the Exposure DSL
338
364
339
365
Grape ships with a DSL to easily define entities within the context of an existing class:
0 commit comments