Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Revert "Removing cached_find"

This reverts commit 4e08e1b.

Conflicts:

	lib/a_b/model/variant.rb
  • Loading branch information...
commit 93b95cbfb1360eb990b9cbfe3cf2c7d5f2f7e2a7 1 parent 8969a90
Winton Welsh authored November 08, 2010
9  db/migrate/015_remove_variants_lock.rb
... ...
@@ -0,0 +1,9 @@
  1
+class RemoveVariantsLock < ActiveRecord::Migration
  2
+  def self.up
  3
+    remove_column :variants, :lock_version
  4
+  end
  5
+
  6
+  def self.down
  7
+    add_column :variants, :lock_version, :integer, :default => 0
  8
+  end
  9
+end
10  lib/a_b/model/cached_find.rb
... ...
@@ -0,0 +1,10 @@
  1
+require 'pp'
  2
+
  3
+module CachedFind
  4
+  
  5
+  def cached_find(*args)
  6
+    $cached_find ||= {}
  7
+    cache = $cached_find[self.to_s.intern] ||= {}
  8
+    cache[args.pretty_inspect] ||= self.find(*args)
  9
+  end
  10
+end
2  lib/a_b/model/env.rb
... ...
@@ -1,5 +1,7 @@
1 1
 class Env < ActiveRecord::Base
2 2
   
  3
+  extend CachedFind
  4
+  
3 5
   belongs_to :site
4 6
   belongs_to :user
5 7
   
8  lib/a_b/model/variant.rb
... ...
@@ -1,5 +1,7 @@
1 1
 class Variant < ActiveRecord::Base
2 2
   
  3
+  extend CachedFind
  4
+  
3 5
   belongs_to :category
4 6
   belongs_to :site
5 7
   belongs_to :test, :class_name => 'ABTest', :foreign_key => 'test_id'
@@ -42,9 +44,11 @@ def self.record(options)
42 44
     ids = data['c'] + data['v']
43 45
     ids = ids.compact.uniq
44 46
     
45  
-    variants = Variant.find_all_by_id(ids, :include => :site)
  47
+    variants = ids.collect do |id|
  48
+      Variant.cached_find(id, :include => :site)
  49
+    end
46 50
     
47  
-    env = Env.find(:first, :conditions => {
  51
+    env = Env.cached_find(:first, :conditions => {
48 52
       :name => env,
49 53
       :user_id => variants[0].user_id
50 54
     })
1  spec/a_b/model/env_spec.rb
@@ -3,6 +3,7 @@
3 3
 describe Env do
4 4
   
5 5
   before(:each) do
  6
+    $cached_find = nil
6 7
     teardown_tests
7 8
     setup_tests
8 9
   end
1  spec/a_b/model/variant_spec.rb
@@ -3,6 +3,7 @@
3 3
 describe Variant do
4 4
   
5 5
   before(:each) do
  6
+    $cached_find = nil
6 7
     teardown_tests
7 8
     setup_tests
8 9
   end

0 notes on commit 93b95cb

Please sign in to comment.
Something went wrong with that request. Please try again.