Commit 06162adb5060969054b0397fe18b1fb2c8e4eb2d

Authored by Victor Costa
Committed by Marcos Pereira
1 parent 723a6061

Improve register performance

- Send activation code with delayed job
- Avoid unecessary user save after update person

Signed-off-by: Gustavo Jaruga <darksshades@gmail.com>
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com>
app/models/person.rb
... ... @@ -328,7 +328,7 @@ class Person &lt; Profile
328 328 end
329 329  
330 330 after_update do |person|
331   - person.user.save!
  331 + person.user.save! unless person.user.changes.blank?
332 332 end
333 333  
334 334 def is_admin?(environment = nil)
... ...
app/models/profile.rb
... ... @@ -692,15 +692,15 @@ private :generate_url, :url_options
692 692 after_create :insert_default_article_set
693 693 def insert_default_article_set
694 694 if template
695   - copy_articles_from template
  695 + self.save! if copy_articles_from template
696 696 else
697 697 default_set_of_articles.each do |article|
698 698 article.profile = self
699 699 article.advertise = false
700 700 article.save!
701 701 end
  702 + self.save!
702 703 end
703   - self.save!
704 704 end
705 705  
706 706 # Override this method in subclasses of Profile to create a default article
... ... @@ -721,10 +721,12 @@ private :generate_url, :url_options
721 721 end
722 722  
723 723 def copy_articles_from other
  724 + return false if other.top_level_articles.empty?
724 725 other.top_level_articles.each do |a|
725 726 copy_article_tree a
726 727 end
727 728 self.articles.reload
  729 + true
728 730 end
729 731  
730 732 def copy_article_tree(article, parent=nil)
... ...
app/models/user.rb
... ... @@ -445,7 +445,7 @@ class User &lt; ActiveRecord::Base
445 445  
446 446 def deliver_activation_code
447 447 return if person.is_template?
448   - UserMailer.activation_code(self).deliver unless self.activation_code.blank?
  448 + Delayed::Job.enqueue(UserMailer::Job.new(self, :activation_code)) unless self.activation_code.blank?
449 449 end
450 450  
451 451 def delay_activation_check
... ...
features/signup.feature
... ... @@ -16,6 +16,7 @@ Feature: signup
16 16 | Full name | José da Silva |
17 17 And wait for the captcha signup time
18 18 And I press "Create my account"
  19 + And there are no pending jobs
19 20 Then I should receive an e-mail on josesilva@example.com
20 21 When I go to login page
21 22 And I fill in "Username" with "josesilva"
... ...
test/functional/search_controller_test.rb
... ... @@ -27,6 +27,7 @@ class SearchControllerTest &lt; ActionController::TestCase
27 27 # By pass user validation on person creation
28 28 user = mock()
29 29 user.stubs(:id).returns(1)
  30 + user.stubs(:changes).returns(nil)
30 31 user.stubs(:valid?).returns(true)
31 32 user.stubs(:email).returns('some@test.com')
32 33 user.stubs(:save!).returns(true)
... ...
test/integration/signup_test.rb
... ... @@ -45,7 +45,7 @@ class SignupTest &lt; ActionDispatch::IntegrationTest
45 45 assert_redirected_to controller: 'home', action: 'welcome'
46 46  
47 47 assert_equal count + 1, User.count
48   - assert_equal mail_count + 1, ActionMailer::Base.deliveries.count
  48 + assert_includes Delayed::Job.all.map{|d|d.name}, 'UserMailer::Job'
49 49  
50 50 end
51 51  
... ...
test/unit/person_test.rb
... ... @@ -1937,11 +1937,18 @@ class PersonTest &lt; ActiveSupport::TestCase
1937 1937  
1938 1938 should 'a person follows many articles' do
1939 1939 person = create_user('article_follower').person
1940   -
  1940 +
1941 1941 1.upto(10).map do |n|
1942 1942 person.following_articles << fast_create(Article, :profile_id => fast_create(Person))
1943 1943 end
1944 1944 assert_equal 10, person.following_articles.count
1945 1945 end
1946 1946  
  1947 + should 'not save user after an update on person and user is not touched' do
  1948 + user = create_user('testuser')
  1949 + person = user.person
  1950 + person.user.expects(:save!).never
  1951 + person.save!
  1952 + end
  1953 +
1947 1954 end
... ...