Skip to content
This repository
Browse code

Do not require ActiveRecord to be loaded to use Devise.

  • Loading branch information...
commit e7f809de3c78ab413e43010e4df3bb65556440b3 1 parent 5f49852
José Valim authored
8  CHANGELOG.rdoc
Source Rendered
... ...
@@ -1,3 +1,11 @@
  1
+* enhancement
  2
+  * [#16] Allow devise to be more agnostic. Do not require ActiveRecord to be loaded.
  3
+
  4
+* deprecations
  5
+  * Model configuration is on Devise::Models.config and not on Devise.config anymore.
  6
+
  7
+== 0.3.0
  8
+
1 9
 * bug fix
2 10
   * [#15] Allow yml messages to be configured by not using engine locales
3 11
 
4  README.rdoc
Source Rendered
@@ -111,9 +111,9 @@ In addition to :except, you can provide some options to devise call:
111 111
 
112 112
   devise :all, :remember_for => 2.weeks
113 113
 
114  
-All those values can also be set in a global way by setting them in Devise:
  114
+All those values can also be set in a global way by setting them in Devise::Models:
115 115
 
116  
-  Devise.confirm_within = 1.week
  116
+  Devise::Models.confirm_within = 1.week
117 117
 
118 118
 == Routes
119 119
 
52  lib/devise.rb
@@ -9,54 +9,11 @@ module Devise
9 9
   }.freeze
10 10
 
11 11
   TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE'].freeze
12  
-
13  
-  # Creates configuration values for Devise and for the given module.
14  
-  #
15  
-  #   Devise.model_config(Devise::Authenticable, :stretches, 10)
16  
-  #
17  
-  # The line above creates:
18  
-  #
19  
-  #   1) An accessor called Devise.stretches, which value is used by default;
20  
-  #
21  
-  #   2) Some class methods for your model Model.stretches and Model.stretches=
22  
-  #      which have higher priority than Devise.stretches;
23  
-  #
24  
-  #   3) And an instance method stretches.
25  
-  #
26  
-  # To add the class methods you need to have a module ClassMethods defined
27  
-  # inside the given class.
28  
-  #
29  
-  def self.model_config(mod, accessor, default=nil) #:nodoc:
30  
-    mattr_accessor accessor
31  
-    send(:"#{accessor}=", default)
32  
-
33  
-    mod.class_eval <<-METHOD, __FILE__, __LINE__
34  
-      def #{accessor}
35  
-        self.class.#{accessor}
36  
-      end
37  
-    METHOD
38  
-
39  
-    mod.const_get(:ClassMethods).class_eval <<-METHOD, __FILE__, __LINE__
40  
-      def #{accessor}
41  
-        if defined?(@#{accessor})
42  
-          @#{accessor}
43  
-        elsif superclass.respond_to?(:#{accessor})
44  
-          superclass.#{accessor}
45  
-        else
46  
-          Devise.#{accessor}
47  
-        end
48  
-      end
49  
-
50  
-      def #{accessor}=(value)
51  
-        @#{accessor} = value
52  
-      end
53  
-    METHOD
54  
-  end
55 12
 end
56 13
 
57 14
 # Devise initialization process goes like this:
58 15
 #
59  
-#   1) Include Devise::ActiveRecord and Devise::Migrations
  16
+#   1) Includes in Devise::ActiveRecord and Devise::Migrations
60 17
 #   2) Load and config warden
61 18
 #   3) Load devise mapping structure
62 19
 #   4) Add routes extensions
@@ -64,8 +21,11 @@ def #{accessor}=(value)
64 21
 #   6) Include filters and helpers in controllers and views
65 22
 #
66 23
 Rails.configuration.after_initialize do
67  
-  ActiveRecord::Base.extend Devise::ActiveRecord
68  
-  ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Devise::Migrations
  24
+  if defined?(ActiveRecord)
  25
+    ActiveRecord::Base.extend Devise::Models
  26
+    ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Devise::Migrations
  27
+  end
  28
+
69 29
   I18n.load_path.unshift File.expand_path(File.join(File.dirname(__FILE__), 'devise', 'locales', 'en.yml'))
70 30
 end
71 31
 
54  lib/devise/active_record.rb → lib/devise/models.rb
... ...
@@ -1,5 +1,57 @@
1 1
 module Devise
2  
-  module ActiveRecord
  2
+  module Models
  3
+    # Creates configuration values for Devise and for the given module.
  4
+    #
  5
+    #   Devise::Models.config(Devise::Authenticable, :stretches, 10)
  6
+    #
  7
+    # The line above creates:
  8
+    #
  9
+    #   1) An accessor called Devise::Models.stretches, which value is used by default;
  10
+    #
  11
+    #   2) Some class methods for your model Model.stretches and Model.stretches=
  12
+    #      which have higher priority than Devise.stretches;
  13
+    #
  14
+    #   3) And an instance method stretches.
  15
+    #
  16
+    # To add the class methods you need to have a module ClassMethods defined
  17
+    # inside the given class.
  18
+    #
  19
+    def self.config(mod, accessor, default=nil) #:nodoc:
  20
+      mattr_accessor accessor
  21
+      send(:"#{accessor}=", default)
  22
+
  23
+      # TODO Remove me in a next release
  24
+      Devise.class_eval <<-METHOD, __FILE__, __LINE__
  25
+        def self.#{accessor}=(value)
  26
+          ActiveSupport::Deprecation.warn "Devise.#{accessor}= is deprecated, " <<
  27
+                                          "use Devise::Models.#{accessor}= instead."
  28
+          Devise::Models.#{accessor} = value
  29
+        end
  30
+      METHOD
  31
+
  32
+      mod.class_eval <<-METHOD, __FILE__, __LINE__
  33
+        def #{accessor}
  34
+          self.class.#{accessor}
  35
+        end
  36
+      METHOD
  37
+
  38
+      mod.const_get(:ClassMethods).class_eval <<-METHOD, __FILE__, __LINE__
  39
+        def #{accessor}
  40
+          if defined?(@#{accessor})
  41
+            @#{accessor}
  42
+          elsif superclass.respond_to?(:#{accessor})
  43
+            superclass.#{accessor}
  44
+          else
  45
+            Devise::Models.#{accessor}
  46
+          end
  47
+        end
  48
+
  49
+        def #{accessor}=(value)
  50
+          @#{accessor} = value
  51
+        end
  52
+      METHOD
  53
+    end
  54
+
3 55
     # Shortcut method for including all devise modules inside your model.
4 56
     # You can give some extra options while declaring devise in your model:
5 57
     #
4  lib/devise/models/authenticatable.rb
@@ -90,8 +90,8 @@ def find_or_initialize_with_error_by_email(email)
90 90
         end
91 91
       end
92 92
 
93  
-      Devise.model_config(self, :pepper)
94  
-      Devise.model_config(self, :stretches, 10)
  93
+      Devise::Models.config(self, :pepper)
  94
+      Devise::Models.config(self, :stretches, 10)
95 95
     end
96 96
   end
97 97
 end
2  lib/devise/models/confirmable.rb
@@ -150,7 +150,7 @@ def confirm!(attributes={})
150 150
         end
151 151
       end
152 152
 
153  
-      Devise.model_config(self, :confirm_within, 0.days)
  153
+      Devise::Models.config(self, :confirm_within, 0.days)
154 154
     end
155 155
   end
156 156
 end
2  lib/devise/models/rememberable.rb
@@ -89,7 +89,7 @@ def serialize_from_cookie(cookie)
89 89
         end
90 90
       end
91 91
 
92  
-      Devise.model_config(self, :remember_for, 2.weeks)
  92
+      Devise::Models.config(self, :remember_for, 2.weeks)
93 93
     end
94 94
   end
95 95
 end
4  test/integration/confirmable_test.rb
@@ -59,7 +59,7 @@ def visit_user_confirmation_with_token(confirmation_token)
59 59
   end
60 60
 
61 61
   test 'not confirmed user and setup to block without confirmation should not be able to sign in' do
62  
-    Devise.confirm_within = 0
  62
+    Devise::Models.confirm_within = 0
63 63
     user = sign_in_as_user(:confirm => false)
64 64
 
65 65
     assert_redirected_to new_user_session_path(:unconfirmed => true)
@@ -67,7 +67,7 @@ def visit_user_confirmation_with_token(confirmation_token)
67 67
   end
68 68
 
69 69
   test 'not confirmed user but configured with some days to confirm should be able to sign in' do
70  
-    Devise.confirm_within = 1
  70
+    Devise::Models.confirm_within = 1
71 71
     user = sign_in_as_user(:confirm => false)
72 72
 
73 73
     assert_response :success
4  test/integration/rememberable_test.rb
@@ -3,7 +3,7 @@
3 3
 class RememberMeTest < ActionController::IntegrationTest
4 4
 
5 5
   def create_user_and_remember(add_to_token='')
6  
-    Devise.remember_for = 1
  6
+    Devise::Models.remember_for = 1
7 7
     user = create_user
8 8
     user.remember_me!
9 9
     cookies['remember_token'] = User.serialize_into_cookie(user) + add_to_token
@@ -39,7 +39,7 @@ def create_user_and_remember(add_to_token='')
39 39
 
40 40
   test 'do not remember with token expired' do
41 41
     user = create_user_and_remember
42  
-    Devise.remember_for = 0
  42
+    Devise::Models.remember_for = 0
43 43
     get users_path
44 44
     assert_response :success
45 45
     assert_not warden.authenticated?(:user)
14  test/models/authenticatable_test.rb
@@ -66,28 +66,28 @@ def encrypt_password(user, pepper=nil, stretches=1)
66 66
 
67 67
   test 'should fallback to devise pepper default configuring' do
68 68
     begin
69  
-      Devise.pepper = ''
  69
+      Devise::Models.pepper = ''
70 70
       user = new_user
71 71
       assert_equal encrypt_password(user), user.encrypted_password
72  
-      Devise.pepper = 'new_pepper'
  72
+      Devise::Models.pepper = 'new_pepper'
73 73
       user = new_user
74 74
       assert_equal encrypt_password(user, 'new_pepper'), user.encrypted_password
75  
-      Devise.pepper = '123456'
  75
+      Devise::Models.pepper = '123456'
76 76
       user = new_user
77 77
       assert_equal encrypt_password(user, '123456'), user.encrypted_password
78 78
     ensure
79  
-      Devise.pepper = nil
  79
+      Devise::Models.pepper = nil
80 80
     end
81 81
   end
82 82
 
83 83
   test 'should fallback to devise stretches default configuring' do
84 84
     begin
85  
-      default_stretches = Devise.stretches
86  
-      Devise.stretches = 1
  85
+      default_stretches = Devise::Models.stretches
  86
+      Devise::Models.stretches = 1
87 87
       user = new_user
88 88
       assert_equal encrypt_password(user, nil, nil), user.encrypted_password
89 89
     ensure
90  
-      Devise.stretches = default_stretches
  90
+      Devise::Models.stretches = default_stretches
91 91
     end
92 92
   end
93 93
 
16  test/models/confirmable_test.rb
@@ -179,20 +179,20 @@ def setup
179 179
 
180 180
   test 'confirm time should fallback to devise confirm in default configuration' do
181 181
     begin
182  
-      confirm_within = Devise.confirm_within
183  
-      Devise.confirm_within = 1.day
  182
+      confirm_within = Devise::Models.confirm_within
  183
+      Devise::Models.confirm_within = 1.day
184 184
       user = new_user
185 185
       user.confirmation_sent_at = 2.days.ago
186 186
       assert_not user.active?
187  
-      Devise.confirm_within = 3.days
  187
+      Devise::Models.confirm_within = 3.days
188 188
       assert user.active?
189 189
     ensure
190  
-      Devise.confirm_within = confirm_within
  190
+      Devise::Models.confirm_within = confirm_within
191 191
     end
192 192
   end
193 193
 
194 194
   test 'should be active when confirmation sent at is not overpast' do
195  
-    Devise.confirm_within = 5.days
  195
+    Devise::Models.confirm_within = 5.days
196 196
     user = create_user
197 197
     user.confirmation_sent_at = 4.days.ago
198 198
     assert user.active?
@@ -208,21 +208,21 @@ def setup
208 208
   end
209 209
 
210 210
   test 'should not be active when confirmation was sent within the limit' do
211  
-    Devise.confirm_within = 5.days
  211
+    Devise::Models.confirm_within = 5.days
212 212
     user = create_user
213 213
     user.confirmation_sent_at = 5.days.ago
214 214
     assert_not user.active?
215 215
   end
216 216
 
217 217
   test 'should be active when confirm in is zero' do
218  
-    Devise.confirm_within = 0.days
  218
+    Devise::Models.confirm_within = 0.days
219 219
     user = create_user
220 220
     user.confirmation_sent_at = Date.today
221 221
     assert_not user.active?
222 222
   end
223 223
 
224 224
   test 'should not be active when confirmation was sent before confirm in time' do
225  
-    Devise.confirm_within = 4.days
  225
+    Devise::Models.confirm_within = 4.days
226 226
     user = create_user
227 227
     user.confirmation_sent_at = 5.days.ago
228 228
     assert_not user.active?
20  test/models/rememberable_test.rb
@@ -3,7 +3,7 @@
3 3
 class RememberableTest < ActiveSupport::TestCase
4 4
 
5 5
   def setup
6  
-    Devise.remember_for = 1
  6
+    Devise::Models.remember_for = 1
7 7
   end
8 8
 
9 9
   test 'should respond to remember_me attribute' do
@@ -91,37 +91,37 @@ def setup
91 91
 
92 92
   test 'remember for should fallback to devise remember for default configuration' do
93 93
     begin
94  
-      remember_for = Devise.remember_for
  94
+      remember_for = Devise::Models.remember_for
95 95
       user = create_user
96  
-      Devise.remember_for = 1.day
  96
+      Devise::Models.remember_for = 1.day
97 97
       user.remember_me!
98 98
       assert_not user.remember_expired?
99  
-      Devise.remember_for = 0.days
  99
+      Devise::Models.remember_for = 0.days
100 100
       user.remember_me!
101 101
       assert user.remember_expired?
102 102
     ensure
103  
-      Devise.remember_for = remember_for
  103
+      Devise::Models.remember_for = remember_for
104 104
     end
105 105
   end
106 106
 
107 107
   test 'remember expires at should sum date of creation with remember for configuration' do
108  
-    Devise.remember_for = 3.days
  108
+    Devise::Models.remember_for = 3.days
109 109
     user = create_user
110 110
     user.remember_me!
111 111
     assert_equal 3.days.from_now.to_date, user.remember_expires_at.to_date
112  
-    Devise.remember_for = 5.days
  112
+    Devise::Models.remember_for = 5.days
113 113
     assert_equal 5.days.from_now.to_date, user.remember_expires_at.to_date
114 114
   end
115 115
 
116 116
   test 'remember should be expired if remember_for is zero' do
117  
-    Devise.remember_for = 0.days
  117
+    Devise::Models.remember_for = 0.days
118 118
     user = create_user
119 119
     user.remember_me!
120 120
     assert user.remember_expired?
121 121
   end
122 122
 
123 123
   test 'remember should be expired if it was created before limit time' do
124  
-    Devise.remember_for = 1.day
  124
+    Devise::Models.remember_for = 1.day
125 125
     user = create_user
126 126
     user.remember_me!
127 127
     user.update_attribute(:remember_created_at, 2.days.ago)
@@ -129,7 +129,7 @@ def setup
129 129
   end
130 130
 
131 131
   test 'remember should not be expired if it was created whitin the limit time' do
132  
-    Devise.remember_for = 30.days
  132
+    Devise::Models.remember_for = 30.days
133 133
     user = create_user
134 134
     user.remember_me!
135 135
     user.update_attribute(:remember_created_at, 30.days.ago + 2.minutes)
0  test/active_record_test.rb → test/models_test.rb
File renamed without changes

0 notes on commit e7f809d

Please sign in to comment.
Something went wrong with that request. Please try again.