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
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
Next
2
2
====
3
3
4
+
*[#85](https://github.com/intridea/grape-entity/pull/85): Added `present_collection` to indicate that an `Entity` presents an entire Collection - [@dspaeth-faber](https://github.com/dspaeth-faber).
5
+
*[#85](https://guthub.com/intridea/grape-entity/pull/85): Hashes can now be passed as object to be presented and the `Hash` keys can be referenced by expose - [@dspaeth-faber](https://github.com/dspaeth-faber).
Copy file name to clipboardExpand all lines: README.md
+36-2Lines changed: 36 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -63,6 +63,13 @@ Define a list of fields that will always be exposed.
63
63
expose :user_name, :ip
64
64
```
65
65
66
+
The field lookup takes several steps
67
+
68
+
* first try `entity-instance.exposure`
69
+
* next try `object.exposure`
70
+
* next try `object.fetch(exposure)`
71
+
* last raise an Exception
72
+
66
73
#### Exposing with a Presenter
67
74
68
75
Don't derive your model classes from `Grape::Entity`, expose them using a presenter.
@@ -119,6 +126,20 @@ root 'users', 'user'
119
126
expose :id, :name, ...
120
127
```
121
128
129
+
By default every object of a collection is wrapped into an instance of your `Entity` class.
130
+
You can override this behavior and wrapp the hole collection into one instance of your `Entity`
131
+
class.
132
+
133
+
As example:
134
+
135
+
```ruby
136
+
137
+
present_collection true, :collection_name# `collection_name` is optional and defaults to `items`
138
+
expose :collection_name, using:API:Items
139
+
140
+
141
+
```
142
+
122
143
#### Runtime Exposure
123
144
124
145
Use a block or a `Proc` to evaluate exposure at runtime. The supplied block or
@@ -151,6 +172,20 @@ private
151
172
end
152
173
```
153
174
175
+
You have always access to the presented instance with `object`
176
+
177
+
```ruby
178
+
classExampleEntity < Grape::Entity
179
+
expose :formatted_value
180
+
# ...
181
+
private
182
+
183
+
defformatted_value
184
+
"+ X #{object.value}"
185
+
end
186
+
end
187
+
```
188
+
154
189
#### Aliases
155
190
156
191
Expose under a different name with `:as`.
@@ -216,8 +251,7 @@ The above will automatically create a `Status::Entity` class and define properti
216
251
217
252
### Using Entities
218
253
219
-
With Grape, once an entity is defined, it can be used within endpoints, by calling `present`. The `present` method accepts two arguments, the object to be presented and the options associated with it. The options hash must always include `:with`, which defines the entity to expose.
220
-
254
+
With Grape, once an entity is defined, it can be used within endpoints, by calling `present`. The `present` method accepts two arguments, the `object` to be presented and the `options` associated with it. The options hash must always include `:with`, which defines the entity to expose.
221
255
If the entity includes documentation it can be included in an endpoint's description.
0 commit comments