Skip to content

Commit

Permalink
More improvements in DataMapper support front.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Nov 22, 2009
1 parent f3d5c1a commit fddf95f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rdoc
@@ -1,4 +1,5 @@
* enhancements
* Added DataMapper support
* Remove store_location from authenticatable strategy and add it to failure app
* Allow a strategy to be placed after authenticatable
* [#45] Do not rely attribute? methods, since they are not added on Datamapper
Expand Down
2 changes: 1 addition & 1 deletion generators/devise_install/templates/devise.rb
Expand Up @@ -37,7 +37,7 @@
# Configure the e-mail address which will be shown in DeviseMailer.
# config.mailer_sender = "foo.bar@yourapp.com"
# Configure the ORM. Supports :active_record and :mongo_mapper
# Configure the ORM. Supports :active_record, :data_mapper and :mongo_mapper.
# config.orm = :active_record
# Turn scoped views on. Before rendering "sessions/new", it will first check for
Expand Down
11 changes: 5 additions & 6 deletions lib/devise/models/authenticatable.rb
Expand Up @@ -87,11 +87,10 @@ def find_for_authentication(conditions)
# Attempt to find a user by it's email. If not user is found, returns a
# new user with an email not found error.
def find_or_initialize_with_error_by_email(email)
perishable = find_or_initialize_by_email(email)
if perishable.new_record?
perishable.errors.add(:email, :not_found, :default => 'not found')
end
perishable
attributes = { :email => email }
record = find(:first, :conditions => attributes) || new(attributes)
record.errors.add(:email, :not_found, :default => 'not found') if record.new_record?
record
end

# Hook to serialize user into session. Overwrite if you want.
Expand All @@ -103,7 +102,7 @@ def serialize_into_session(record)
def serialize_from_session(keys)
klass, id = keys
raise "#{self} cannot serialize from #{klass} session since it's not its ancestors" unless klass <= self
klass.find_by_id(id)
klass.find(:first, :conditions => { :id => id })
end
end

Expand Down
62 changes: 62 additions & 0 deletions lib/devise/orm/data_mapper.rb
@@ -0,0 +1,62 @@
module Devise
module Orm
module DataMapper
def self.included_modules_hook(klass, modules)
klass.send :extend, self

# DataMapper validations have a completely different API
if modules.include?(:validatable) && !klass.respond_to?(:validates_presence_of)
raise ":validatable is not supported in DataMapper, please craft your validations by hand"
end

modules.each do |mod|
klass.send(mod)
end
end

include Devise::Schema

SCHEMA_OPTIONS = {
:null => :nullable,
:limit => :length
}

# Hooks for confirmable
def before_create(*args)
before :create, *args
end

def after_create(*args)
after :create, *args
end

# Add ActiveRecord like finder
def find(*args)
options = args.extract_options!
case args.first
when :first
first(options.merge(options.delete(:conditions)))
when :all
all(options.merge(options.delete(:conditions)))
else
get(*args)
end
end

# Tell how to apply schema methods. This automatically maps :limit to
# :length and :null to :nullable.
def apply_schema(name, type, options={})
return unless Devise.apply_schema

SCHEMA_OPTIONS.each do |old_key, new_key|
next unless options[old_key]
options[new_key] = options.delete(old_key)
end

property name, type, options
end
end
end
end

DataMapper::Model.send(:include, Devise::Models)
1 change: 0 additions & 1 deletion lib/devise/orm/mongo_mapper.rb
@@ -1,7 +1,6 @@
module Devise
module Orm
module MongoMapper
# Include attributes modules and set the proper ones.
def self.included_modules_hook(klass, modules)
klass.send :extend, self

Expand Down
2 changes: 1 addition & 1 deletion lib/devise/schema.rb
Expand Up @@ -42,7 +42,7 @@ def rememberable
end

# Overwrite with specific modification to create your own schema.
def apply_schema(name, tupe, options={})
def apply_schema(name, type, options={})
raise NotImplementedError
end
end
Expand Down

0 comments on commit fddf95f

Please sign in to comment.