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
 - Remove .idea from gitignore
 - Add integration test
  • Loading branch information
jpaas authored and johnhamelink committed Nov 23, 2015
1 parent efe5128 commit 379c643
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 0 deletions.
1 change: 1 addition & 0 deletions active_model_serializers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ Gem::Specification.new do |spec|

spec.add_development_dependency 'bundler', '~> 1.6'
spec.add_development_dependency 'timecop', '~> 0.7'
spec.add_development_dependency 'grape', '~> 0.13.0'
end
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This is the documentation of AMS, it's focused on the **0.10.x version.**
- [How to add pagination links](howto/add_pagination_links.md)
- [Using AMS Outside Of Controllers](howto/outside_controller_use.md)
- [How to use JSON API with Ember](howto/ember-and-json-api.md)
- [Grape](howto/grape.md)

## Getting Help

Expand Down
27 changes: 27 additions & 0 deletions docs/howto/grape.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Integration with Grape

To add Grape support, `require 'grape/active_model_serializers'` in the base of your Grape endpoints like so:

```ruby
module Example
class Dummy < Grape::API
require 'grape/active_model_serializers'
mount Example::V1::Base
end
end
```

Then add the formatter and helpers to the endpoints (a `Defaults` module is used here which would be included in all endpoints):

```ruby
module Example::V1::Defaults
extend ActiveSupport::Concern

included do
formatter :json, Grape::Formatters::ActiveModelSerializers
helpers Grape::Helpers::ActiveModelSerializers
end

end
```

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'
16 changes: 16 additions & 0 deletions lib/grape/formatters/active_model_serializers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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
def self.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
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
50 changes: 50 additions & 0 deletions test/grape_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
require 'test_helper'
require 'securerandom'
require 'grape'
require 'grape/active_model_serializers'

class ActiveModelSerializers::GrapeTest < Minitest::Test
include Rack::Test::Methods

class GrapeTest < Grape::API
format :json
formatter :json, Grape::Formatters::ActiveModelSerializers
helpers Grape::Helpers::ActiveModelSerializers

resources :grape do
get '/render' do
render ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
end

get '/render_with_json_api' do
post = ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
render(
post,
meta: { page: 1, total_pages: 2 },
adapter: :json_api
)
end
end
end

def app
GrapeTest.new
end

def test_formatter_returns_json
get '/grape/render'
assert last_response.ok?
assert JSON.parse(last_response.body)
end

def test_render_helper_passes_through_options_correctly
get '/grape/render_with_json_api'

post = ARModels::Post.new(title: 'Dummy Title', body: 'Lorem Ipsum')
serializer = ARModels::PostSerializer.new(post)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(serializer, meta: { page: 1, total_pages: 2 })

assert last_response.ok?
assert_equal adapter.to_json, last_response.body
end
end

0 comments on commit 379c643

Please sign in to comment.