Skip to content

Commit

Permalink
Adds MongoMapper support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Bleigh committed Oct 30, 2011
1 parent 623589c commit f75dc55
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 20 deletions.
27 changes: 17 additions & 10 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: .
specs:
omniauth-identity (1.0.0.beta1)
omniauth (= 1.0.0.beta1)
omniauth-identity (1.0.0.rc2)
omniauth (~> 1.0.0.rc2)

GEM
remote: http://rubygems.org/
Expand All @@ -20,7 +20,7 @@ GEM
multi_json (~> 1.0)
arel (2.2.1)
bcrypt-ruby (3.0.1)
bson (1.4.1)
bson (1.3.1)
bson_ext (1.4.1)
builder (3.0.0)
diff-lcs (1.1.3)
Expand All @@ -36,16 +36,22 @@ GEM
i18n (0.6.0)
maruku (0.6.0)
syntax (>= 1.0.0)
mongo (1.4.1)
bson (= 1.4.1)
mongoid (2.3.2)
activemodel (~> 3.1)
mongo (~> 1.4)
mongo (1.3.1)
bson (>= 1.3.1)
mongo_mapper (0.9.2)
activemodel (~> 3.0)
activesupport (~> 3.0)
plucky (~> 0.3.8)
mongoid (2.2.3)
activemodel (~> 3.0)
mongo (~> 1.3)
tzinfo (~> 0.3.22)
multi_json (1.0.3)
omniauth (1.0.0.beta1)
omniauth (1.0.0.rc2)
hashie (~> 1.2)
rack
plucky (0.3.8)
mongo (~> 1.3)
rack (1.3.5)
rack-test (0.6.1)
rack (>= 1.0)
Expand Down Expand Up @@ -79,7 +85,8 @@ DEPENDENCIES
guard-bundler
guard-rspec
maruku (~> 0.6)
mongoid (~> 2.3)
mongo_mapper
mongoid (~> 2.2.3)
omniauth-identity!
rack-test (~> 0.5)
rake (~> 0.8)
Expand Down
34 changes: 26 additions & 8 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,43 @@ look something like this:

Next, you need to create a model (called `Identity by default`) that will be
able to persist the information provided by the user. Luckily for you, there
are pre-built models for popular ORMs that make this dead simple. You just
need to subclass the relevant class:
are pre-built models for popular ORMs that make this dead simple.

### ActiveRecord

Just subclass `OmniAuth::Identity::Models::ActiveRecord` and provide fields
in the database for all of the fields you are using.

class Identity < OmniAuth::Identity::Models::ActiveRecord
# Add whatever you like!
end

Adapters are provided for `ActiveRecord` and `MongoMapper` and are
autoloaded on request (but not loaded by default so no dependencies are
injected).
### Mongoid

You can also use `Mongoid`, but you need to include it in your class:
Include the `OmniAuth::Identity::Models::Mongoid` mixin and specify
fields that you will need.

class Identity
include Mongoid::Document
include OmniAuth::Identity::Models::Mongoid

# Add whatever you like!
field :email, type: String
field :name, type: String
field :password_digest, type: String
end

### MongoMapper

Include the `OmniAuth::Identity::Models::MongoMapper` mixin and specify
fields that you will need.

class Identity
include MongoMapper::Document
include OmniAuth::Identity::Models::MongoMapper

key :email, String
key :name, String
key :password_digest, String
end

Once you've got an Identity persistence model and the strategy up and
Expand Down
2 changes: 1 addition & 1 deletion lib/omniauth/identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Identity
autoload :SecurePassword, 'omniauth/identity/secure_password'
module Models
autoload :ActiveRecord, 'omniauth/identity/models/active_record'
# autoload :MongoMapper, 'omniauth/identity/models/mongo_mapper'
autoload :MongoMapper, 'omniauth/identity/models/mongo_mapper'
autoload :Mongoid, 'omniauth/identity/models/mongoid'
end
end
Expand Down
28 changes: 28 additions & 0 deletions lib/omniauth/identity/models/mongo_mapper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
require 'mongo_mapper'

module OmniAuth
module Identity
module Models
module MongoMapper
def self.included(base)
base.class_eval do
include ::OmniAuth::Identity::Model
include ::OmniAuth::Identity::SecurePassword

has_secure_password

def self.auth_key=(key)
super
validates_uniqueness_of key, :case_sensitive => false
end

def self.locate(key)
where(auth_key => key).first
end
end
end
end
end
end
end

3 changes: 2 additions & 1 deletion omniauth-identity.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rspec', '~> 2.5'
gem.add_development_dependency 'bcrypt-ruby', '~> 3.0'
gem.add_development_dependency 'activerecord', '~> 3.1'
gem.add_development_dependency 'mongoid', '~> 2.3'
gem.add_development_dependency 'mongoid', '~> 2.2.3'
gem.add_development_dependency 'mongo_mapper'
gem.add_development_dependency 'bson_ext', '~> 1.4'

gem.name = 'omniauth-identity'
Expand Down
14 changes: 14 additions & 0 deletions spec/omniauth/identity/models/mongo_mapper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'spec_helper'

describe(OmniAuth::Identity::Models::MongoMapper, :db => true) do
class MongoMapperTestIdentity
include MongoMapper::Document
include OmniAuth::Identity::Models::MongoMapper
auth_key :ham_sandwich
end

it 'should locate using the auth key using a where query' do
MongoMapperTestIdentity.should_receive(:where).with('ham_sandwich' => 'open faced').and_return(['wakka'])
MongoMapperTestIdentity.locate('open faced').should == 'wakka'
end
end

0 comments on commit f75dc55

Please sign in to comment.