Module: Scorpion::Rails::ActiveRecord::Association

Includes:
Method, Stinger
Defined in:
lib/scorpion/rails/active_record/association.rb

Overview

Adds dependency injection to ActiveRecord::Base model associations.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Method

#scorpion_hunt

Methods included from Stinger

#sting!, wrap

Class Method Details

.infect(klass) ⇒ Object

Propagate the module inheritance to all derived classes so that we can always overlay our interception methods on the top-most overriden method.



32
33
34
35
36
37
38
39
40
41
# File 'lib/scorpion/rails/active_record/association.rb', line 32

def self.infect( klass )
  klass.class_exec do
    def self.inherited( from )
      Scorpion::Rails::ActiveRecord::Association.infect( from )

      super
    end
  end
  overlay( klass )
end

.overlay(klass) ⇒ Object

Overlay interception methods on the klass.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/scorpion/rails/active_record/association.rb', line 44

def self.overlay( klass )
  [ :load_target, :target, :reader, :writer, :scope ].each do |method|
    next unless klass.instance_methods.include? method

    mod = Module.new do
      module_eval <<-RUBY, __FILE__, __LINE__ + 1
        def #{ method }( *args, &block )
          sting! super
        end
      RUBY
    end

    klass.prepend mod
  end
end

.prepended(base) ⇒ Object

Make sure we override the methods of child classes as well.



24
25
26
27
# File 'lib/scorpion/rails/active_record/association.rb', line 24

def self.prepended( base )
  infect base
  super
end

Instance Method Details

#scorpion(scope = nil) ⇒ Object



15
16
17
# File 'lib/scorpion/rails/active_record/association.rb', line 15

def scorpion( scope = nil )
  super || owner.scorpion( scope )
end