Module: ActiveRecord::Delegation

Extended by:
ActiveSupport::Concern
Included in:
Relation
Defined in:
lib/active_record/relation/delegation.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods, ClassSpecificRelation, DelegateCache

Constant Summary collapse

BLACKLISTED_ARRAY_METHODS =

This module creates compiled delegation methods dynamically at runtime, which makes subsequent calls to that method faster by avoiding method_missing. The delegations may vary depending on the klass of a relation, so we create a subclass of Relation for each different klass, and the delegations are compiled into that subclass only.

[
  :compact!, :flatten!, :reject!, :reverse!, :rotate!, :map!,
  :shuffle!, :slice!, :sort!, :sort_by!, :delete_if,
  :keep_if, :pop, :shift, :delete_at, :compact, :select!
].to_set

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/active_record/relation/delegation.rb', line 128

def method_missing(method, *args, &block)
  if @klass.respond_to?(method)
    scoping { @klass.public_send(method, *args, &block) }
  elsif array_delegable?(method)
    to_a.public_send(method, *args, &block)
  elsif arel.respond_to?(method)
    arel.public_send(method, *args, &block)
  else
    super
  end
end

Instance Method Details

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
119
120
# File 'lib/active_record/relation/delegation.rb', line 116

def respond_to?(method, include_private = false)
  super || @klass.respond_to?(method, include_private) ||
    array_delegable?(method) ||
    arel.respond_to?(method, include_private)
end