Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly fetch serialization scope #21

Merged
merged 1 commit into from
Aug 21, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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