Skip to content

Commit

Permalink
Merge pull request #74 from ruby-grape/support-sequel
Browse files Browse the repository at this point in the history
Ensure support for Sequel.
  • Loading branch information
drn authored Apr 26, 2017
2 parents 2748d5b + eb28eb2 commit da18531
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### 1.5.1 (Next)

* Your contribution here.
* [#74](https://github.com/ruby-grape/grape-active_model_serializers/pull/74): Add support for Sequel - [@drn](https://github.com/drn).
* [#65](https://github.com/ruby-grape/grape-active_model_serializers/pull/65): Added Danger, PR linter - [@dblock](https://github.com/dblock).
* [#63](https://github.com/ruby-grape/grape-active_model_serializers/pull/63): Pass adapter options through render - [@drn](https://github.com/drn).

Expand Down
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ else
end

group :test do
gem 'sqlite3'
gem 'sequel', '~> 4.37', require: false
gem 'ruby-grape-danger', '~> 0.1.0', require: false
end
11 changes: 7 additions & 4 deletions lib/grape-active_model_serializers/serializer_resolver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ def resource_defined_class
end

def collection_class
return nil unless resource.respond_to?(:to_ary)
ActiveModel::Serializer.config.collection_serializer
if resource.respond_to?(:to_ary) || resource.respond_to?(:all)
ActiveModel::Serializer.config.collection_serializer
end
end

def namespace_inferred_class
Expand Down Expand Up @@ -84,8 +85,10 @@ def resource_namespace
end

def resource_class
if resource.respond_to?(:to_ary)
resource.try(:klass) || resource.compact.first.class
if resource.respond_to?(:klass)
resource.klass
elsif resource.respond_to?(:to_ary) || resource.respond_to?(:all)
resource.first.class
else
resource.class
end
Expand Down
68 changes: 68 additions & 0 deletions spec/integration/sequel_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'sequel'
require 'spec_helper'

describe 'Sequel Integration' do
before do
DB = Sequel.sqlite unless defined?(DB)
DB.create_table(:users) do
primary_key :id
String :name
end
ActiveModelSerializers.config.adapter = :json
app.format :json
app.formatter :json, Grape::Formatter::ActiveModelSerializers
end

after do
DB.drop_table(:users)
Object.send(:remove_const, :SequelUser)
Object.send(:remove_const, :SequelUserSerializer)
end

let!(:model) {
SequelUser = Class.new(Sequel::Model(:users)) do
include ActiveModel::Serialization
def self.model_name
'User'
end
end
}
let!(:serializer) {
SequelUserSerializer = Class.new(ActiveModel::Serializer) do
attributes :id, :name
end
}
let(:app) { Class.new(Grape::API) }

context 'collection' do
let!(:users) {
[
model.create(name: 'one'),
model.create(name: 'two')
]
}

it 'renders' do
app.get('/users') { render SequelUser.dataset }
response = get '/users'
expect(JSON.parse(response.body)).to eq(
'users' => [
{ 'id' => 1, 'name' => 'one' },
{ 'id' => 2, 'name' => 'two' }
]
)
end
end

context 'member' do
let!(:user) { model.create(name: 'name') }

it 'renders' do
app.get('/user/1') { render SequelUser.first }
response = get '/user/1'
expect(JSON.parse(response.body)).to eq(
'user' => { 'id' => 1, 'name' => 'name' }
)
end
end
end

0 comments on commit da18531

Please sign in to comment.