Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Do not require ActiveRecord to be loaded to use Devise.

  • Loading branch information...
commit e7f809de3c78ab413e43010e4df3bb65556440b3 1 parent 5f49852
@josevalim josevalim authored
View
8 CHANGELOG.rdoc
@@ -1,3 +1,11 @@
+* enhancement
+ * [#16] Allow devise to be more agnostic. Do not require ActiveRecord to be loaded.
+
+* deprecations
+ * Model configuration is on Devise::Models.config and not on Devise.config anymore.
+
+== 0.3.0
+
* bug fix
* [#15] Allow yml messages to be configured by not using engine locales
View
4 README.rdoc
@@ -111,9 +111,9 @@ In addition to :except, you can provide some options to devise call:
devise :all, :remember_for => 2.weeks
-All those values can also be set in a global way by setting them in Devise:
+All those values can also be set in a global way by setting them in Devise::Models:
- Devise.confirm_within = 1.week
+ Devise::Models.confirm_within = 1.week
== Routes
View
52 lib/devise.rb
@@ -9,54 +9,11 @@ module Devise
}.freeze
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze
-
- # Creates configuration values for Devise and for the given module.
- #
- # Devise.model_config(Devise::Authenticable, :stretches, 10)
- #
- # The line above creates:
- #
- # 1) An accessor called Devise.stretches, which value is used by default;
- #
- # 2) Some class methods for your model Model.stretches and Model.stretches=
- # which have higher priority than Devise.stretches;
- #
- # 3) And an instance method stretches.
- #
- # To add the class methods you need to have a module ClassMethods defined
- # inside the given class.
- #
- def self.model_config(mod, accessor, default=nil) #:nodoc:
- mattr_accessor accessor
- send(:"#{accessor}=", default)
-
- mod.class_eval <<-METHOD, __FILE__, __LINE__
- def #{accessor}
- self.class.#{accessor}
- end
- METHOD
-
- mod.const_get(:ClassMethods).class_eval <<-METHOD, __FILE__, __LINE__
- def #{accessor}
- if defined?(@#{accessor})
- @#{accessor}
- elsif superclass.respond_to?(:#{accessor})
- superclass.#{accessor}
- else
- Devise.#{accessor}
- end
- end
-
- def #{accessor}=(value)
- @#{accessor} = value
- end
- METHOD
- end
end
# Devise initialization process goes like this:
#
-# 1) Include Devise::ActiveRecord and Devise::Migrations
+# 1) Includes in Devise::ActiveRecord and Devise::Migrations
# 2) Load and config warden
# 3) Load devise mapping structure
# 4) Add routes extensions
@@ -64,8 +21,11 @@ def #{accessor}=(value)
# 6) Include filters and helpers in controllers and views
#
Rails.configuration.after_initialize do
- ActiveRecord::Base.extend Devise::ActiveRecord
- ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Devise::Migrations
+ if defined?(ActiveRecord)
+ ActiveRecord::Base.extend Devise::Models
+ ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Devise::Migrations
+ end
+
I18n.load_path.unshift File.expand_path(File.join(File.dirname(__FILE__), 'devise', 'locales', 'en.yml'))
end
View
54 lib/devise/active_record.rb → lib/devise/models.rb
@@ -1,5 +1,57 @@
module Devise
- module ActiveRecord
+ module Models
+ # Creates configuration values for Devise and for the given module.
+ #
+ # Devise::Models.config(Devise::Authenticable, :stretches, 10)
+ #
+ # The line above creates:
+ #
+ # 1) An accessor called Devise::Models.stretches, which value is used by default;
+ #
+ # 2) Some class methods for your model Model.stretches and Model.stretches=
+ # which have higher priority than Devise.stretches;
+ #
+ # 3) And an instance method stretches.
+ #
+ # To add the class methods you need to have a module ClassMethods defined
+ # inside the given class.
+ #
+ def self.config(mod, accessor, default=nil) #:nodoc:
+ mattr_accessor accessor
+ send(:"#{accessor}=", default)
+
+ # TODO Remove me in a next release
+ Devise.class_eval <<-METHOD, __FILE__, __LINE__
+ def self.#{accessor}=(value)
+ ActiveSupport::Deprecation.warn "Devise.#{accessor}= is deprecated, " <<
+ "use Devise::Models.#{accessor}= instead."
+ Devise::Models.#{accessor} = value
+ end
+ METHOD
+
+ mod.class_eval <<-METHOD, __FILE__, __LINE__
+ def #{accessor}
+ self.class.#{accessor}
+ end
+ METHOD
+
+ mod.const_get(:ClassMethods).class_eval <<-METHOD, __FILE__, __LINE__
+ def #{accessor}
+ if defined?(@#{accessor})
+ @#{accessor}
+ elsif superclass.respond_to?(:#{accessor})
+ superclass.#{accessor}
+ else
+ Devise::Models.#{accessor}
+ end
+ end
+
+ def #{accessor}=(value)
+ @#{accessor} = value
+ end
+ METHOD
+ end
+
# Shortcut method for including all devise modules inside your model.
# You can give some extra options while declaring devise in your model:
#
View
4 lib/devise/models/authenticatable.rb
@@ -90,8 +90,8 @@ def find_or_initialize_with_error_by_email(email)
end
end
- Devise.model_config(self, :pepper)
- Devise.model_config(self, :stretches, 10)
+ Devise::Models.config(self, :pepper)
+ Devise::Models.config(self, :stretches, 10)
end
end
end
View
2  lib/devise/models/confirmable.rb
@@ -150,7 +150,7 @@ def confirm!(attributes={})
end
end
- Devise.model_config(self, :confirm_within, 0.days)
+ Devise::Models.config(self, :confirm_within, 0.days)
end
end
end
View
2  lib/devise/models/rememberable.rb
@@ -89,7 +89,7 @@ def serialize_from_cookie(cookie)
end
end
- Devise.model_config(self, :remember_for, 2.weeks)
+ Devise::Models.config(self, :remember_for, 2.weeks)
end
end
end
View
4 test/integration/confirmable_test.rb
@@ -59,7 +59,7 @@ def visit_user_confirmation_with_token(confirmation_token)
end
test 'not confirmed user and setup to block without confirmation should not be able to sign in' do
- Devise.confirm_within = 0
+ Devise::Models.confirm_within = 0
user = sign_in_as_user(:confirm => false)
assert_redirected_to new_user_session_path(:unconfirmed => true)
@@ -67,7 +67,7 @@ def visit_user_confirmation_with_token(confirmation_token)
end
test 'not confirmed user but configured with some days to confirm should be able to sign in' do
- Devise.confirm_within = 1
+ Devise::Models.confirm_within = 1
user = sign_in_as_user(:confirm => false)
assert_response :success
View
4 test/integration/rememberable_test.rb
@@ -3,7 +3,7 @@
class RememberMeTest < ActionController::IntegrationTest
def create_user_and_remember(add_to_token='')
- Devise.remember_for = 1
+ Devise::Models.remember_for = 1
user = create_user
user.remember_me!
cookies['remember_token'] = User.serialize_into_cookie(user) + add_to_token
@@ -39,7 +39,7 @@ def create_user_and_remember(add_to_token='')
test 'do not remember with token expired' do
user = create_user_and_remember
- Devise.remember_for = 0
+ Devise::Models.remember_for = 0
get users_path
assert_response :success
assert_not warden.authenticated?(:user)
View
14 test/models/authenticatable_test.rb
@@ -66,28 +66,28 @@ def encrypt_password(user, pepper=nil, stretches=1)
test 'should fallback to devise pepper default configuring' do
begin
- Devise.pepper = ''
+ Devise::Models.pepper = ''
user = new_user
assert_equal encrypt_password(user), user.encrypted_password
- Devise.pepper = 'new_pepper'
+ Devise::Models.pepper = 'new_pepper'
user = new_user
assert_equal encrypt_password(user, 'new_pepper'), user.encrypted_password
- Devise.pepper = '123456'
+ Devise::Models.pepper = '123456'
user = new_user
assert_equal encrypt_password(user, '123456'), user.encrypted_password
ensure
- Devise.pepper = nil
+ Devise::Models.pepper = nil
end
end
test 'should fallback to devise stretches default configuring' do
begin
- default_stretches = Devise.stretches
- Devise.stretches = 1
+ default_stretches = Devise::Models.stretches
+ Devise::Models.stretches = 1
user = new_user
assert_equal encrypt_password(user, nil, nil), user.encrypted_password
ensure
- Devise.stretches = default_stretches
+ Devise::Models.stretches = default_stretches
end
end
View
16 test/models/confirmable_test.rb
@@ -179,20 +179,20 @@ def setup
test 'confirm time should fallback to devise confirm in default configuration' do
begin
- confirm_within = Devise.confirm_within
- Devise.confirm_within = 1.day
+ confirm_within = Devise::Models.confirm_within
+ Devise::Models.confirm_within = 1.day
user = new_user
user.confirmation_sent_at = 2.days.ago
assert_not user.active?
- Devise.confirm_within = 3.days
+ Devise::Models.confirm_within = 3.days
assert user.active?
ensure
- Devise.confirm_within = confirm_within
+ Devise::Models.confirm_within = confirm_within
end
end
test 'should be active when confirmation sent at is not overpast' do
- Devise.confirm_within = 5.days
+ Devise::Models.confirm_within = 5.days
user = create_user
user.confirmation_sent_at = 4.days.ago
assert user.active?
@@ -208,21 +208,21 @@ def setup
end
test 'should not be active when confirmation was sent within the limit' do
- Devise.confirm_within = 5.days
+ Devise::Models.confirm_within = 5.days
user = create_user
user.confirmation_sent_at = 5.days.ago
assert_not user.active?
end
test 'should be active when confirm in is zero' do
- Devise.confirm_within = 0.days
+ Devise::Models.confirm_within = 0.days
user = create_user
user.confirmation_sent_at = Date.today
assert_not user.active?
end
test 'should not be active when confirmation was sent before confirm in time' do
- Devise.confirm_within = 4.days
+ Devise::Models.confirm_within = 4.days
user = create_user
user.confirmation_sent_at = 5.days.ago
assert_not user.active?
View
20 test/models/rememberable_test.rb
@@ -3,7 +3,7 @@
class RememberableTest < ActiveSupport::TestCase
def setup
- Devise.remember_for = 1
+ Devise::Models.remember_for = 1
end
test 'should respond to remember_me attribute' do
@@ -91,37 +91,37 @@ def setup
test 'remember for should fallback to devise remember for default configuration' do
begin
- remember_for = Devise.remember_for
+ remember_for = Devise::Models.remember_for
user = create_user
- Devise.remember_for = 1.day
+ Devise::Models.remember_for = 1.day
user.remember_me!
assert_not user.remember_expired?
- Devise.remember_for = 0.days
+ Devise::Models.remember_for = 0.days
user.remember_me!
assert user.remember_expired?
ensure
- Devise.remember_for = remember_for
+ Devise::Models.remember_for = remember_for
end
end
test 'remember expires at should sum date of creation with remember for configuration' do
- Devise.remember_for = 3.days
+ Devise::Models.remember_for = 3.days
user = create_user
user.remember_me!
assert_equal 3.days.from_now.to_date, user.remember_expires_at.to_date
- Devise.remember_for = 5.days
+ Devise::Models.remember_for = 5.days
assert_equal 5.days.from_now.to_date, user.remember_expires_at.to_date
end
test 'remember should be expired if remember_for is zero' do
- Devise.remember_for = 0.days
+ Devise::Models.remember_for = 0.days
user = create_user
user.remember_me!
assert user.remember_expired?
end
test 'remember should be expired if it was created before limit time' do
- Devise.remember_for = 1.day
+ Devise::Models.remember_for = 1.day
user = create_user
user.remember_me!
user.update_attribute(:remember_created_at, 2.days.ago)
@@ -129,7 +129,7 @@ def setup
end
test 'remember should not be expired if it was created whitin the limit time' do
- Devise.remember_for = 30.days
+ Devise::Models.remember_for = 30.days
user = create_user
user.remember_me!
user.update_attribute(:remember_created_at, 30.days.ago + 2.minutes)
View
0  test/active_record_test.rb → test/models_test.rb
File renamed without changes
Please sign in to comment.
Something went wrong with that request. Please try again.