diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2014-04-10 17:24:10 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2014-04-10 17:24:10 -0700 |
commit | dbc8c0ee36ddda7c27a375c78bbdb989fd30c298 (patch) | |
tree | 04e54b068e065c39cb6de472606675341b53d545 /activerecord | |
parent | 249fd686fb86c89463d791f44114fd829cb05bd2 (diff) | |
download | rails-dbc8c0ee36ddda7c27a375c78bbdb989fd30c298.tar.gz rails-dbc8c0ee36ddda7c27a375c78bbdb989fd30c298.tar.bz2 rails-dbc8c0ee36ddda7c27a375c78bbdb989fd30c298.zip |
decouple the factory method from the constructing model
The factory method only requires the constructing model to get the
connection object. Since the model is available when calling the factory
method, we can just pass the appropriate connection in.
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/core.rb | 4 | ||||
-rw-r--r-- | activerecord/lib/active_record/statement_cache.rb | 4 | ||||
-rw-r--r-- | activerecord/test/cases/statement_cache_test.rb | 10 |
3 files changed, 8 insertions, 10 deletions
diff --git a/activerecord/lib/active_record/core.rb b/activerecord/lib/active_record/core.rb index 1b7f579aa3..269675192a 100644 --- a/activerecord/lib/active_record/core.rb +++ b/activerecord/lib/active_record/core.rb @@ -135,7 +135,7 @@ module ActiveRecord key = primary_key s = find_by_statement_cache[key] || find_by_statement_cache.synchronize { - find_by_statement_cache[key] ||= StatementCache.create { |params| + find_by_statement_cache[key] ||= StatementCache.create(connection) { |params| where(key => params[key]).limit(1) } } @@ -159,7 +159,7 @@ module ActiveRecord klass = self s = find_by_statement_cache[key] || find_by_statement_cache.synchronize { - find_by_statement_cache[key] ||= StatementCache.create { |params| + find_by_statement_cache[key] ||= StatementCache.create(connection) { |params| wheres = key.each_with_object({}) { |param,o| o[param] = params[param] } diff --git a/activerecord/lib/active_record/statement_cache.rb b/activerecord/lib/active_record/statement_cache.rb index fbba95d78f..d6c48125f4 100644 --- a/activerecord/lib/active_record/statement_cache.rb +++ b/activerecord/lib/active_record/statement_cache.rb @@ -76,11 +76,9 @@ module ActiveRecord attr_reader :bind_map, :query_builder - def self.create(block = Proc.new) + def self.create(connection, block = Proc.new) relation = block.call Params.new bind_map = BindMap.new relation.bind_values - klass = relation.klass - connection = klass.connection query_builder = connection.cacheable_query relation.arel new query_builder, bind_map end diff --git a/activerecord/test/cases/statement_cache_test.rb b/activerecord/test/cases/statement_cache_test.rb index 0455764f6c..96a899a08b 100644 --- a/activerecord/test/cases/statement_cache_test.rb +++ b/activerecord/test/cases/statement_cache_test.rb @@ -15,7 +15,7 @@ module ActiveRecord Book.create(name: "my book") Book.create(name: "my other book") - cache = StatementCache.create do |params| + cache = StatementCache.create(Book.connection) do |params| Book.where(:name => params[:name]) end @@ -30,7 +30,7 @@ module ActiveRecord b1 = Book.create(name: "my book") b2 = Book.create(name: "my other book") - cache = StatementCache.create do |params| + cache = StatementCache.create(Book.connection) do |params| Book.where(id: params[:id]) end @@ -53,7 +53,7 @@ module ActiveRecord #End def test_statement_cache_with_simple_statement - cache = ActiveRecord::StatementCache.create do |params| + cache = ActiveRecord::StatementCache.create(Book.connection) do |params| Book.where(name: "my book").where("author_id > 3") end @@ -64,7 +64,7 @@ module ActiveRecord end def test_statement_cache_with_complex_statement - cache = ActiveRecord::StatementCache.create do |params| + cache = ActiveRecord::StatementCache.create(Book.connection) do |params| Liquid.joins(:molecules => :electrons).where('molecules.name' => 'dioxane', 'electrons.name' => 'lepton') end @@ -77,7 +77,7 @@ module ActiveRecord end def test_statement_cache_values_differ - cache = ActiveRecord::StatementCache.create do |params| + cache = ActiveRecord::StatementCache.create(Book.connection) do |params| Book.where(name: "my book") end |