File: grape_integration.md

package info (click to toggle)
ruby-active-model-serializers 0.10.12-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,752 kB
  • sloc: ruby: 13,138; sh: 53; makefile: 6
file content (42 lines) | stat: -rw-r--r-- 973 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
[Back to Guides](../README.md)

The ActiveModelSerializers grape formatter relies on the existence of `env['grape.request']` which is implemeted by `Grape::Middleware::Globals`. You can meet his dependency by calling it before mounting the endpoints.

In the simpliest way:

```
class API < Grape::API
  # @note Make sure this is above you're first +mount+
  use Grape::Middleware::Globals
end
```

or more like what is shown in current Grape tutorials:

```
module MyApi
  class ApiBase < Grape::API
    use Grape::Middleware::Globals

    require 'grape/active_model_serializers'
    include Grape::ActiveModelSerializers

    mount MyApi::V1::ApiBase
  end
end
```

You could meet this dependency with your own middleware. The invocation might look like:

```
module MyApi
  class ApiBase < Grape::API
    use My::Middleware::Thingamabob

    require 'grape/active_model_serializers'
    include Grape::ActiveModelSerializers

    mount MyApi::V1::ApiBase
  end
end
```