Skip to content

Commit

Permalink
Merge pull request #30 from OutrageousLabs/read-options
Browse files Browse the repository at this point in the history
read options from default_serializer_options.
  • Loading branch information
jrhe committed Nov 20, 2014
2 parents c2aed6f + 28de380 commit 3a0a1b4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 9 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ get "/homes"
end
```

### Support for `default_serializer_options`

```ruby
helper do
def default_serializer_options
{only: params[:only], except: params[:except]}
end
end
```

### current_user

One of the nice features of ActiveModel::Serializers is that it
Expand Down
2 changes: 1 addition & 1 deletion grape-active_model_serializers.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ Gem::Specification.new do |gem|
gem.add_development_dependency "rspec"
gem.add_development_dependency "rack-test"
gem.add_development_dependency "rake"
gem.add_development_dependency 'guard-rspec'
gem.add_development_dependency "guard-rspec"
end
2 changes: 1 addition & 1 deletion lib/grape-active_model_serializers/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def meta_key=(key)
end

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

# array root is the innermost namespace name ('space') if there is one,
Expand Down
12 changes: 6 additions & 6 deletions spec/grape-active_model_serializers/endpoint_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
let(:serializer) { Grape::Formatter::ActiveModelSerializers }

let(:user) do
Object.new do
def name
'sven'
end
end
end
Object.new do
def name
'sven'
end
end
end

let(:users) { [user, user] }

Expand Down
11 changes: 10 additions & 1 deletion spec/grape-active_model_serializers/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,23 @@
def endpoint.current_user
@current_user ||= User.new(first_name: 'Current user')
end

def endpoint.default_serializer_options
{ only: :only, except: :except }
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)
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

0 comments on commit 3a0a1b4

Please sign in to comment.