devise 3.5.10 → 4.7.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (257) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +265 -1129
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +291 -96
  5. data/app/controllers/devise/confirmations_controller.rb +3 -1
  6. data/app/controllers/devise/omniauth_callbacks_controller.rb +8 -6
  7. data/app/controllers/devise/passwords_controller.rb +7 -4
  8. data/app/controllers/devise/registrations_controller.rb +39 -18
  9. data/app/controllers/devise/sessions_controller.rb +9 -7
  10. data/app/controllers/devise/unlocks_controller.rb +4 -2
  11. data/app/controllers/devise_controller.rb +25 -12
  12. data/app/helpers/devise_helper.rb +23 -18
  13. data/app/mailers/devise/mailer.rb +10 -4
  14. data/app/views/devise/confirmations/new.html.erb +2 -2
  15. data/app/views/devise/mailer/email_changed.html.erb +7 -0
  16. data/app/views/devise/passwords/edit.html.erb +3 -3
  17. data/app/views/devise/passwords/new.html.erb +2 -2
  18. data/app/views/devise/registrations/edit.html.erb +9 -5
  19. data/app/views/devise/registrations/new.html.erb +4 -4
  20. data/app/views/devise/sessions/new.html.erb +4 -4
  21. data/app/views/devise/shared/_error_messages.html.erb +15 -0
  22. data/app/views/devise/shared/_links.html.erb +7 -7
  23. data/app/views/devise/unlocks/new.html.erb +2 -2
  24. data/config/locales/en.yml +5 -2
  25. data/lib/devise/controllers/helpers.rb +30 -27
  26. data/lib/devise/controllers/rememberable.rb +3 -1
  27. data/lib/devise/controllers/scoped_views.rb +2 -0
  28. data/lib/devise/controllers/sign_in_out.rb +37 -14
  29. data/lib/devise/controllers/store_location.rb +25 -7
  30. data/lib/devise/controllers/url_helpers.rb +3 -1
  31. data/lib/devise/delegator.rb +2 -0
  32. data/lib/devise/encryptor.rb +6 -4
  33. data/lib/devise/failure_app.rb +75 -38
  34. data/lib/devise/hooks/activatable.rb +2 -0
  35. data/lib/devise/hooks/csrf_cleaner.rb +2 -0
  36. data/lib/devise/hooks/forgetable.rb +2 -0
  37. data/lib/devise/hooks/lockable.rb +6 -1
  38. data/lib/devise/hooks/proxy.rb +3 -1
  39. data/lib/devise/hooks/rememberable.rb +2 -0
  40. data/lib/devise/hooks/timeoutable.rb +4 -2
  41. data/lib/devise/hooks/trackable.rb +2 -0
  42. data/lib/devise/mailers/helpers.rb +7 -4
  43. data/lib/devise/mapping.rb +3 -1
  44. data/lib/devise/models/authenticatable.rb +57 -31
  45. data/lib/devise/models/confirmable.rb +90 -29
  46. data/lib/devise/models/database_authenticatable.rb +93 -22
  47. data/lib/devise/models/lockable.rb +11 -5
  48. data/lib/devise/models/omniauthable.rb +2 -0
  49. data/lib/devise/models/recoverable.rb +33 -21
  50. data/lib/devise/models/registerable.rb +4 -0
  51. data/lib/devise/models/rememberable.rb +6 -11
  52. data/lib/devise/models/timeoutable.rb +2 -0
  53. data/lib/devise/models/trackable.rb +15 -1
  54. data/lib/devise/models/validatable.rb +10 -3
  55. data/lib/devise/models.rb +3 -1
  56. data/lib/devise/modules.rb +2 -0
  57. data/lib/devise/omniauth/config.rb +2 -0
  58. data/lib/devise/omniauth/url_helpers.rb +14 -5
  59. data/lib/devise/omniauth.rb +2 -0
  60. data/lib/devise/orm/active_record.rb +5 -1
  61. data/lib/devise/orm/mongoid.rb +6 -2
  62. data/lib/devise/parameter_filter.rb +4 -0
  63. data/lib/devise/parameter_sanitizer.rb +139 -65
  64. data/lib/devise/rails/routes.rb +48 -37
  65. data/lib/devise/rails/warden_compat.rb +3 -10
  66. data/lib/devise/rails.rb +7 -16
  67. data/lib/devise/secret_key_finder.rb +27 -0
  68. data/lib/devise/strategies/authenticatable.rb +3 -1
  69. data/lib/devise/strategies/base.rb +2 -0
  70. data/lib/devise/strategies/database_authenticatable.rb +11 -4
  71. data/lib/devise/strategies/rememberable.rb +2 -0
  72. data/lib/devise/test/controller_helpers.rb +167 -0
  73. data/lib/devise/test/integration_helpers.rb +63 -0
  74. data/lib/devise/test_helpers.rb +7 -124
  75. data/lib/devise/time_inflector.rb +2 -0
  76. data/lib/devise/token_generator.rb +3 -41
  77. data/lib/devise/version.rb +3 -1
  78. data/lib/devise.rb +57 -40
  79. data/lib/generators/active_record/devise_generator.rb +29 -10
  80. data/lib/generators/active_record/templates/migration.rb +4 -2
  81. data/lib/generators/active_record/templates/migration_existing.rb +4 -2
  82. data/lib/generators/devise/controllers_generator.rb +3 -1
  83. data/lib/generators/devise/devise_generator.rb +5 -3
  84. data/lib/generators/devise/install_generator.rb +18 -5
  85. data/lib/generators/devise/orm_helpers.rb +10 -21
  86. data/lib/generators/devise/views_generator.rb +8 -9
  87. data/lib/generators/mongoid/devise_generator.rb +7 -5
  88. data/lib/generators/templates/README +9 -8
  89. data/lib/generators/templates/controllers/confirmations_controller.rb +2 -0
  90. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +3 -1
  91. data/lib/generators/templates/controllers/passwords_controller.rb +2 -0
  92. data/lib/generators/templates/controllers/registrations_controller.rb +6 -4
  93. data/lib/generators/templates/controllers/sessions_controller.rb +4 -2
  94. data/lib/generators/templates/controllers/unlocks_controller.rb +2 -0
  95. data/lib/generators/templates/devise.rb +63 -21
  96. data/lib/generators/templates/markerb/email_changed.markerb +7 -0
  97. data/lib/generators/templates/markerb/password_change.markerb +2 -2
  98. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +5 -1
  99. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +10 -2
  100. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +4 -1
  101. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +11 -3
  102. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +11 -3
  103. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +7 -2
  104. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +4 -1
  105. metadata +15 -314
  106. data/.gitignore +0 -10
  107. data/.travis.yml +0 -44
  108. data/.yardopts +0 -9
  109. data/CODE_OF_CONDUCT.md +0 -22
  110. data/CONTRIBUTING.md +0 -16
  111. data/Gemfile +0 -30
  112. data/Gemfile.lock +0 -187
  113. data/Rakefile +0 -36
  114. data/devise.gemspec +0 -27
  115. data/devise.png +0 -0
  116. data/gemfiles/Gemfile.rails-3.2-stable +0 -29
  117. data/gemfiles/Gemfile.rails-3.2-stable.lock +0 -172
  118. data/gemfiles/Gemfile.rails-4.0-stable +0 -30
  119. data/gemfiles/Gemfile.rails-4.0-stable.lock +0 -166
  120. data/gemfiles/Gemfile.rails-4.1-stable +0 -30
  121. data/gemfiles/Gemfile.rails-4.1-stable.lock +0 -171
  122. data/gemfiles/Gemfile.rails-4.2-stable +0 -30
  123. data/gemfiles/Gemfile.rails-4.2-stable.lock +0 -193
  124. data/script/cached-bundle +0 -49
  125. data/script/s3-put +0 -71
  126. data/test/controllers/custom_registrations_controller_test.rb +0 -40
  127. data/test/controllers/custom_strategy_test.rb +0 -62
  128. data/test/controllers/helper_methods_test.rb +0 -21
  129. data/test/controllers/helpers_test.rb +0 -316
  130. data/test/controllers/inherited_controller_i18n_messages_test.rb +0 -51
  131. data/test/controllers/internal_helpers_test.rb +0 -129
  132. data/test/controllers/load_hooks_controller_test.rb +0 -19
  133. data/test/controllers/passwords_controller_test.rb +0 -31
  134. data/test/controllers/sessions_controller_test.rb +0 -103
  135. data/test/controllers/url_helpers_test.rb +0 -65
  136. data/test/delegator_test.rb +0 -19
  137. data/test/devise_test.rb +0 -107
  138. data/test/failure_app_test.rb +0 -315
  139. data/test/generators/active_record_generator_test.rb +0 -109
  140. data/test/generators/controllers_generator_test.rb +0 -48
  141. data/test/generators/devise_generator_test.rb +0 -39
  142. data/test/generators/install_generator_test.rb +0 -13
  143. data/test/generators/mongoid_generator_test.rb +0 -23
  144. data/test/generators/views_generator_test.rb +0 -103
  145. data/test/helpers/devise_helper_test.rb +0 -49
  146. data/test/integration/authenticatable_test.rb +0 -729
  147. data/test/integration/confirmable_test.rb +0 -324
  148. data/test/integration/database_authenticatable_test.rb +0 -95
  149. data/test/integration/http_authenticatable_test.rb +0 -105
  150. data/test/integration/lockable_test.rb +0 -239
  151. data/test/integration/omniauthable_test.rb +0 -135
  152. data/test/integration/recoverable_test.rb +0 -347
  153. data/test/integration/registerable_test.rb +0 -359
  154. data/test/integration/rememberable_test.rb +0 -214
  155. data/test/integration/timeoutable_test.rb +0 -184
  156. data/test/integration/trackable_test.rb +0 -92
  157. data/test/mailers/confirmation_instructions_test.rb +0 -115
  158. data/test/mailers/reset_password_instructions_test.rb +0 -96
  159. data/test/mailers/unlock_instructions_test.rb +0 -91
  160. data/test/mapping_test.rb +0 -134
  161. data/test/models/authenticatable_test.rb +0 -23
  162. data/test/models/confirmable_test.rb +0 -511
  163. data/test/models/database_authenticatable_test.rb +0 -269
  164. data/test/models/lockable_test.rb +0 -350
  165. data/test/models/omniauthable_test.rb +0 -7
  166. data/test/models/recoverable_test.rb +0 -251
  167. data/test/models/registerable_test.rb +0 -7
  168. data/test/models/rememberable_test.rb +0 -169
  169. data/test/models/serializable_test.rb +0 -49
  170. data/test/models/timeoutable_test.rb +0 -51
  171. data/test/models/trackable_test.rb +0 -41
  172. data/test/models/validatable_test.rb +0 -127
  173. data/test/models_test.rb +0 -153
  174. data/test/omniauth/config_test.rb +0 -57
  175. data/test/omniauth/url_helpers_test.rb +0 -54
  176. data/test/orm/active_record.rb +0 -10
  177. data/test/orm/mongoid.rb +0 -13
  178. data/test/parameter_sanitizer_test.rb +0 -81
  179. data/test/rails_app/Rakefile +0 -6
  180. data/test/rails_app/app/active_record/admin.rb +0 -6
  181. data/test/rails_app/app/active_record/shim.rb +0 -2
  182. data/test/rails_app/app/active_record/user.rb +0 -6
  183. data/test/rails_app/app/active_record/user_on_engine.rb +0 -7
  184. data/test/rails_app/app/active_record/user_on_main_app.rb +0 -7
  185. data/test/rails_app/app/active_record/user_without_email.rb +0 -8
  186. data/test/rails_app/app/controllers/admins/sessions_controller.rb +0 -6
  187. data/test/rails_app/app/controllers/admins_controller.rb +0 -6
  188. data/test/rails_app/app/controllers/application_controller.rb +0 -12
  189. data/test/rails_app/app/controllers/application_with_fake_engine.rb +0 -30
  190. data/test/rails_app/app/controllers/custom/registrations_controller.rb +0 -31
  191. data/test/rails_app/app/controllers/home_controller.rb +0 -25
  192. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +0 -2
  193. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +0 -2
  194. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +0 -14
  195. data/test/rails_app/app/controllers/users_controller.rb +0 -31
  196. data/test/rails_app/app/helpers/application_helper.rb +0 -3
  197. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +0 -3
  198. data/test/rails_app/app/mailers/users/mailer.rb +0 -3
  199. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +0 -4
  200. data/test/rails_app/app/mongoid/admin.rb +0 -29
  201. data/test/rails_app/app/mongoid/shim.rb +0 -23
  202. data/test/rails_app/app/mongoid/user.rb +0 -39
  203. data/test/rails_app/app/mongoid/user_on_engine.rb +0 -39
  204. data/test/rails_app/app/mongoid/user_on_main_app.rb +0 -39
  205. data/test/rails_app/app/mongoid/user_without_email.rb +0 -33
  206. data/test/rails_app/app/views/admins/index.html.erb +0 -1
  207. data/test/rails_app/app/views/admins/sessions/new.html.erb +0 -2
  208. data/test/rails_app/app/views/home/admin_dashboard.html.erb +0 -1
  209. data/test/rails_app/app/views/home/index.html.erb +0 -1
  210. data/test/rails_app/app/views/home/join.html.erb +0 -1
  211. data/test/rails_app/app/views/home/private.html.erb +0 -1
  212. data/test/rails_app/app/views/home/user_dashboard.html.erb +0 -1
  213. data/test/rails_app/app/views/layouts/application.html.erb +0 -24
  214. data/test/rails_app/app/views/users/edit_form.html.erb +0 -1
  215. data/test/rails_app/app/views/users/index.html.erb +0 -1
  216. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +0 -1
  217. data/test/rails_app/app/views/users/sessions/new.html.erb +0 -1
  218. data/test/rails_app/bin/bundle +0 -3
  219. data/test/rails_app/bin/rails +0 -4
  220. data/test/rails_app/bin/rake +0 -4
  221. data/test/rails_app/config/application.rb +0 -40
  222. data/test/rails_app/config/boot.rb +0 -14
  223. data/test/rails_app/config/database.yml +0 -18
  224. data/test/rails_app/config/environment.rb +0 -5
  225. data/test/rails_app/config/environments/development.rb +0 -30
  226. data/test/rails_app/config/environments/production.rb +0 -84
  227. data/test/rails_app/config/environments/test.rb +0 -41
  228. data/test/rails_app/config/initializers/backtrace_silencers.rb +0 -7
  229. data/test/rails_app/config/initializers/devise.rb +0 -180
  230. data/test/rails_app/config/initializers/inflections.rb +0 -2
  231. data/test/rails_app/config/initializers/secret_token.rb +0 -8
  232. data/test/rails_app/config/initializers/session_store.rb +0 -1
  233. data/test/rails_app/config/routes.rb +0 -125
  234. data/test/rails_app/config.ru +0 -4
  235. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +0 -71
  236. data/test/rails_app/db/schema.rb +0 -55
  237. data/test/rails_app/lib/shared_admin.rb +0 -17
  238. data/test/rails_app/lib/shared_user.rb +0 -29
  239. data/test/rails_app/lib/shared_user_without_email.rb +0 -26
  240. data/test/rails_app/lib/shared_user_without_omniauth.rb +0 -13
  241. data/test/rails_app/public/404.html +0 -26
  242. data/test/rails_app/public/422.html +0 -26
  243. data/test/rails_app/public/500.html +0 -26
  244. data/test/rails_app/public/favicon.ico +0 -0
  245. data/test/rails_test.rb +0 -9
  246. data/test/routes_test.rb +0 -264
  247. data/test/support/action_controller/record_identifier.rb +0 -10
  248. data/test/support/assertions.rb +0 -39
  249. data/test/support/helpers.rb +0 -77
  250. data/test/support/integration.rb +0 -92
  251. data/test/support/locale/en.yml +0 -8
  252. data/test/support/mongoid.yml +0 -6
  253. data/test/support/webrat/integrations/rails.rb +0 -24
  254. data/test/test_helper.rb +0 -34
  255. data/test/test_helpers_test.rb +0 -178
  256. data/test/test_models.rb +0 -33
  257. data/test/time_helpers.rb +0 -137
@@ -1,178 +0,0 @@
1
- require 'test_helper'
2
-
3
- class TestHelpersTest < ActionController::TestCase
4
- tests UsersController
5
- include Devise::TestHelpers
6
-
7
- test "redirects if attempting to access a page unauthenticated" do
8
- get :index
9
- assert_redirected_to new_user_session_path
10
- assert_equal "You need to sign in or sign up before continuing.", flash[:alert]
11
- end
12
-
13
- test "redirects if attempting to access a page with an unconfirmed account" do
14
- swap Devise, allow_unconfirmed_access_for: 0.days do
15
- user = create_user
16
- assert !user.active_for_authentication?
17
-
18
- sign_in user
19
- get :index
20
- assert_redirected_to new_user_session_path
21
- end
22
- end
23
-
24
- test "returns nil if accessing current_user with an unconfirmed account" do
25
- swap Devise, allow_unconfirmed_access_for: 0.days do
26
- user = create_user
27
- assert !user.active_for_authentication?
28
-
29
- sign_in user
30
- get :accept, id: user
31
- assert_nil assigns(:current_user)
32
- end
33
- end
34
-
35
- test "does not redirect with valid user" do
36
- user = create_user
37
- user.confirm
38
-
39
- sign_in user
40
- get :index
41
- assert_response :success
42
- end
43
-
44
- test "does not redirect with valid user after failed first attempt" do
45
- get :index
46
- assert_response :redirect
47
-
48
- user = create_user
49
- user.confirm
50
-
51
- sign_in user
52
- get :index
53
- assert_response :success
54
- end
55
-
56
- test "redirects if valid user signed out" do
57
- user = create_user
58
- user.confirm
59
-
60
- sign_in user
61
- get :index
62
-
63
- sign_out user
64
- get :index
65
- assert_redirected_to new_user_session_path
66
- end
67
-
68
- test "respects custom failure app" do
69
- custom_failure_app = Class.new(Devise::FailureApp) do
70
- def redirect
71
- self.status = 306
72
- end
73
- end
74
-
75
- swap Devise.warden_config, failure_app: custom_failure_app do
76
- get :index
77
- assert_response 306
78
- end
79
- end
80
-
81
- test "passes given headers from the failure app to the response" do
82
- custom_failure_app = Class.new(Devise::FailureApp) do
83
- def respond
84
- self.status = 401
85
- self.response.headers["CUSTOMHEADER"] = 1
86
- end
87
- end
88
-
89
- swap Devise.warden_config, failure_app: custom_failure_app do
90
- sign_in create_user
91
- get :index
92
- assert_equal 1, @response.headers["CUSTOMHEADER"]
93
- end
94
- end
95
-
96
- test "returns the body of a failure app" do
97
- get :index
98
- assert_equal response.body, "<html><body>You are being <a href=\"http://test.host/users/sign_in\">redirected</a>.</body></html>"
99
- end
100
-
101
- test "defined Warden after_authentication callback should not be called when sign_in is called" do
102
- begin
103
- Warden::Manager.after_authentication do |user, auth, opts|
104
- flunk "callback was called while it should not"
105
- end
106
-
107
- user = create_user
108
- user.confirm
109
- sign_in user
110
- ensure
111
- Warden::Manager._after_set_user.pop
112
- end
113
- end
114
-
115
- test "defined Warden before_logout callback should not be called when sign_out is called" do
116
- begin
117
- Warden::Manager.before_logout do |user, auth, opts|
118
- flunk "callback was called while it should not"
119
- end
120
- user = create_user
121
- user.confirm
122
-
123
- sign_in user
124
- sign_out user
125
- ensure
126
- Warden::Manager._before_logout.pop
127
- end
128
- end
129
-
130
- test "before_failure call should work" do
131
- begin
132
- executed = false
133
- Warden::Manager.before_failure do |env,opts|
134
- executed = true
135
- end
136
-
137
- user = create_user
138
- sign_in user
139
-
140
- get :index
141
- assert executed
142
- ensure
143
- Warden::Manager._before_failure.pop
144
- end
145
- end
146
-
147
- test "allows to sign in with different users" do
148
- first_user = create_user
149
- first_user.confirm
150
-
151
- sign_in first_user
152
- get :index
153
- assert_match /User ##{first_user.id}/, @response.body
154
- sign_out first_user
155
-
156
- second_user = create_user
157
- second_user.confirm
158
-
159
- sign_in second_user
160
- get :index
161
- assert_match /User ##{second_user.id}/, @response.body
162
- end
163
-
164
- test "creates a new warden proxy if the request object has changed" do
165
- old_warden_proxy = warden
166
- @request = ActionController::TestRequest.new
167
- new_warden_proxy = warden
168
-
169
- assert_not_equal old_warden_proxy, new_warden_proxy
170
- end
171
-
172
- test "doesn't create a new warden proxy if the request object hasn't changed" do
173
- old_warden_proxy = warden
174
- new_warden_proxy = warden
175
-
176
- assert_equal old_warden_proxy, new_warden_proxy
177
- end
178
- end
data/test/test_models.rb DELETED
@@ -1,33 +0,0 @@
1
- class Configurable < User
2
- devise :database_authenticatable, :confirmable, :rememberable, :timeoutable, :lockable,
3
- stretches: 15, pepper: 'abcdef', allow_unconfirmed_access_for: 5.days,
4
- remember_for: 7.days, timeout_in: 15.minutes, unlock_in: 10.days
5
- end
6
-
7
- class WithValidation < Admin
8
- devise :database_authenticatable, :validatable, password_length: 2..6
9
- end
10
-
11
- class UserWithValidation < User
12
- validates_presence_of :username
13
- end
14
-
15
- class UserWithCustomEncryption < User
16
- protected
17
- def password_digest(password)
18
- password.reverse
19
- end
20
- end
21
-
22
- class UserWithVirtualAttributes < User
23
- devise case_insensitive_keys: [:email, :email_confirmation]
24
- validates :email, presence: true, confirmation: { on: :create }
25
- end
26
-
27
- class Several < Admin
28
- devise :validatable
29
- devise :lockable
30
- end
31
-
32
- class Inheritable < Admin
33
- end
data/test/time_helpers.rb DELETED
@@ -1,137 +0,0 @@
1
- # A copy of Rails time helpers. With this file we can support the `travel_to`
2
- # helper for Rails versions prior 4.1.
3
- # File origin: https://github.com/rails/rails/blob/52ce6ece8c8f74064bb64e0a0b1ddd83092718e1/activesupport/lib/active_support/testing/time_helpers.rb
4
- module ActiveSupport
5
- module Testing
6
- class SimpleStubs # :nodoc:
7
- Stub = Struct.new(:object, :method_name, :original_method)
8
-
9
- def initialize
10
- @stubs = {}
11
- end
12
-
13
- def stub_object(object, method_name, return_value)
14
- key = [object.object_id, method_name]
15
-
16
- if stub = @stubs[key]
17
- unstub_object(stub)
18
- end
19
-
20
- new_name = "__simple_stub__#{method_name}"
21
-
22
- @stubs[key] = Stub.new(object, method_name, new_name)
23
-
24
- object.singleton_class.send :alias_method, new_name, method_name
25
- object.define_singleton_method(method_name) { return_value }
26
- end
27
-
28
- def unstub_all!
29
- @stubs.each_value do |stub|
30
- unstub_object(stub)
31
- end
32
- @stubs = {}
33
- end
34
-
35
- private
36
-
37
- def unstub_object(stub)
38
- singleton_class = stub.object.singleton_class
39
- singleton_class.send :undef_method, stub.method_name
40
- singleton_class.send :alias_method, stub.method_name, stub.original_method
41
- singleton_class.send :undef_method, stub.original_method
42
- end
43
- end
44
-
45
- # Contains helpers that help you test passage of time.
46
- module TimeHelpers
47
- # Changes current time to the time in the future or in the past by a given time difference by
48
- # stubbing +Time.now+, +Date.today+, and +DateTime.now+.
49
- #
50
- # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
51
- # travel 1.day
52
- # Time.current # => Sun, 10 Nov 2013 15:34:49 EST -05:00
53
- # Date.current # => Sun, 10 Nov 2013
54
- # DateTime.current # => Sun, 10 Nov 2013 15:34:49 -0500
55
- #
56
- # This method also accepts a block, which will return the current time back to its original
57
- # state at the end of the block:
58
- #
59
- # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
60
- # travel 1.day do
61
- # User.create.created_at # => Sun, 10 Nov 2013 15:34:49 EST -05:00
62
- # end
63
- # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
64
- def travel(duration, &block)
65
- travel_to Time.now + duration, &block
66
- end
67
-
68
- # Changes current time to the given time by stubbing +Time.now+,
69
- # +Date.today+, and +DateTime.now+ to return the time or date passed into this method.
70
- #
71
- # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
72
- # travel_to Time.new(2004, 11, 24, 01, 04, 44)
73
- # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
74
- # Date.current # => Wed, 24 Nov 2004
75
- # DateTime.current # => Wed, 24 Nov 2004 01:04:44 -0500
76
- #
77
- # Dates are taken as their timestamp at the beginning of the day in the
78
- # application time zone. <tt>Time.current</tt> returns said timestamp,
79
- # and <tt>Time.now</tt> its equivalent in the system time zone. Similarly,
80
- # <tt>Date.current</tt> returns a date equal to the argument, and
81
- # <tt>Date.today</tt> the date according to <tt>Time.now</tt>, which may
82
- # be different. (Note that you rarely want to deal with <tt>Time.now</tt>,
83
- # or <tt>Date.today</tt>, in order to honor the application time zone
84
- # please always use <tt>Time.current</tt> and <tt>Date.current</tt>.)
85
- #
86
- # Note that the usec for the time passed will be set to 0 to prevent rounding
87
- # errors with external services, like MySQL (which will round instead of floor,
88
- # leading to off-by-one-second errors).
89
- #
90
- # This method also accepts a block, which will return the current time back to its original
91
- # state at the end of the block:
92
- #
93
- # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
94
- # travel_to Time.new(2004, 11, 24, 01, 04, 44) do
95
- # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
96
- # end
97
- # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
98
- def travel_to(date_or_time)
99
- if date_or_time.is_a?(Date) && !date_or_time.is_a?(DateTime)
100
- now = date_or_time.midnight.to_time
101
- else
102
- now = date_or_time.to_time.change(usec: 0)
103
- end
104
-
105
- simple_stubs.stub_object(Time, :now, now)
106
- simple_stubs.stub_object(Date, :today, now.to_date)
107
- simple_stubs.stub_object(DateTime, :now, now.to_datetime)
108
-
109
- if block_given?
110
- begin
111
- yield
112
- ensure
113
- travel_back
114
- end
115
- end
116
- end
117
-
118
- # Returns the current time back to its original state, by removing the stubs added by
119
- # `travel` and `travel_to`.
120
- #
121
- # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
122
- # travel_to Time.new(2004, 11, 24, 01, 04, 44)
123
- # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
124
- # travel_back
125
- # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
126
- def travel_back
127
- simple_stubs.unstub_all!
128
- end
129
-
130
- private
131
-
132
- def simple_stubs
133
- @simple_stubs ||= SimpleStubs.new
134
- end
135
- end
136
- end
137
- end