Sha256: 1b94fb3959a783fbde9ac245935cd2a6b82e711366ed56c84db4992cd4d719c5

Contents?: true

Size: 1.67 KB

Versions: 85

Compression:

Stored size: 1.67 KB

Contents

require 'active_support/core_ext/module/delegation'

module ActiveRecord
  module Delegation
    # Set up common delegations for performance (avoids method_missing)
    delegate :to_xml, :to_yaml, :length, :collect, :map, :each, :all?, :include?, :to_ary, :to => :to_a
    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[a-zA-Z_]\w*[!?]?\z/
        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

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

    protected

    def method_missing(method, *args, &block)
      if @klass.respond_to?(method)
        ::ActiveRecord::Delegation.delegate_to_scoped_klass(method)
        scoping { @klass.send(method, *args, &block) }
      elsif Array.method_defined?(method)
        ::ActiveRecord::Delegation.delegate method, :to => :to_a
        to_a.send(method, *args, &block)
      elsif arel.respond_to?(method)
        ::ActiveRecord::Delegation.delegate method, :to => :arel
        arel.send(method, *args, &block)
      else
        super
      end
    end
  end
end

Version data entries

85 entries across 71 versions & 13 rubygems

Version Path
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/activerecord-3.2.22.5/lib/active_record/relation/delegation.rb
activerecord-3.2.22.5 lib/active_record/relation/delegation.rb
activerecord-3.2.22.4 lib/active_record/relation/delegation.rb
activerecord-3.2.22.3 lib/active_record/relation/delegation.rb
activerecord-3.2.22.2 lib/active_record/relation/delegation.rb
activerecord-3.2.22.1 lib/active_record/relation/delegation.rb
active_mailer-0.0.10 test/fixtures/dummyapp_rails_3.2/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/relation/delegation.rb
activerecord-3.2.22 lib/active_record/relation/delegation.rb
judge-2.0.5 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.12/lib/active_record/relation/delegation.rb
activerecord-3.2.21 lib/active_record/relation/delegation.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/relation/delegation.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/relation/delegation.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/2.1.0/gems/activerecord-3.2.18/lib/active_record/relation/delegation.rb
apl-library-0.0.90 vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.18/lib/active_record/relation/delegation.rb
apl-library-0.0.90 vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/relation/delegation.rb
apl-library-0.0.90 vendor/bundle/ruby/2.1.0/gems/apl-library-0.0.90/vendor/bundle/ruby/1.8/gems/activerecord-3.2.18/lib/active_record/relation/delegation.rb
activerecord-3.2.20 lib/active_record/relation/delegation.rb
activerecord-3.2.19 lib/active_record/relation/delegation.rb
activerecord-3.2.18 lib/active_record/relation/delegation.rb
activerecord-3.2.17 lib/active_record/relation/delegation.rb