diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-08-03 06:35:20 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-08-04 02:27:50 +0900 |
commit | 510428ff64ce19340913145140986119c64c8b7d (patch) | |
tree | fad3875ad35367d401ab24c15af054e1f3149926 /activerecord/lib/active_record/statement_cache.rb | |
parent | c6dcee4770b06ee3cd88a63cceb3988806ffd383 (diff) | |
download | rails-510428ff64ce19340913145140986119c64c8b7d.tar.gz rails-510428ff64ce19340913145140986119c64c8b7d.tar.bz2 rails-510428ff64ce19340913145140986119c64c8b7d.zip |
Passing `klass` to `StatementCache.new`
Actually `StatementCache#execute` is always passed the same klass that
the owner klass of the connection when the statement cache is created.
So passing `klass` to `StatementCache.new` will make more DRY.
Diffstat (limited to 'activerecord/lib/active_record/statement_cache.rb')
-rw-r--r-- | activerecord/lib/active_record/statement_cache.rb | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/statement_cache.rb b/activerecord/lib/active_record/statement_cache.rb index 2af7d00246..59acd63a0f 100644 --- a/activerecord/lib/active_record/statement_cache.rb +++ b/activerecord/lib/active_record/statement_cache.rb @@ -11,7 +11,7 @@ module ActiveRecord # The cached statement is executed by using the # {connection.execute}[rdoc-ref:ConnectionAdapters::DatabaseStatements#execute] method: # - # cache.execute([], Book, Book.connection) + # cache.execute([], Book.connection) # # The relation returned by the block is cached, and for each # {execute}[rdoc-ref:ConnectionAdapters::DatabaseStatements#execute] @@ -26,7 +26,7 @@ module ActiveRecord # # And pass the bind values as the first argument of +execute+ call. # - # cache.execute(["my book"], Book, Book.connection) + # cache.execute(["my book"], Book.connection) class StatementCache # :nodoc: class Substitute; end # :nodoc: @@ -87,21 +87,20 @@ module ActiveRecord end end - attr_reader :bind_map, :query_builder - def self.create(connection, block = Proc.new) relation = block.call Params.new query_builder, binds = connection.cacheable_query(self, relation.arel) bind_map = BindMap.new(binds) - new query_builder, bind_map + new(query_builder, bind_map, relation.klass) end - def initialize(query_builder, bind_map) + def initialize(query_builder, bind_map, klass) @query_builder = query_builder - @bind_map = bind_map + @bind_map = bind_map + @klass = klass end - def execute(params, klass, connection, &block) + def execute(params, connection, &block) bind_values = bind_map.bind params sql = query_builder.sql_for bind_values, connection @@ -114,5 +113,9 @@ module ActiveRecord when NilClass, Array, Range, Hash, Relation, Base then true end end + + protected + + attr_reader :query_builder, :bind_map, :klass end end |