aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/relation/delegation.rb
diff options
context:
space:
mode:
authorJosé Valim <jose.valim@gmail.com>2011-12-15 20:46:30 +0100
committerJosé Valim <jose.valim@gmail.com>2011-12-15 20:47:26 +0100
commitb17bc58c76c580911e18f2dd90a17aaae192a2a3 (patch)
treec177027523b9a6fd50b2327c94e290a063a9f588 /activerecord/lib/active_record/relation/delegation.rb
parent1e8b75181306ada87b1a4b05d1551348dd916f91 (diff)
downloadrails-b17bc58c76c580911e18f2dd90a17aaae192a2a3.tar.gz
rails-b17bc58c76c580911e18f2dd90a17aaae192a2a3.tar.bz2
rails-b17bc58c76c580911e18f2dd90a17aaae192a2a3.zip
Move delegation reponsibilities of Relation to a module. Also precompile method missing calls for rofscale.
Diffstat (limited to 'activerecord/lib/active_record/relation/delegation.rb')
-rw-r--r--activerecord/lib/active_record/relation/delegation.rb42
1 files changed, 42 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/delegation.rb b/activerecord/lib/active_record/relation/delegation.rb
new file mode 100644
index 0000000000..c1fcd48a8e
--- /dev/null
+++ b/activerecord/lib/active_record/relation/delegation.rb
@@ -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 \ No newline at end of file