Created
October 13, 2012 01:14
-
-
Save spohlenz/3882783 to your computer and use it in GitHub Desktop.
Devise with MongoModel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Replace relevant section in config/initializers/devise.rb | |
# ==> ORM configuration | |
# Load and configure the ORM. Supports :active_record (default) and | |
# :mongoid (bson_ext recommended) by default. Other ORMs may be | |
# available as additional gems. | |
require 'devise/orm/mongomodel' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Save me in lib/devise/orm/mongomodel.rb | |
require 'mongomodel' | |
module MongoModel | |
class Document | |
extend OrmAdapter::ToAdapter | |
class OrmAdapter < ::OrmAdapter::Base | |
# get a list of column names for a given class | |
def column_names | |
klass.properties.values.map(&:name) | |
end | |
# @see OrmAdapter::Base#get! | |
def get!(id) | |
klass.find(wrap_key(id)) | |
end | |
# @see OrmAdapter::Base#get | |
def get(id) | |
klass.find_by_id(wrap_key(id)) | |
end | |
# @see OrmAdapter::Base#find_first | |
def find_first(conditions = {}) | |
conditions, order = extract_conditions!(conditions) | |
scope = klass.where(conditions) | |
scope = scope.order(order) unless order.nil? | |
scope.first | |
end | |
# @see OrmAdapter::Base#find_all | |
def find_all(conditions = {}) | |
conditions, order, limit, offset = extract_conditions!(conditions) | |
scope = klass.where(conditions) | |
scope = scope.order(order) unless order.nil? | |
scope = scope.limit(limit) unless limit.nil? | |
scope = scope.offset(offset) unless limit.nil? || offset.nil? | |
scope.all | |
end | |
# @see OrmAdapter::Base#create! | |
def create!(attributes = {}) | |
klass.create!(attributes) | |
end | |
# @see OrmAdapter::Base#destroy | |
def destroy(object) | |
object.destroy if valid_object?(object) | |
end | |
end | |
end | |
end | |
MongoModel::Document.extend Devise::Models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < MongoModel::Document | |
# Include default devise modules. Others available are: | |
# :token_authenticatable, :confirmable, | |
# :lockable, :timeoutable and :omniauthable | |
devise :database_authenticatable, :registerable, | |
:recoverable, :rememberable, :trackable, :validatable | |
## Database authenticatable | |
property :email, String, :default => "" | |
property :encrypted_password, String, :default => "" | |
validates_presence_of :email | |
validates_presence_of :encrypted_password | |
## Recoverable | |
property :reset_password_token, String | |
property :reset_password_sent_at, Time | |
## Rememberable | |
property :remember_created_at, Time | |
## Trackable | |
property :sign_in_count, Integer, :default => 0 | |
property :current_sign_in_at, Time | |
property :last_sign_in_at, Time | |
property :current_sign_in_ip, String | |
property :last_sign_in_ip, String | |
## Confirmable | |
# property :confirmation_token, String | |
# property :confirmed_at, Time | |
# property :confirmation_sent_at, Time | |
# property :unconfirmed_email, String # Only if using reconfirmable | |
## Lockable | |
# property :failed_attempts, Integer, :default => 0 # Only if lock strategy is :failed_attempts | |
# property :unlock_token, String # Only if unlock strategy is :email or :both | |
# property :locked_at, Time | |
## Token authenticatable | |
# property :authentication_token, String | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment