Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sohara/753258 to your computer and use it in GitHub Desktop.
Save sohara/753258 to your computer and use it in GitHub Desktop.
# Monkey patching required for compatibility between MongoMapper (v. 0.8.6) and
# the master branch of Devise heading towards 1.2. It seems some methods have been moved
# around to different modules/classes within Devise since the 1.1 branch
module Devise
module Orm
module MongoMapper
module Hook
def devise_modules_hook!
extend Schema
yield
return unless Devise.apply_schema
devise_modules.each { |m| send(m) if respond_to?(m, true) }
end
end
module Schema
include Devise::Schema
# Tell how to apply schema methods
def apply_devise_schema(name, type, options={})
type = Time if type == DateTime
key name, type, options
end
end
end
end
end
Devise::Models::Authenticatable::ClassMethods.class_eval do
def find_for_authentication(conditions)
# find(:first, :conditions => conditions)
first(conditions)
end
# Find an initialize a record setting an error if it can't be found.
def find_or_initialize_with_error_by(attribute, value, error=:invalid) #:nodoc:
find_or_initialize_with_errors([attribute], { attribute => value }, error)
end
def find_or_initialize_with_errors(required_attributes, attributes, error=:invalid)
attribute = attributes.keys.first
value = attributes.values.first
if value.present?
conditions = { attribute => value }
record = first(conditions) # find(:first, :conditions => conditions)
end
unless record
record = new
if value.present?
record.send(:"#{attribute}=", value)
else
error = :blank
end
record.errors.add(attribute, error)
end
record
end
def generate_token(column)
loop do
token = Devise.friendly_token
# break token unless find(:first, :conditions => { column => token })
break token unless first({ column => token })
end
end
end
Devise::Models::Rememberable::ClassMethods.class_eval do
# Recreate the user based on the stored cookie
def serialize_from_cookie(id, remember_token)
conditions = { :id => id, :remember_token => remember_token }
record = first(conditions) # find(:first, :conditions => conditions)
record if record && !record.remember_expired?
end
end
# Changed method to call Klass.first method which returns a record an instance of
# the class instead of the Klass.find, which in the case of MongoMapper always returns an
# array, even if there is only one match. Also requires sending id as an explicit
# key/value hash
class Warden::SessionSerializer
def deserialize(keys)
klass, id = keys
klass.constantize.first(:id => id)
end
end
Devise::RegistrationsController.class_eval do
protected
def authenticate_scope!
send(:"authenticate_#{resource_name}!")
self.resource = resource_class.first(:id => (send(:"current_#{resource_name}").to_key))
end
end
MongoMapper::Document.append_extensions(Devise::Models)
MongoMapper::Document.append_extensions(Devise::Orm::MongoMapper::Hook)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment