diff --git a/app/controllers/public/account_controller.rb b/app/controllers/public/account_controller.rb index 18f6419..c8b6685 100644 --- a/app/controllers/public/account_controller.rb +++ b/app/controllers/public/account_controller.rb @@ -16,7 +16,7 @@ class AccountController < ApplicationController def activate @user = User.find_by_activation_code(params[:activation_code]) if params[:activation_code] if @user - unless @user.environment.enabled?('admin_must_approve_new_users') + unless @user.environment.enabled?('admin_must_approve_new_users') if @user.activate @message = _("Your account has been activated, now you can log in!") check_redirection @@ -30,7 +30,7 @@ class AccountController < ApplicationController @user.activation_code = nil @user.save! redirect_to :controller => :home - end + end end else 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 @user.signup! owner_role = Role.find_by_name('owner') @user.person.affiliate(@user.person, [owner_role]) if owner_role - invitation = Task.find_by_code(@invitation_code) + invitation = Task.from_code @invitation_code if invitation invitation.update_attributes!({:friend => @user.person}) invitation.finish @@ -205,7 +205,7 @@ class AccountController < ApplicationController # # Posts back. def new_password - @change_password = ChangePassword.find_by_code(params[:code]) + @change_password = ChangePassword.from_code params[:code] unless @change_password render :action => 'invalid_change_password_code', :status => 403 @@ -398,7 +398,7 @@ class AccountController < ApplicationController end def load_enterprise_activation - @enterprise_activation ||= EnterpriseActivation.find_by_code(params[:enterprise_code]) + @enterprise_activation ||= EnterpriseActivation.from_code params[:enterprise_code] end def load_enterprise diff --git a/app/models/task.rb b/app/models/task.rb index 446ddc4..0c83ab6 100644 --- a/app/models/task.rb +++ b/app/models/task.rb @@ -50,7 +50,7 @@ class Task < ActiveRecord::Base before_validation(:on => :create) do |task| if task.code.nil? task.code = Task.generate_code(task.code_length) - while (Task.find_by_code(task.code)) + while Task.from_code(task.code).first task.code = Task.generate_code(task.code_length) end end @@ -302,6 +302,12 @@ class Task < ActiveRecord::Base end end + # finds a task by its (generated) code. Only returns a task with the + # specified code AND with status = Task::Status::ACTIVE. + # + # Can be used in subclasses to find only their instances. + scope :from_code, -> (code) { where code: code, status: Task::Status::ACTIVE } + class << self # generates a random code string consisting of length characters (or 36 by @@ -315,14 +321,6 @@ class Task < ActiveRecord::Base code end - # finds a task by its (generated) code. Only returns a task with the - # specified code AND with status = Task::Status::ACTIVE. - # - # Can be used in subclasses to find only their instances. - def find_by_code(code) - self.where(code: code, status: Task::Status::ACTIVE) - end - def per_page 15 end diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index e6511b9..fc7152d 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -241,7 +241,7 @@ class AccountControllerTest < ActionController::TestCase should 'provide interface for entering new password' do change = ChangePassword.new - ChangePassword.expects(:find_by_code).with('osidufgiashfkjsadfhkj99999').returns(change) + ChangePassword.expects(:from_code).with('osidufgiashfkjsadfhkj99999').returns(change) person = mock person.stubs(:identifier).returns('joe') person.stubs(:name).returns('Joe') @@ -253,7 +253,7 @@ class AccountControllerTest < ActionController::TestCase should 'actually change password after entering new password' do change = ChangePassword.new - ChangePassword.expects(:find_by_code).with('osidufgiashfkjsadfhkj99999').returns(change) + ChangePassword.expects(:from_code).with('osidufgiashfkjsadfhkj99999').returns(change) requestor = mock requestor.stubs(:identifier).returns('joe') @@ -323,7 +323,7 @@ class AccountControllerTest < ActionController::TestCase person = create_user('mylogin').person login_as(person.identifier) - EnterpriseActivation.expects(:find_by_code).with('some_invalid_code').returns(nil).at_least_once + EnterpriseActivation.expects(:from_code).with('some_invalid_code').returns(nil).at_least_once get :activation_question, :enterprise_code => 'some_invalid_code' @@ -338,7 +338,7 @@ class AccountControllerTest < ActionController::TestCase ent.update_attribute(:cnpj, '0'*14) task = mock task.expects(:enterprise).returns(ent).at_least_once - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once get :activation_question, :enterprise_code => '0123456789' @@ -353,7 +353,7 @@ class AccountControllerTest < ActionController::TestCase task = mock task.expects(:enterprise).returns(ent).at_least_once - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once get :activation_question, :enterprise_code => '0123456789' @@ -368,7 +368,7 @@ class AccountControllerTest < ActionController::TestCase task = mock task.expects(:enterprise).returns(ent).at_least_once - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once get :activation_question, :enterprise_code => '0123456789' @@ -384,7 +384,7 @@ class AccountControllerTest < ActionController::TestCase task = mock task.expects(:enterprise).returns(ent).at_least_once - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once get :activation_question, :enterprise_code => '0123456789' @@ -400,7 +400,7 @@ class AccountControllerTest < ActionController::TestCase task = mock task.expects(:enterprise).returns(ent).at_least_once - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once get :activation_question, :enterprise_code => '0123456789' @@ -417,7 +417,7 @@ class AccountControllerTest < ActionController::TestCase task = mock task.expects(:enterprise).returns(ent).at_least_once - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once get :activation_question, :enterprise_code => '0123456789' @@ -433,7 +433,7 @@ class AccountControllerTest < ActionController::TestCase task = mock task.expects(:enterprise).returns(ent).at_least_once - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once get :activation_question, :enterprise_code => '0123456789' @@ -446,7 +446,7 @@ class AccountControllerTest < ActionController::TestCase task = mock task.expects(:enterprise).returns(ent).never - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).never + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).never post :accept_terms, :enterprise_code => '0123456789', :answer => '1998' @@ -462,7 +462,7 @@ class AccountControllerTest < ActionController::TestCase task = mock task.expects(:enterprise).returns(ent).at_least_once - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once post :accept_terms, :enterprise_code => '0123456789', :answer => '1997' @@ -484,7 +484,7 @@ class AccountControllerTest < ActionController::TestCase ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false) ent.update_attribute(:foundation_year, 1998) task = EnterpriseActivation.create!(:enterprise => ent) - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once post :accept_terms, :enterprise_code => '0123456789', :answer => '1998' @@ -503,7 +503,7 @@ class AccountControllerTest < ActionController::TestCase task = mock task.expects(:enterprise).returns(ent).at_least_once - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once get :accept_terms, :enterprise_code => '0123456789', :answer => 1998 @@ -517,7 +517,7 @@ class AccountControllerTest < ActionController::TestCase ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false) ent.update_attribute(:foundation_year, 1998) task = EnterpriseActivation.create!(:enterprise => ent) - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).never + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).never post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true @@ -531,7 +531,7 @@ class AccountControllerTest < ActionController::TestCase login_as(p.identifier) task = EnterpriseActivation.create!(:enterprise => ent) - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => false ent.reload @@ -547,7 +547,7 @@ class AccountControllerTest < ActionController::TestCase login_as(p.identifier) task = EnterpriseActivation.create!(:enterprise => ent) - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true ent.reload @@ -566,7 +566,7 @@ class AccountControllerTest < ActionController::TestCase ent = fast_create(Enterprise, :name => 'test enterprise', :identifier => 'test_ent', :enabled => false) ent.update_attribute(:foundation_year, 1998) task = EnterpriseActivation.create!(:enterprise => ent) - EnterpriseActivation.expects(:find_by_code).with('0123456789').returns(task).at_least_once + EnterpriseActivation.expects(:from_code).with('0123456789').returns(task).at_least_once post :activate_enterprise, :enterprise_code => '0123456789', :answer => '1998', :terms_accepted => true diff --git a/test/unit/task_test.rb b/test/unit/task_test.rb index 00388e3..73ede84 100644 --- a/test/unit/task_test.rb +++ b/test/unit/task_test.rb @@ -131,7 +131,7 @@ class TaskTest < ActiveSupport::TestCase task.cancel - assert_nil Task.find_by_code(task.code) + assert_nil Task.from_code(task.code) end should 'be able to find active tasks' do @@ -139,7 +139,7 @@ class TaskTest < ActiveSupport::TestCase task.requestor = sample_user task.save! - assert_not_nil Task.find_by_code(task.code) + assert_not_nil Task.from_code(task.code) end should 'use 36-chars codes by default' do -- libgit2 0.21.2