Commit b41cf2b2ed8cd970a2289a204a50a5dd08fc0b26

Authored by Rodrigo Souto
1 parent f462e462

Save enterprise updates products position with a delayed job

(ActionItem2307)
app/models/enterprise.rb
... ... @@ -76,7 +76,11 @@ class Enterprise < Organization
76 76 end
77 77  
78 78 after_save do |e|
79   - e.products.each{ |p| p.enterprise_updated(e) }
  79 + e.delay.update_products_position
  80 + end
  81 +
  82 + def update_products_position
  83 + products.each{ |p| p.enterprise_updated(self) }
80 84 end
81 85  
82 86 def closed?
... ...
test/integration/performance_test.rb
... ... @@ -51,6 +51,26 @@ class PerformanceTest < ActionController::IntegrationTest
51 51 assert a1 > a2*2, "#{a1} should be larger than #{a2} by at least a factor of 2"
52 52 end
53 53  
  54 + should 'not have a linear increase in time to save enterprise due to amount of products' do
  55 + enterprise0 = Enterprise.create!(:name => 'Enterprise 0', :identifier => 'enterprise0')
  56 + enterprise1 = Enterprise.create!(:name => 'Enterprise 1', :identifier => 'enterprise1')
  57 + enterprise2 = Enterprise.create!(:name => 'Enterprise 2', :identifier => 'enterprise2')
  58 +
  59 + create_products(enterprise1,25)
  60 + create_products(enterprise2,50)
  61 +
  62 + enterprise0.reload
  63 + enterprise1.reload
  64 + enterprise2.reload
  65 +
  66 + time0 = (Benchmark.measure { 10.times { enterprise0.save! } }).total
  67 + time1 = (Benchmark.measure { 10.times { enterprise1.save! } }).total
  68 + time2 = (Benchmark.measure { 10.times { enterprise2.save! } }).total
  69 +
  70 + assert (time1 - time0) < time0*0.5
  71 + assert (time2 - time0) < time0*0.5
  72 + end
  73 +
54 74 protected
55 75  
56 76 def create_profile(name)
... ... @@ -68,5 +88,13 @@ class PerformanceTest &lt; ActionController::IntegrationTest
68 88 end
69 89 end
70 90  
  91 + def create_products(enterprise,n)
  92 + number = Product.all.count
  93 + pc = ProductCategory.create!(:name => "Any Category #{n}", :environment => Environment.default)
  94 + n.times do |i|
  95 + Product.create!(:enterprise => enterprise, :product_category => pc, :name => "Product #{i+number+1}")
  96 + end
  97 + end
  98 +
71 99 end
72 100  
... ...
test/unit/product_test.rb
... ... @@ -109,7 +109,7 @@ class ProductTest &lt; Test::Unit::TestCase
109 109 prod = ent.products.create!(:name => 'test product', :product_category => @product_category)
110 110  
111 111 ent.lat = 45.0; ent.lng = 45.0; ent.save!
112   -
  112 + process_delayed_job_queue
113 113 prod.reload
114 114  
115 115 assert_in_delta 45.0, prod.lat, 0.0001
... ...