Correctly fetch serialization scope

This commit is contained in:
Radan Skoric 2014-07-21 09:37:06 +02:00
parent a4ae1d64e9
commit 0db4cae3b5
4 changed files with 22 additions and 8 deletions

View file

@ -17,7 +17,7 @@ module Grape
options = build_options_from_endpoint(endpoint)
if serializer = options.fetch(:serializer, ActiveModel::Serializer.serializer_for(resource))
options[:scope] = serialization_scope unless options.has_key?(:scope)
options[:scope] = endpoint.serialization_scope unless options.has_key?(:scope)
# ensure we have an root to fallback on
options[:resource_name] = default_root(endpoint) if resource.respond_to?(:to_ary)
serializer.new(resource, options.merge(other_options))
@ -70,10 +70,6 @@ module Grape
endpoint.options[:path][0].to_s.split('/')[-1]
end
end
def serialization_scope
:current_user
end
end
end
end

View file

@ -1,7 +1,4 @@
require 'spec_helper'
require 'support/models/user'
require 'support/serializers/user_serializer'
require 'grape-active_model_serializers'
require 'securerandom'
describe '#render' do

View file

@ -31,4 +31,24 @@ describe Grape::Formatter::ActiveModelSerializers do
expect(subject.meta_key).to eq({ meta_key: :custom_key_name })
end
end
describe '.fetch_serializer' do
let(:user) { User.new(first_name: 'John') }
let(:endpoint) { Grape::Endpoint.new({}, {path: '/', method: 'foo'}) }
let(:env) { { 'api.endpoint' => endpoint } }
before do
def endpoint.current_user
@current_user ||= User.new(first_name: 'Current user')
end
end
subject { described_class.fetch_serializer(user, env) }
it { should be_a UserSerializer }
it 'should have correct scope set' do
expect(subject.scope).to eq(endpoint.current_user)
end
end
end

View file

@ -9,6 +9,7 @@ require 'active_support/core_ext/hash/conversions'
require "active_support/json"
require 'rspec'
require 'rack/test'
require 'grape-active_model_serializers'
require 'jazz_hands'