forked from rails-api/active_model_serializers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Grape formatter feature requested in rails-api#1268
- Loading branch information
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# To add graoe support, require 'grape-active_model_serializers' in the base of your grape endpoints | ||
# Then add 'formatter :json, Grape::Formatters::ActiveModelSerializers' to the endpoints | ||
# Then add 'helpers Grape::Helpers::ActiveModelSerializers' to the endpoints | ||
require 'active_model_serializers' | ||
require 'grape/formatters/active_model_serializers' | ||
require 'grape/helpers/active_model_serializers' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# A grape response formatter that can be used as 'formatter :json, Grape::Formatters::ActiveModelSerializers' | ||
# | ||
# Serializer options can be passed as a hash from your grape endpoint using env[:active_model_serializer_options], | ||
# or better yet user the render helper in Grape::Helpers::ActiveModelSerializers | ||
module Grape | ||
module Formatters | ||
module ActiveModelSerializers | ||
class << self | ||
def call(resource, env) | ||
serializer_options = {} | ||
serializer_options.merge!(env[:active_model_serializer_options]) if env[:active_model_serializer_options] | ||
ActiveModel::SerializableResource.new(resource, serializer_options).to_json | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Helpers can be included in your grape endpoint as: helpers Grape::Helpers::ActiveModelSerializers | ||
module Grape | ||
module Helpers | ||
module ActiveModelSerializers | ||
# A convenience method for passing ActiveModelSerializer serializer options | ||
# | ||
# Example: To include relationships in the response: render(post, include: ['comments']) | ||
# | ||
# Example: To include pagination meta data: render(posts, meta: { page: posts.page, total_pages: posts.total_pages }) | ||
def render(resource, active_model_serializer_options = {}) | ||
env[:active_model_serializer_options] = active_model_serializer_options | ||
resource | ||
end | ||
end | ||
end | ||
end |