mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Move delegation reponsibilities of Relation to a module. Also precompile method missing calls for rofscale.
This commit is contained in:
parent
1e8b751813
commit
b17bc58c76
3 changed files with 44 additions and 22 deletions
|
@ -51,6 +51,7 @@ module ActiveRecord
|
|||
autoload :SpawnMethods
|
||||
autoload :Batches
|
||||
autoload :Explain
|
||||
autoload :Delegation
|
||||
end
|
||||
|
||||
autoload :Base
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
require 'active_support/core_ext/object/blank'
|
||||
require 'active_support/core_ext/module/delegation'
|
||||
|
||||
module ActiveRecord
|
||||
# = Active Record Relation
|
||||
|
@ -10,13 +9,7 @@ module ActiveRecord
|
|||
MULTI_VALUE_METHODS = [:select, :group, :order, :joins, :where, :having, :bind]
|
||||
SINGLE_VALUE_METHODS = [:limit, :offset, :lock, :readonly, :from, :reorder, :reverse_order, :uniq]
|
||||
|
||||
include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain
|
||||
|
||||
# These are explicitly delegated to improve performance (avoids method_missing)
|
||||
delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to_ary, :to => :to_a
|
||||
delegate :ast, :engine, :to => :arel
|
||||
delegate :table_name, :quoted_table_name, :primary_key, :quoted_primary_key,
|
||||
:connection, :columns_hash, :auto_explain_threshold_in_seconds, :to => :klass
|
||||
include FinderMethods, Calculations, SpawnMethods, QueryMethods, Batches, Explain, Delegation
|
||||
|
||||
attr_reader :table, :klass, :loaded
|
||||
attr_accessor :extensions, :default_scoped
|
||||
|
@ -519,20 +512,6 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def method_missing(method, *args, &block)
|
||||
if Array.method_defined?(method)
|
||||
to_a.send(method, *args, &block)
|
||||
elsif @klass.respond_to?(method)
|
||||
scoping { @klass.send(method, *args, &block) }
|
||||
elsif arel.respond_to?(method)
|
||||
arel.send(method, *args, &block)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def references_eager_loaded_tables?
|
||||
|
|
42
activerecord/lib/active_record/relation/delegation.rb
Normal file
42
activerecord/lib/active_record/relation/delegation.rb
Normal file
|
@ -0,0 +1,42 @@
|
|||
require 'active_support/core_ext/module/delegation'
|
||||
|
||||
module ActiveRecord
|
||||
module Delegation
|
||||
# These are explicitly delegated to improve performance (avoids method_missing)
|
||||
delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to_ary, :to => :to_a
|
||||
delegate :ast, :engine, :to => :arel
|
||||
delegate :table_name, :quoted_table_name, :primary_key, :quoted_primary_key,
|
||||
:connection, :columns_hash, :auto_explain_threshold_in_seconds, :to => :klass
|
||||
|
||||
def self.delegate_to_scoped_klass(method)
|
||||
if method.to_s =~ /[a-z]\w*!?/
|
||||
module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def #{method}(*args, &block)
|
||||
scoping { @klass.#{method}(*args, &block) }
|
||||
end
|
||||
RUBY
|
||||
else
|
||||
module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
||||
def #{method}(*args, &block)
|
||||
scoping { @klass.send(#{method.inspect}, *args, &block) }
|
||||
end
|
||||
RUBY
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def method_missing(method, *args, &block)
|
||||
if Array.method_defined?(method)
|
||||
to_a.send(method, *args, &block)
|
||||
elsif @klass.respond_to?(method)
|
||||
::ActiveRecord::Delegation.delegate_to_scoped_klass(method)
|
||||
scoping { @klass.send(method, *args, &block) }
|
||||
elsif arel.respond_to?(method)
|
||||
arel.send(method, *args, &block)
|
||||
else
|
||||
super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue