Skip to content

Commit

Permalink
Support mongoid4
Browse files Browse the repository at this point in the history
- Introduce orm_model_dir to reuse models across directories
- Detect mongoid versions to support both mongoid 3 and 4 inside same
  model.
- Update .travis.yml to run build on rails4 + mongoid4
  • Loading branch information
shinzui committed Feb 9, 2014
1 parent 1be70d7 commit e8513af
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 8 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ env:
- rails=4.1.0.beta1
- orm=mongoid2
- orm=mongoid3
- orm=mongoid4
- orm=mongo_mapper
services:
- mongodb
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ when 'mongoid2'
when 'mongoid3'
gem 'mongoid', '3.0.10'

when 'mongoid4'
gem 'mongoid', '4.0.0.beta1'
gem 'moped'

when 'mongo_mapper'
gem 'mongo_mapper', '0.12.0'
gem 'bson_ext', '~> 1.7'
Expand Down
18 changes: 14 additions & 4 deletions lib/doorkeeper/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ def self.configuration
@config || (raise MissingConfiguration.new)
end

def self.orm_model_dir
case configuration.orm
when :mongoid3, :mongoid4
"mongoid3_4"
else
configuration.orm
end

end

def self.enable_orm
require "doorkeeper/models/#{@config.orm}/access_grant"
require "doorkeeper/models/#{@config.orm}/access_token"
require "doorkeeper/models/#{@config.orm}/application"
require "doorkeeper/models/#{orm_model_dir}/access_grant"
require "doorkeeper/models/#{orm_model_dir}/access_token"
require "doorkeeper/models/#{orm_model_dir}/application"
require 'doorkeeper/models/access_grant'
require 'doorkeeper/models/access_token'
require 'doorkeeper/models/application'
Expand Down Expand Up @@ -185,7 +195,7 @@ def scopes
end

def orm_name
[:mongoid2, :mongoid3].include?(orm) ? :mongoid : orm
[:mongoid2, :mongoid3, :mongoid4].include?(orm) ? :mongoid : orm
end

def client_credentials_methods
Expand Down
15 changes: 15 additions & 0 deletions lib/doorkeeper/models/mongoid/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Doorkeeper
module Models
module Mongoid
module Version
def mongoid3?
::Mongoid::VERSION.starts_with?("3")
end

def mongoid4?
::Mongoid::VERSION.starts_with?("4")
end
end
end
end
end
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
require 'doorkeeper/models/mongoid/revocable'
require 'doorkeeper/models/mongoid/scopes'
require 'doorkeeper/models/mongoid/version'

module Doorkeeper
class AccessGrant
include Mongoid::Document
include Mongoid::Timestamps
include Doorkeeper::Models::Mongoid::Revocable
include Doorkeeper::Models::Mongoid::Scopes
extend Doorkeeper::Models::Mongoid::Version

self.store_in collection: :oauth_access_grants

field :resource_owner_id, :type => Moped::BSON::ObjectId
if mongoid3?
field :resource_owner_id, :type => Moped::BSON::ObjectId
else
field :resource_owner_id, :type => BSON::ObjectId
end

field :application_id, :type => Hash
field :token, :type => String
field :expires_in, :type => Integer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
require 'doorkeeper/models/mongoid/revocable'
require 'doorkeeper/models/mongoid/scopes'
require 'doorkeeper/models/mongoid/version'

module Doorkeeper
class AccessToken
include Mongoid::Document
include Mongoid::Timestamps
include Doorkeeper::Models::Mongoid::Revocable
include Doorkeeper::Models::Mongoid::Scopes
extend Doorkeeper::Models::Mongoid::Version

self.store_in collection: :oauth_access_tokens

field :resource_owner_id, :type => Moped::BSON::ObjectId
if mongoid3?
field :resource_owner_id, :type => Moped::BSON::ObjectId
else
field :resource_owner_id, :type => BSON::ObjectId
end

field :token, :type => String
field :expires_in, :type => Integer
field :revoked_at, :type => DateTime
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
when :active_record
class User < ActiveRecord::Base
end
when :mongoid2, :mongoid3
when :mongoid2, :mongoid3, :mongoid4
class User
include Mongoid::Document
include Mongoid::Timestamps
Expand Down
2 changes: 1 addition & 1 deletion spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

Bundler.require :default

orm = if [:mongoid2, :mongoid3].include?(DOORKEEPER_ORM)
orm = if [:mongoid2, :mongoid3, :mongoid4].include?(DOORKEEPER_ORM)
Mongoid.load!(File.join(File.dirname(File.expand_path(__FILE__)), "#{DOORKEEPER_ORM}.yml"))
:mongoid
else
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# just for the purpose of running a single test. If you are using a tool that
# preloads Rails for running tests, you may have to set it to true.
config.eager_load = false
config.i18n.enforce_available_locales = true
end

# Show full error reports and disable caching
Expand Down
18 changes: 18 additions & 0 deletions spec/dummy/config/mongoid4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
development:
sessions:
default:
database: doorkeeper-mongoid4-development
hosts:
- localhost:27017
options:
w: 1

test:
sessions:
default:
database: doorkeeper-mongoid4-test
hosts:
- localhost:27017
options:
write:
w: 1

0 comments on commit e8513af

Please sign in to comment.