Sha256: 8679fc1ced00fbaf6ff0009df36e8cc5089ae211ba14a413e3d4cf69562161cf

Contents?: true

Size: 1.59 KB

Versions: 9

Compression:

Stored size: 1.59 KB

Contents

module Scorpion
  module Rails
    module ActiveRecord

      # Adds dependency injection to ActiveRecord::Base model associations.
      module Association
        include Scorpion::Stinger

        # ============================================================================
        # @!group Attributes
        #

        include Scorpion::Method

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

        #
        # @!endgroup Attributes


        # Make sure we override the methods of child classes as well.
        def self.prepended( base )
          infect base
          super
        end

        # Propagate the module inheritance to all derived classes so that we can
        # always overlay our interception methods on the top-most overriden
        # method.
        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 interception methods on the klass.
        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

      end

    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
scorpion-ioc-1.0.10 lib/scorpion/rails/active_record/association.rb
scorpion-ioc-1.0.8 lib/scorpion/rails/active_record/association.rb
scorpion-ioc-1.0.7 lib/scorpion/rails/active_record/association.rb
scorpion-ioc-1.0.6 lib/scorpion/rails/active_record/association.rb
scorpion-ioc-1.0.4 lib/scorpion/rails/active_record/association.rb
scorpion-ioc-1.0.3 lib/scorpion/rails/active_record/association.rb
scorpion-ioc-1.0.2 lib/scorpion/rails/active_record/association.rb
scorpion-ioc-1.0.1 lib/scorpion/rails/active_record/association.rb
scorpion-ioc-1.0.0 lib/scorpion/rails/active_record/association.rb