Commit b84e8fa873c483a4dbea9383bc826f3d7995e005
1 parent
a294f41d
Exists in
master
and in
20 other branches
rails4: from Task.find_by_code to scope
Showing
4 changed files
with
32 additions
and
34 deletions
Show diff stats
app/controllers/public/account_controller.rb
... | ... | @@ -16,7 +16,7 @@ class AccountController < ApplicationController |
16 | 16 | def activate |
17 | 17 | @user = User.find_by_activation_code(params[:activation_code]) if params[:activation_code] |
18 | 18 | if @user |
19 | - unless @user.environment.enabled?('admin_must_approve_new_users') | |
19 | + unless @user.environment.enabled?('admin_must_approve_new_users') | |
20 | 20 | if @user.activate |
21 | 21 | @message = _("Your account has been activated, now you can log in!") |
22 | 22 | check_redirection |
... | ... | @@ -30,7 +30,7 @@ class AccountController < ApplicationController |
30 | 30 | @user.activation_code = nil |
31 | 31 | @user.save! |
32 | 32 | redirect_to :controller => :home |
33 | - end | |
33 | + end | |
34 | 34 | end |
35 | 35 | else |
36 | 36 | session[:notice] = _("It looks like you're trying to activate an account. Perhaps have already activated this account?") |
... | ... | @@ -113,7 +113,7 @@ class AccountController < ApplicationController |
113 | 113 | @user.signup! |
114 | 114 | owner_role = Role.find_by_name('owner') |
115 | 115 | @user.person.affiliate(@user.person, [owner_role]) if owner_role |
116 | - invitation = Task.find_by_code(@invitation_code) | |
116 | + invitation = Task.from_code @invitation_code | |
117 | 117 | if invitation |
118 | 118 | invitation.update_attributes!({:friend => @user.person}) |
119 | 119 | invitation.finish |
... | ... | @@ -205,7 +205,7 @@ class AccountController < ApplicationController |
205 | 205 | # |
206 | 206 | # Posts back. |
207 | 207 | def new_password |
208 | - @change_password = ChangePassword.find_by_code(params[:code]) | |
208 | + @change_password = ChangePassword.from_code params[:code] | |
209 | 209 | |
210 | 210 | unless @change_password |
211 | 211 | render :action => 'invalid_change_password_code', :status => 403 |
... | ... | @@ -398,7 +398,7 @@ class AccountController < ApplicationController |
398 | 398 | end |
399 | 399 | |
400 | 400 | def load_enterprise_activation |
401 | - @enterprise_activation ||= EnterpriseActivation.find_by_code(params[:enterprise_code]) | |
401 | + @enterprise_activation ||= EnterpriseActivation.from_code params[:enterprise_code] | |
402 | 402 | end |
403 | 403 | |
404 | 404 | def load_enterprise | ... | ... |
app/models/task.rb
... | ... | @@ -50,7 +50,7 @@ class Task < ActiveRecord::Base |
50 | 50 | before_validation(:on => :create) do |task| |
51 | 51 | if task.code.nil? |
52 | 52 | task.code = Task.generate_code(task.code_length) |
53 | - while (Task.find_by_code(task.code)) | |
53 | + while Task.from_code(task.code).first | |
54 | 54 | task.code = Task.generate_code(task.code_length) |
55 | 55 | end |
56 | 56 | end |
... | ... | @@ -302,6 +302,12 @@ class Task < ActiveRecord::Base |
302 | 302 | end |
303 | 303 | end |
304 | 304 | |
305 | + # finds a task by its (generated) code. Only returns a task with the | |
306 | + # specified code AND with status = Task::Status::ACTIVE. | |
307 | + # | |
308 | + # Can be used in subclasses to find only their instances. | |
309 | + scope :from_code, -> (code) { where code: code, status: Task::Status::ACTIVE } | |
310 | + | |
305 | 311 | class << self |
306 | 312 | |
307 | 313 | # generates a random code string consisting of length characters (or 36 by |
... | ... | @@ -315,14 +321,6 @@ class Task < ActiveRecord::Base |
315 | 321 | code |
316 | 322 | end |
317 | 323 | |
318 | - # finds a task by its (generated) code. Only returns a task with the | |
319 | - # specified code AND with status = Task::Status::ACTIVE. | |
320 | - # | |
321 | - # Can be used in subclasses to find only their instances. | |
322 | - def find_by_code(code) | |
323 | - self.where(code: code, status: Task::Status::ACTIVE) | |
324 | - end | |
325 | - | |
326 | 324 | def per_page |
327 | 325 | 15 |
328 | 326 | end | ... | ... |
test/functional/account_controller_test.rb
... | ... | @@ -241,7 +241,7 @@ class AccountControllerTest < ActionController::TestCase |
241 | 241 | |
242 | 242 | should 'provide interface for entering new password' do |
243 | 243 | change = ChangePassword.new |
244 | - ChangePassword.expects(:find_by_code).with('osidufgiashfkjsadfhkj99999').returns(change) | |
244 | + ChangePassword.expects(:from_code).with('osidufgiashfkjsadfhkj99999').returns(change) | |
245 | 245 | person = mock |
246 | 246 | person.stubs(:identifier).returns('joe') |
247 | 247 | person.stubs(:name).returns('Joe') |
... | ... | @@ -253,7 +253,7 @@ class AccountControllerTest < ActionController::TestCase |
253 | 253 | |
254 | 254 | should 'actually change password after entering new password' do |
255 | 255 | change = ChangePassword.new |
256 | - ChangePassword.expects(:find_by_code).with('osidufgiashfkjsadfhkj99999').returns(change) | |
256 | + ChangePassword.expects(:from_code).with('osidufgiashfkjsadfhkj99999').returns(change) | |
257 | 257 | |
258 | 258 | requestor = mock |
259 | 259 | requestor.stubs(:identifier).returns('joe') |
... | ... | @@ -323,7 +323,7 @@ class AccountControllerTest < ActionController::TestCase |
323 | 323 | person = create_user('mylogin').person |
324 | 324 | login_as(person.identifier) |
325 | 325 | |
326 | - EnterpriseActivation.expects(:find_by_code).with('some_invalid_code').returns(nil).at_least_once | |
326 | + EnterpriseActivation.expects(:from_code).with('some_invalid_code').returns(nil).at_least_once | |
327 | 327 | |
328 | 328 | get :activation_question, :enterprise_code => 'some_invalid_code' |
329 | 329 | |
... | ... | @@ -338,7 +338,7 @@ class AccountControllerTest < ActionController::TestCase |
338 | 338 | ent.update_attribute(:cnpj, '0'*14) |
339 | 339 | task = mock |
340 | 340 | task.expects(:enterprise).returns(ent).at_least_once |
341 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
341 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
342 | 342 | |
343 | 343 | get :activation_question, :enterprise_code => '0123456789' |
344 | 344 | |
... | ... | @@ -353,7 +353,7 @@ class AccountControllerTest < ActionController::TestCase |
353 | 353 | |
354 | 354 | task = mock |
355 | 355 | task.expects(:enterprise).returns(ent).at_least_once |
356 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
356 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
357 | 357 | |
358 | 358 | get :activation_question, :enterprise_code => '0123456789' |
359 | 359 | |
... | ... | @@ -368,7 +368,7 @@ class AccountControllerTest < ActionController::TestCase |
368 | 368 | |
369 | 369 | task = mock |
370 | 370 | task.expects(:enterprise).returns(ent).at_least_once |
371 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
371 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
372 | 372 | |
373 | 373 | get :activation_question, :enterprise_code => '0123456789' |
374 | 374 | |
... | ... | @@ -384,7 +384,7 @@ class AccountControllerTest < ActionController::TestCase |
384 | 384 | |
385 | 385 | task = mock |
386 | 386 | task.expects(:enterprise).returns(ent).at_least_once |
387 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
387 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
388 | 388 | |
389 | 389 | get :activation_question, :enterprise_code => '0123456789' |
390 | 390 | |
... | ... | @@ -400,7 +400,7 @@ class AccountControllerTest < ActionController::TestCase |
400 | 400 | |
401 | 401 | task = mock |
402 | 402 | task.expects(:enterprise).returns(ent).at_least_once |
403 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
403 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
404 | 404 | |
405 | 405 | get :activation_question, :enterprise_code => '0123456789' |
406 | 406 | |
... | ... | @@ -417,7 +417,7 @@ class AccountControllerTest < ActionController::TestCase |
417 | 417 | |
418 | 418 | task = mock |
419 | 419 | task.expects(:enterprise).returns(ent).at_least_once |
420 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
420 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
421 | 421 | |
422 | 422 | get :activation_question, :enterprise_code => '0123456789' |
423 | 423 | |
... | ... | @@ -433,7 +433,7 @@ class AccountControllerTest < ActionController::TestCase |
433 | 433 | |
434 | 434 | task = mock |
435 | 435 | task.expects(:enterprise).returns(ent).at_least_once |
436 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
436 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
437 | 437 | |
438 | 438 | get :activation_question, :enterprise_code => '0123456789' |
439 | 439 | |
... | ... | @@ -446,7 +446,7 @@ class AccountControllerTest < ActionController::TestCase |
446 | 446 | |
447 | 447 | task = mock |
448 | 448 | task.expects(:enterprise).returns(ent).never |
449 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).never | |
449 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).never | |
450 | 450 | |
451 | 451 | post :accept_terms, :enterprise_code => '0123456789', :answer => '1998' |
452 | 452 | |
... | ... | @@ -462,7 +462,7 @@ class AccountControllerTest < ActionController::TestCase |
462 | 462 | |
463 | 463 | task = mock |
464 | 464 | task.expects(:enterprise).returns(ent).at_least_once |
465 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
465 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
466 | 466 | |
467 | 467 | post :accept_terms, :enterprise_code => '0123456789', :answer => '1997' |
468 | 468 | |
... | ... | @@ -484,7 +484,7 @@ class AccountControllerTest < ActionController::TestCase |
484 | 484 | ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false) |
485 | 485 | ent.update_attribute(:foundation_year, 1998) |
486 | 486 | task = EnterpriseActivation.create!(:enterprise => ent) |
487 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
487 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
488 | 488 | |
489 | 489 | post :accept_terms, :enterprise_code => '0123456789', :answer => '1998' |
490 | 490 | |
... | ... | @@ -503,7 +503,7 @@ class AccountControllerTest < ActionController::TestCase |
503 | 503 | |
504 | 504 | task = mock |
505 | 505 | task.expects(:enterprise).returns(ent).at_least_once |
506 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
506 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
507 | 507 | |
508 | 508 | get :accept_terms, :enterprise_code => '0123456789', :answer => 1998 |
509 | 509 | |
... | ... | @@ -517,7 +517,7 @@ class AccountControllerTest < ActionController::TestCase |
517 | 517 | ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false) |
518 | 518 | ent.update_attribute(:foundation_year, 1998) |
519 | 519 | task = EnterpriseActivation.create!(:enterprise => ent) |
520 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).never | |
520 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).never | |
521 | 521 | |
522 | 522 | post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true |
523 | 523 | |
... | ... | @@ -531,7 +531,7 @@ class AccountControllerTest < ActionController::TestCase |
531 | 531 | login_as(p.identifier) |
532 | 532 | |
533 | 533 | task = EnterpriseActivation.create!(:enterprise => ent) |
534 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
534 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
535 | 535 | |
536 | 536 | post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => false |
537 | 537 | ent.reload |
... | ... | @@ -547,7 +547,7 @@ class AccountControllerTest < ActionController::TestCase |
547 | 547 | login_as(p.identifier) |
548 | 548 | |
549 | 549 | task = EnterpriseActivation.create!(:enterprise => ent) |
550 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
550 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
551 | 551 | |
552 | 552 | post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true |
553 | 553 | ent.reload |
... | ... | @@ -566,7 +566,7 @@ class AccountControllerTest < ActionController::TestCase |
566 | 566 | ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false) |
567 | 567 | ent.update_attribute(:foundation_year, 1998) |
568 | 568 | task = EnterpriseActivation.create!(:enterprise => ent) |
569 | - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once | |
569 | + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once | |
570 | 570 | |
571 | 571 | post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true |
572 | 572 | ... | ... |
test/unit/task_test.rb
... | ... | @@ -131,7 +131,7 @@ class TaskTest < ActiveSupport::TestCase |
131 | 131 | |
132 | 132 | task.cancel |
133 | 133 | |
134 | - assert_nil Task.find_by_code(task.code) | |
134 | + assert_nil Task.from_code(task.code) | |
135 | 135 | end |
136 | 136 | |
137 | 137 | should 'be able to find active tasks' do |
... | ... | @@ -139,7 +139,7 @@ class TaskTest < ActiveSupport::TestCase |
139 | 139 | task.requestor = sample_user |
140 | 140 | task.save! |
141 | 141 | |
142 | - assert_not_nil Task.find_by_code(task.code) | |
142 | + assert_not_nil Task.from_code(task.code) | |
143 | 143 | end |
144 | 144 | |
145 | 145 | should 'use 36-chars codes by default' do | ... | ... |