mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-03-25 08:45:55 +00:00
Merge pull request #21 from radanskoric/fix-for-0.9.x
Correctly fetch serialization scope
This commit is contained in:
commit
56121ad6d1
4 changed files with 22 additions and 8 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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'
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue