Module: ActiveRecord::Collections::Delegation

Included in:
ActiveRecord::Collection
Defined in:
lib/active_record/collections/delegation.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/active_record/collections/delegation.rb', line 20

def method_missing(meth, *args)
  if relation.respond_to?(meth)
    return call_on_relation(meth, *args)
  end

  if records_respond_to?(meth)
    return call_on_records(meth, *args)
  end

  super
end

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/active_record/collections/delegation.rb', line 4

def self.included(base)
  base.send :extend, ClassMethods
end

Instance Method Details

#on_records(&block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/active_record/collections/delegation.rb', line 52

def on_records(&block)
  collection = dup
  collection.instance_eval do
    def self.method_missing(meth, *args)
      call_on_records(meth, *args)
    end
    def self.respond_to_missing?(meth, include_private=false)
      records_respond_to?(meth, include_private)
    end
  end
  return collection.instance_eval(&block) if block_given?
  collection
end

#on_relation(&block) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/active_record/collections/delegation.rb', line 38

def on_relation(&block)
  collection = dup
  collection.instance_eval do
    def self.method_missing(meth, *args)
      call_on_relation(meth, *args)
    end
    def self.respond_to_missing?(meth, include_private=false)
      relation.respond_to?(meth, include_private)
    end
  end
  return collection.instance_eval(&block) if block_given?
  collection
end

#respond_to_missing?(meth, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/active_record/collections/delegation.rb', line 32

def respond_to_missing?(meth, include_private=false)
  records_respond_to?(meth, include_private) ||
  relation.respond_to?(meth, include_private) ||
  super
end