Commit b84e8fa873c483a4dbea9383bc826f3d7995e005

Authored by Braulio Bhavamitra
1 parent a294f41d

rails4: from Task.find_by_code to scope

app/controllers/public/account_controller.rb
@@ -16,7 +16,7 @@ class AccountController < ApplicationController @@ -16,7 +16,7 @@ class AccountController < ApplicationController
16 def activate 16 def activate
17 @user = User.find_by_activation_code(params[:activation_code]) if params[:activation_code] 17 @user = User.find_by_activation_code(params[:activation_code]) if params[:activation_code]
18 if @user 18 if @user
19 - unless @user.environment.enabled?('admin_must_approve_new_users') 19 + unless @user.environment.enabled?('admin_must_approve_new_users')
20 if @user.activate 20 if @user.activate
21 @message = _("Your account has been activated, now you can log in!") 21 @message = _("Your account has been activated, now you can log in!")
22 check_redirection 22 check_redirection
@@ -30,7 +30,7 @@ class AccountController < ApplicationController @@ -30,7 +30,7 @@ class AccountController < ApplicationController
30 @user.activation_code = nil 30 @user.activation_code = nil
31 @user.save! 31 @user.save!
32 redirect_to :controller => :home 32 redirect_to :controller => :home
33 - end 33 + end
34 end 34 end
35 else 35 else
36 session[:notice] = _("It looks like you're trying to activate an account. Perhaps have already activated this account?") 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,7 +113,7 @@ class AccountController < ApplicationController
113 @user.signup! 113 @user.signup!
114 owner_role = Role.find_by_name('owner') 114 owner_role = Role.find_by_name('owner')
115 @user.person.affiliate(@user.person, [owner_role]) if owner_role 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 if invitation 117 if invitation
118 invitation.update_attributes!({:friend => @user.person}) 118 invitation.update_attributes!({:friend => @user.person})
119 invitation.finish 119 invitation.finish
@@ -205,7 +205,7 @@ class AccountController < ApplicationController @@ -205,7 +205,7 @@ class AccountController < ApplicationController
205 # 205 #
206 # Posts back. 206 # Posts back.
207 def new_password 207 def new_password
208 - @change_password = ChangePassword.find_by_code(params[:code]) 208 + @change_password = ChangePassword.from_code params[:code]
209 209
210 unless @change_password 210 unless @change_password
211 render :action => 'invalid_change_password_code', :status => 403 211 render :action => 'invalid_change_password_code', :status => 403
@@ -398,7 +398,7 @@ class AccountController < ApplicationController @@ -398,7 +398,7 @@ class AccountController < ApplicationController
398 end 398 end
399 399
400 def load_enterprise_activation 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 end 402 end
403 403
404 def load_enterprise 404 def load_enterprise
app/models/task.rb
@@ -50,7 +50,7 @@ class Task < ActiveRecord::Base @@ -50,7 +50,7 @@ class Task < ActiveRecord::Base
50 before_validation(:on => :create) do |task| 50 before_validation(:on => :create) do |task|
51 if task.code.nil? 51 if task.code.nil?
52 task.code = Task.generate_code(task.code_length) 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 task.code = Task.generate_code(task.code_length) 54 task.code = Task.generate_code(task.code_length)
55 end 55 end
56 end 56 end
@@ -302,6 +302,12 @@ class Task < ActiveRecord::Base @@ -302,6 +302,12 @@ class Task < ActiveRecord::Base
302 end 302 end
303 end 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 class << self 311 class << self
306 312
307 # generates a random code string consisting of length characters (or 36 by 313 # generates a random code string consisting of length characters (or 36 by
@@ -315,14 +321,6 @@ class Task &lt; ActiveRecord::Base @@ -315,14 +321,6 @@ class Task &lt; ActiveRecord::Base
315 code 321 code
316 end 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 def per_page 324 def per_page
327 15 325 15
328 end 326 end
test/functional/account_controller_test.rb
@@ -241,7 +241,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -241,7 +241,7 @@ class AccountControllerTest &lt; ActionController::TestCase
241 241
242 should 'provide interface for entering new password' do 242 should 'provide interface for entering new password' do
243 change = ChangePassword.new 243 change = ChangePassword.new
244 - ChangePassword.expects(:find_by_code).with('osidufgiashfkjsadfhkj99999').returns(change) 244 + ChangePassword.expects(:from_code).with('osidufgiashfkjsadfhkj99999').returns(change)
245 person = mock 245 person = mock
246 person.stubs(:identifier).returns('joe') 246 person.stubs(:identifier).returns('joe')
247 person.stubs(:name).returns('Joe') 247 person.stubs(:name).returns('Joe')
@@ -253,7 +253,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -253,7 +253,7 @@ class AccountControllerTest &lt; ActionController::TestCase
253 253
254 should 'actually change password after entering new password' do 254 should 'actually change password after entering new password' do
255 change = ChangePassword.new 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 requestor = mock 258 requestor = mock
259 requestor.stubs(:identifier).returns('joe') 259 requestor.stubs(:identifier).returns('joe')
@@ -323,7 +323,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -323,7 +323,7 @@ class AccountControllerTest &lt; ActionController::TestCase
323 person = create_user('mylogin').person 323 person = create_user('mylogin').person
324 login_as(person.identifier) 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 get :activation_question, :enterprise_code => 'some_invalid_code' 328 get :activation_question, :enterprise_code => 'some_invalid_code'
329 329
@@ -338,7 +338,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -338,7 +338,7 @@ class AccountControllerTest &lt; ActionController::TestCase
338 ent.update_attribute(:cnpj, '0'*14) 338 ent.update_attribute(:cnpj, '0'*14)
339 task = mock 339 task = mock
340 task.expects(:enterprise).returns(ent).at_least_once 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 get :activation_question, :enterprise_code => '0123456789' 343 get :activation_question, :enterprise_code => '0123456789'
344 344
@@ -353,7 +353,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -353,7 +353,7 @@ class AccountControllerTest &lt; ActionController::TestCase
353 353
354 task = mock 354 task = mock
355 task.expects(:enterprise).returns(ent).at_least_once 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 get :activation_question, :enterprise_code => '0123456789' 358 get :activation_question, :enterprise_code => '0123456789'
359 359
@@ -368,7 +368,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -368,7 +368,7 @@ class AccountControllerTest &lt; ActionController::TestCase
368 368
369 task = mock 369 task = mock
370 task.expects(:enterprise).returns(ent).at_least_once 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 get :activation_question, :enterprise_code => '0123456789' 373 get :activation_question, :enterprise_code => '0123456789'
374 374
@@ -384,7 +384,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -384,7 +384,7 @@ class AccountControllerTest &lt; ActionController::TestCase
384 384
385 task = mock 385 task = mock
386 task.expects(:enterprise).returns(ent).at_least_once 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 get :activation_question, :enterprise_code => '0123456789' 389 get :activation_question, :enterprise_code => '0123456789'
390 390
@@ -400,7 +400,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -400,7 +400,7 @@ class AccountControllerTest &lt; ActionController::TestCase
400 400
401 task = mock 401 task = mock
402 task.expects(:enterprise).returns(ent).at_least_once 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 get :activation_question, :enterprise_code => '0123456789' 405 get :activation_question, :enterprise_code => '0123456789'
406 406
@@ -417,7 +417,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -417,7 +417,7 @@ class AccountControllerTest &lt; ActionController::TestCase
417 417
418 task = mock 418 task = mock
419 task.expects(:enterprise).returns(ent).at_least_once 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 get :activation_question, :enterprise_code => '0123456789' 422 get :activation_question, :enterprise_code => '0123456789'
423 423
@@ -433,7 +433,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -433,7 +433,7 @@ class AccountControllerTest &lt; ActionController::TestCase
433 433
434 task = mock 434 task = mock
435 task.expects(:enterprise).returns(ent).at_least_once 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 get :activation_question, :enterprise_code => '0123456789' 438 get :activation_question, :enterprise_code => '0123456789'
439 439
@@ -446,7 +446,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -446,7 +446,7 @@ class AccountControllerTest &lt; ActionController::TestCase
446 446
447 task = mock 447 task = mock
448 task.expects(:enterprise).returns(ent).never 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 post :accept_terms, :enterprise_code => '0123456789', :answer => '1998' 451 post :accept_terms, :enterprise_code => '0123456789', :answer => '1998'
452 452
@@ -462,7 +462,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -462,7 +462,7 @@ class AccountControllerTest &lt; ActionController::TestCase
462 462
463 task = mock 463 task = mock
464 task.expects(:enterprise).returns(ent).at_least_once 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 post :accept_terms, :enterprise_code => '0123456789', :answer => '1997' 467 post :accept_terms, :enterprise_code => '0123456789', :answer => '1997'
468 468
@@ -484,7 +484,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -484,7 +484,7 @@ class AccountControllerTest &lt; ActionController::TestCase
484 ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false) 484 ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false)
485 ent.update_attribute(:foundation_year, 1998) 485 ent.update_attribute(:foundation_year, 1998)
486 task = EnterpriseActivation.create!(:enterprise => ent) 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 post :accept_terms, :enterprise_code => '0123456789', :answer => '1998' 489 post :accept_terms, :enterprise_code => '0123456789', :answer => '1998'
490 490
@@ -503,7 +503,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -503,7 +503,7 @@ class AccountControllerTest &lt; ActionController::TestCase
503 503
504 task = mock 504 task = mock
505 task.expects(:enterprise).returns(ent).at_least_once 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 get :accept_terms, :enterprise_code => '0123456789', :answer => 1998 508 get :accept_terms, :enterprise_code => '0123456789', :answer => 1998
509 509
@@ -517,7 +517,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -517,7 +517,7 @@ class AccountControllerTest &lt; ActionController::TestCase
517 ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false) 517 ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false)
518 ent.update_attribute(:foundation_year, 1998) 518 ent.update_attribute(:foundation_year, 1998)
519 task = EnterpriseActivation.create!(:enterprise => ent) 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 post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true 522 post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true
523 523
@@ -531,7 +531,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -531,7 +531,7 @@ class AccountControllerTest &lt; ActionController::TestCase
531 login_as(p.identifier) 531 login_as(p.identifier)
532 532
533 task = EnterpriseActivation.create!(:enterprise => ent) 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 post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => false 536 post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => false
537 ent.reload 537 ent.reload
@@ -547,7 +547,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -547,7 +547,7 @@ class AccountControllerTest &lt; ActionController::TestCase
547 login_as(p.identifier) 547 login_as(p.identifier)
548 548
549 task = EnterpriseActivation.create!(:enterprise => ent) 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 post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true 552 post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true
553 ent.reload 553 ent.reload
@@ -566,7 +566,7 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -566,7 +566,7 @@ class AccountControllerTest &lt; ActionController::TestCase
566 ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false) 566 ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false)
567 ent.update_attribute(:foundation_year, 1998) 567 ent.update_attribute(:foundation_year, 1998)
568 task = EnterpriseActivation.create!(:enterprise => ent) 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 post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true 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 &lt; ActiveSupport::TestCase @@ -131,7 +131,7 @@ class TaskTest &lt; ActiveSupport::TestCase
131 131
132 task.cancel 132 task.cancel
133 133
134 - assert_nil Task.find_by_code(task.code) 134 + assert_nil Task.from_code(task.code)
135 end 135 end
136 136
137 should 'be able to find active tasks' do 137 should 'be able to find active tasks' do
@@ -139,7 +139,7 @@ class TaskTest &lt; ActiveSupport::TestCase @@ -139,7 +139,7 @@ class TaskTest &lt; ActiveSupport::TestCase
139 task.requestor = sample_user 139 task.requestor = sample_user
140 task.save! 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 end 143 end
144 144
145 should 'use 36-chars codes by default' do 145 should 'use 36-chars codes by default' do