From e8513af3d0c9bcaba0da7a487ebca3d9343c2401 Mon Sep 17 00:00:00 2001 From: Nadeem Bitar Date: Sun, 9 Feb 2014 12:08:28 -0800 Subject: [PATCH] Support mongoid4 - 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 --- .travis.yml | 1 + Gemfile | 4 ++++ lib/doorkeeper/config.rb | 18 ++++++++++++++---- lib/doorkeeper/models/mongoid/version.rb | 15 +++++++++++++++ .../{mongoid3 => mongoid3_4}/access_grant.rb | 9 ++++++++- .../{mongoid3 => mongoid3_4}/access_token.rb | 9 ++++++++- .../{mongoid3 => mongoid3_4}/application.rb | 0 spec/dummy/app/models/user.rb | 2 +- spec/dummy/config/application.rb | 2 +- spec/dummy/config/environments/test.rb | 1 + spec/dummy/config/mongoid4.yml | 18 ++++++++++++++++++ 11 files changed, 71 insertions(+), 8 deletions(-) create mode 100644 lib/doorkeeper/models/mongoid/version.rb rename lib/doorkeeper/models/{mongoid3 => mongoid3_4}/access_grant.rb (70%) rename lib/doorkeeper/models/{mongoid3 => mongoid3_4}/access_token.rb (82%) rename lib/doorkeeper/models/{mongoid3 => mongoid3_4}/application.rb (100%) create mode 100644 spec/dummy/config/mongoid4.yml diff --git a/.travis.yml b/.travis.yml index 5d8699831..22770ac98 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,6 +13,7 @@ env: - rails=4.1.0.beta1 - orm=mongoid2 - orm=mongoid3 + - orm=mongoid4 - orm=mongo_mapper services: - mongodb diff --git a/Gemfile b/Gemfile index dfd393fdf..42d678a59 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/lib/doorkeeper/config.rb b/lib/doorkeeper/config.rb index db17118cd..2c9f3f436 100644 --- a/lib/doorkeeper/config.rb +++ b/lib/doorkeeper/config.rb @@ -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' @@ -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 diff --git a/lib/doorkeeper/models/mongoid/version.rb b/lib/doorkeeper/models/mongoid/version.rb new file mode 100644 index 000000000..b8f202625 --- /dev/null +++ b/lib/doorkeeper/models/mongoid/version.rb @@ -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 diff --git a/lib/doorkeeper/models/mongoid3/access_grant.rb b/lib/doorkeeper/models/mongoid3_4/access_grant.rb similarity index 70% rename from lib/doorkeeper/models/mongoid3/access_grant.rb rename to lib/doorkeeper/models/mongoid3_4/access_grant.rb index e3cf0bbba..9c4ab4eb1 100644 --- a/lib/doorkeeper/models/mongoid3/access_grant.rb +++ b/lib/doorkeeper/models/mongoid3_4/access_grant.rb @@ -1,5 +1,6 @@ require 'doorkeeper/models/mongoid/revocable' require 'doorkeeper/models/mongoid/scopes' +require 'doorkeeper/models/mongoid/version' module Doorkeeper class AccessGrant @@ -7,10 +8,16 @@ class AccessGrant 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 diff --git a/lib/doorkeeper/models/mongoid3/access_token.rb b/lib/doorkeeper/models/mongoid3_4/access_token.rb similarity index 82% rename from lib/doorkeeper/models/mongoid3/access_token.rb rename to lib/doorkeeper/models/mongoid3_4/access_token.rb index dbb383d0c..0707ac080 100644 --- a/lib/doorkeeper/models/mongoid3/access_token.rb +++ b/lib/doorkeeper/models/mongoid3_4/access_token.rb @@ -1,5 +1,6 @@ require 'doorkeeper/models/mongoid/revocable' require 'doorkeeper/models/mongoid/scopes' +require 'doorkeeper/models/mongoid/version' module Doorkeeper class AccessToken @@ -7,10 +8,16 @@ class AccessToken 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 diff --git a/lib/doorkeeper/models/mongoid3/application.rb b/lib/doorkeeper/models/mongoid3_4/application.rb similarity index 100% rename from lib/doorkeeper/models/mongoid3/application.rb rename to lib/doorkeeper/models/mongoid3_4/application.rb diff --git a/spec/dummy/app/models/user.rb b/spec/dummy/app/models/user.rb index 1e887b333..c5f6e9216 100644 --- a/spec/dummy/app/models/user.rb +++ b/spec/dummy/app/models/user.rb @@ -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 diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index d921f7750..526b0f70d 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -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 diff --git a/spec/dummy/config/environments/test.rb b/spec/dummy/config/environments/test.rb index 41383d618..601818e85 100644 --- a/spec/dummy/config/environments/test.rb +++ b/spec/dummy/config/environments/test.rb @@ -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 diff --git a/spec/dummy/config/mongoid4.yml b/spec/dummy/config/mongoid4.yml new file mode 100644 index 000000000..b10c5d3da --- /dev/null +++ b/spec/dummy/config/mongoid4.yml @@ -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