mirror of
https://github.com/samsonjs/grape-active_model_serializers.git
synced 2026-04-27 14:57:43 +00:00
Merge pull request #30 from OutrageousLabs/read-options
read options from default_serializer_options.
This commit is contained in:
commit
3a0a1b4eee
5 changed files with 28 additions and 9 deletions
10
README.md
10
README.md
|
|
@ -109,6 +109,16 @@ get "/homes"
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Support for `default_serializer_options`
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
helper do
|
||||||
|
def default_serializer_options
|
||||||
|
{only: params[:only], except: params[:except]}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
### current_user
|
### current_user
|
||||||
|
|
||||||
One of the nice features of ActiveModel::Serializers is that it
|
One of the nice features of ActiveModel::Serializers is that it
|
||||||
|
|
|
||||||
|
|
@ -22,5 +22,5 @@ Gem::Specification.new do |gem|
|
||||||
gem.add_development_dependency "rspec"
|
gem.add_development_dependency "rspec"
|
||||||
gem.add_development_dependency "rack-test"
|
gem.add_development_dependency "rack-test"
|
||||||
gem.add_development_dependency "rake"
|
gem.add_development_dependency "rake"
|
||||||
gem.add_development_dependency 'guard-rspec'
|
gem.add_development_dependency "guard-rspec"
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -56,7 +56,7 @@ module Grape
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_options_from_endpoint(endpoint)
|
def build_options_from_endpoint(endpoint)
|
||||||
endpoint.namespace_options.merge(endpoint.route_options)
|
[endpoint.default_serializer_options || {}, endpoint.namespace_options, endpoint.route_options].reduce(:merge)
|
||||||
end
|
end
|
||||||
|
|
||||||
# array root is the innermost namespace name ('space') if there is one,
|
# array root is the innermost namespace name ('space') if there is one,
|
||||||
|
|
|
||||||
|
|
@ -7,12 +7,12 @@ describe 'Grape::EndpointExtension' do
|
||||||
let(:serializer) { Grape::Formatter::ActiveModelSerializers }
|
let(:serializer) { Grape::Formatter::ActiveModelSerializers }
|
||||||
|
|
||||||
let(:user) do
|
let(:user) do
|
||||||
Object.new do
|
Object.new do
|
||||||
def name
|
def name
|
||||||
'sven'
|
'sven'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
let(:users) { [user, user] }
|
let(:users) { [user, user] }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,10 @@ describe Grape::Formatter::ActiveModelSerializers do
|
||||||
def endpoint.current_user
|
def endpoint.current_user
|
||||||
@current_user ||= User.new(first_name: 'Current user')
|
@current_user ||= User.new(first_name: 'Current user')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def endpoint.default_serializer_options
|
||||||
|
{ only: :only, except: :except }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
subject { described_class.fetch_serializer(user, env) }
|
subject { described_class.fetch_serializer(user, env) }
|
||||||
|
|
@ -48,7 +52,12 @@ describe Grape::Formatter::ActiveModelSerializers do
|
||||||
it { should be_a UserSerializer }
|
it { should be_a UserSerializer }
|
||||||
|
|
||||||
it 'should have correct scope set' do
|
it 'should have correct scope set' do
|
||||||
expect(subject.scope).to eq(endpoint.current_user)
|
expect(subject.scope.current_user).to eq(endpoint.current_user)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'should read default serializer options' do
|
||||||
|
expect(subject.instance_variable_get('@only')).to eq([:only])
|
||||||
|
expect(subject.instance_variable_get('@except')).to eq([:except])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue