Skip to content

Commit

Permalink
Merge pull request #57 from drn/line-length-linter
Browse files Browse the repository at this point in the history
Solve line length linter issues.
  • Loading branch information
dblock authored Jul 15, 2016
2 parents a48d57c + 1dd73e9 commit 37786d6
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 31 deletions.
5 changes: 4 additions & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
inherit_from: .rubocop_todo.yml

AllCops:
Exclude:
- Guardfile
- grape-active_model_serializers.gemspec
3 changes: 0 additions & 3 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
Metrics/AbcSize:
Max: 20

Metrics/LineLength:
Max: 179

# Offense count: 7
Style/Documentation:
Enabled: false
Expand Down
4 changes: 3 additions & 1 deletion lib/grape-active_model_serializers/endpoint_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ def self.included(base)

base.class_eval do
def serialization_scope
send(_serialization_scope) if _serialization_scope && respond_to?(_serialization_scope, true)
return unless _serialization_scope
return unless respond_to?(_serialization_scope, true)
send(_serialization_scope)
end
end
end
Expand Down
26 changes: 22 additions & 4 deletions spec/grape-active_model_serializers/endpoint_extension_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,17 @@

describe 'Grape::EndpointExtension' do
if Grape::Util.const_defined?('InheritableSetting')
subject { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, path: '/', method: 'foo') }
subject do
Grape::Endpoint.new(
Grape::Util::InheritableSetting.new,
path: '/',
method: 'foo'
)
end
else
subject { Grape::Endpoint.new({}, path: '/', method: 'foo') }
subject do
Grape::Endpoint.new({}, path: '/', method: 'foo')
end
end

let(:serializer) { Grape::Formatter::ActiveModelSerializers }
Expand All @@ -23,19 +31,29 @@ def name
before do
allow(subject).to receive(:env).and_return({})
end

it { should respond_to(:render) }
let(:meta_content) { { total: 2 } }
let(:meta_full) { { meta: meta_content } }

context 'supplying meta' do
before do
allow(subject).to receive(:env) { { meta: meta_full } }
end

it 'passes through the Resource and uses given meta settings' do
allow(subject).to receive(:env).and_return(meta: meta_full)
expect(subject.render(users, meta_full)).to eq(users)
end
end

context 'supplying meta and key' do
let(:meta_key) { { meta_key: :custom_key_name } }

before do
allow(subject).to receive(:env) { { meta: meta_full.merge(meta_key) } }
end

it 'passes through the Resource and uses given meta settings' do
allow(subject).to receive(:env).and_return(meta: meta_full.merge(meta_key))
expect(subject.render(users, meta_full.merge(meta_key))).to eq(users)
end
end
Expand Down
14 changes: 10 additions & 4 deletions spec/grape-active_model_serializers/formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,22 @@
end

it 'should read serializer options like "root"' do
expect(described_class.build_options_from_endpoint(app.endpoints.first)).to include :root
expect(
described_class.build_options_from_endpoint(app.endpoints.first)
).to include(:root)
end
end

describe '.fetch_serializer' do
let(:user) { User.new(first_name: 'John') }

let(:params) { { path: '/', method: 'foo', root: false } }
if Grape::Util.const_defined?('InheritableSetting')
let(:endpoint) { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, path: '/', method: 'foo', root: false) }
let(:setting) { Grape::Util::InheritableSetting.new }
else
let(:endpoint) { Grape::Endpoint.new({}, path: '/', method: 'foo', root: false) }
let(:setting) { {} }
end
let(:endpoint) { Grape::Endpoint.new(setting, params) }

let(:env) { { 'api.endpoint' => endpoint } }

Expand Down Expand Up @@ -61,7 +65,9 @@ def endpoint.default_serializer_options
end

it 'should read serializer options like "root"' do
expect(described_class.build_options_from_endpoint(endpoint).keys).to include :root
expect(
described_class.build_options_from_endpoint(endpoint).keys
).to include(:root)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,34 @@

app.namespace('space') do |ns|
ns.get('/', root: false, apiver: 'v1') do
{ user: { first_name: 'JR', last_name: 'HE', email: '[email protected]' } }
{
user: {
first_name: 'JR',
last_name: 'HE',
email: '[email protected]'
}
}
end
end
end

it 'should read serializer options like "root"' do
expect(described_class.build_options_from_endpoint(app.endpoints.first)).to include :root
expect(
described_class.build_options_from_endpoint(app.endpoints.first)
).to include(:root)
end
end

describe '.fetch_serializer' do
let(:user) { User.new(first_name: 'John', email: '[email protected]') }

let(:params) { { path: '/', method: 'foo', version: 'v1', root: false } }
if Grape::Util.const_defined?('InheritableSetting')
let(:endpoint) { Grape::Endpoint.new(Grape::Util::InheritableSetting.new, path: '/', method: 'foo', version: 'v1', root: false) }
let(:setting) { Grape::Util::InheritableSetting.new }
else
let(:endpoint) { Grape::Endpoint.new({}, path: '/', method: 'foo', version: 'v1', root: false) }
let(:setting) { {} }
end

let(:endpoint) { Grape::Endpoint.new(setting, params) }
let(:env) { { 'api.endpoint' => endpoint } }

before do
Expand All @@ -49,7 +58,7 @@ def endpoint.default_serializer_options
let(:options) { described_class.build_options(user, env) }
subject { described_class.fetch_serializer(user, options) }

let(:instance_options) { subject.instance_variable_get(:@instance_options) }
let(:instance_options) { subject.send(:instance_options) }

it { should be_a V1::UserSerializer }

Expand All @@ -63,7 +72,9 @@ def endpoint.default_serializer_options
end

it 'should read serializer options like "root"' do
expect(described_class.build_options_from_endpoint(endpoint).keys).to include :root
expect(
described_class.build_options_from_endpoint(endpoint).keys
).to include(:root)
end
end
end
Expand Down
63 changes: 52 additions & 11 deletions spec/old_grape_ams_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,31 +31,48 @@
end
it 'uses the built in grape serializer' do
get('/home')
expect(subject).to eql "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}"
expect(subject).to eq(
'{"user":{"first_name":"JR","last_name":"HE"}}'
)
end
end

context "serializer isn't set" do
before do
app.get('/home') do
User.new(first_name: 'JR', last_name: 'HE', email: '[email protected]')
User.new(
first_name: 'JR',
last_name: 'HE',
email: '[email protected]'
)
end
end

it 'infers the serializer' do
get '/home'
expect(subject).to eql "{\"user\":{\"first_name\":\"JR\",\"last_name\":\"HE\"}}"
expect(subject).to eq(
'{"user":{"first_name":"JR","last_name":"HE"}}'
)
end
end

it 'serializes arrays of objects' do
app.get('/users') do
user = User.new(first_name: 'JR', last_name: 'HE', email: '[email protected]')
user = User.new(
first_name: 'JR',
last_name: 'HE',
email: '[email protected]'
)
[user, user]
end

get '/users'
expect(subject).to eql "{\"users\":[{\"first_name\":\"JR\",\"last_name\":\"HE\"},{\"first_name\":\"JR\",\"last_name\":\"HE\"}]}"
expect(subject).to eq(
'{"users":['\
'{"first_name":"JR","last_name":"HE"},'\
'{"first_name":"JR","last_name":"HE"}'\
']}'
)
end

context 'models with compound names' do
Expand All @@ -65,17 +82,29 @@
end

get '/home'
expect(subject).to eql "{\"blog_post\":{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"}}"
expect(subject).to eq(
'{"blog_post":'\
'{"title":"Grape AM::S Rocks!","body":"Really, it does."}'\
'}'
)
end

it "generates the proper 'root' node for serialized arrays" do
app.get('/blog_posts') do
blog_post = BlogPost.new(title: 'Grape AM::S Rocks!', body: 'Really, it does.')
blog_post = BlogPost.new(
title: 'Grape AM::S Rocks!',
body: 'Really, it does.'
)
[blog_post, blog_post]
end

get '/blog_posts'
expect(subject).to eql "{\"blog_posts\":[{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"},{\"title\":\"Grape AM::S Rocks!\",\"body\":\"Really, it does.\"}]}"
expect(subject).to eq(
'{"blog_posts":['\
'{"title":"Grape AM::S Rocks!","body":"Really, it does."},'\
'{"title":"Grape AM::S Rocks!","body":"Really, it does."}'\
']}'
)
end
end

Expand All @@ -87,7 +116,9 @@
end

get '/admin/jeff'
expect(subject).to eql "{\"user\":{\"first_name\":\"Jeff\",\"last_name\":null}}"
expect(subject).to eq(
'{"user":{"first_name":"Jeff","last_name":null}}'
)
end

context 'route is in a namespace' do
Expand All @@ -100,7 +131,12 @@
end

get '/admin/jeff'
expect(subject).to eql "{\"admin\":[{\"first_name\":\"Jeff\",\"last_name\":null},{\"first_name\":\"Jeff\",\"last_name\":null}]}"
expect(subject).to eq(
'{"admin":['\
'{"first_name":"Jeff","last_name":null},'\
'{"first_name":"Jeff","last_name":null}'\
']}'
)
end
end

Expand All @@ -112,7 +148,12 @@
end

get '/people'
expect(subject).to eql "{\"people\":[{\"first_name\":\"Jeff\",\"last_name\":null},{\"first_name\":\"Jeff\",\"last_name\":null}]}"
expect(subject).to eq(
'{"people":['\
'{"first_name":"Jeff","last_name":null},'\
'{"first_name":"Jeff","last_name":null}'\
']}'
)
end
end
end

0 comments on commit 37786d6

Please sign in to comment.