Skip to content

Commit

Permalink
Merge pull request #21 from radanskoric/fix-for-0.9.x
Browse files Browse the repository at this point in the history
Correctly fetch serialization scope
  • Loading branch information
jrhe committed Aug 21, 2014
2 parents a4ae1d6 + 0db4cae commit 56121ad
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
6 changes: 1 addition & 5 deletions lib/grape-active_model_serializers/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def fetch_serializer(resource, env)
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))
Expand Down Expand Up @@ -70,10 +70,6 @@ def default_root(endpoint)
endpoint.options[:path][0].to_s.split('/')[-1]
end
end

def serialization_scope
:current_user
end
end
end
end
Expand Down
3 changes: 0 additions & 3 deletions spec/features/grape-active_model_serializers/render_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 20 additions & 0 deletions spec/grape-active_model_serializers/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,24 @@
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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require "active_support/json"
require 'rspec'
require 'rack/test'
require 'grape-active_model_serializers'

require 'jazz_hands'

Expand Down

0 comments on commit 56121ad

Please sign in to comment.