Skip to content

Commit

Permalink
Grape formatter feature requested in rails-api#1268
Browse files Browse the repository at this point in the history
 - adds handling for when the returned resource is not serializable via ams
 - fix for when resource is an Array
 - fixing last checkin
 - Adds intellij .idea file to .gitignore
 - Moves grape include to grape namespace. Changes Enumerable to Array because a plain hash is enumerable.
 - rubocop corrections
 - Rebased and squashed commits, resubmitted to CI
  • Loading branch information
jpaas authored and johnhamelink committed Nov 19, 2015
1 parent efe5128 commit 2ac7389
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ tmp
*.swp
.ruby-version
.ruby-gemset
.idea
6 changes: 6 additions & 0 deletions lib/grape/active_model_serializers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# To add grape 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'
18 changes: 18 additions & 0 deletions lib/grape/formatters/active_model_serializers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# 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)
return resource.to_json unless resource.is_a?(Array) || resource.respond_to?(:model_name)
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
16 changes: 16 additions & 0 deletions lib/grape/helpers/active_model_serializers.rb
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

0 comments on commit 2ac7389

Please sign in to comment.