diff options
author | Willian Gustavo Veiga <beberveiga@gmail.com> | 2018-10-11 07:44:27 -0300 |
---|---|---|
committer | Willian Gustavo Veiga <beberveiga@gmail.com> | 2018-10-11 07:44:27 -0300 |
commit | 99bd626885b72acd44861727918ee107a649e2b4 (patch) | |
tree | 6d59fcdb226a65d9eb63f8bad2b2df7b09b92a60 /activerecord/lib/active_record/relation | |
parent | 4b60e34f58c0dabe18a793038fc7775d044016f6 (diff) | |
parent | 6a278c6e9a05b0d901e482458cfc2e3df587d522 (diff) | |
download | rails-99bd626885b72acd44861727918ee107a649e2b4.tar.gz rails-99bd626885b72acd44861727918ee107a649e2b4.tar.bz2 rails-99bd626885b72acd44861727918ee107a649e2b4.zip |
Merge branch 'master' into feature/reselect-method
Diffstat (limited to 'activerecord/lib/active_record/relation')
-rw-r--r-- | activerecord/lib/active_record/relation/delegation.rb | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/relation/delegation.rb b/activerecord/lib/active_record/relation/delegation.rb index 8f657840f5..383dc1bf4b 100644 --- a/activerecord/lib/active_record/relation/delegation.rb +++ b/activerecord/lib/active_record/relation/delegation.rb @@ -17,6 +17,7 @@ module ActiveRecord delegate = Class.new(klass) { include ClassSpecificRelation } + include_relation_methods(delegate) mangled_name = klass.name.gsub("::", "_") const_set mangled_name, delegate private_constant mangled_name @@ -29,6 +30,35 @@ module ActiveRecord child_class.initialize_relation_delegate_cache super end + + protected + def include_relation_methods(delegate) + superclass.include_relation_methods(delegate) unless base_class? + delegate.include generated_relation_methods + end + + private + def generated_relation_methods + @generated_relation_methods ||= Module.new.tap do |mod| + mod_name = "GeneratedRelationMethods" + const_set mod_name, mod + private_constant mod_name + end + end + + def generate_relation_method(method) + if /\A[a-zA-Z_]\w*[!?]?\z/.match?(method) + generated_relation_methods.module_eval <<-RUBY, __FILE__, __LINE__ + 1 + def #{method}(*args, &block) + scoping { klass.#{method}(*args, &block) } + end + RUBY + else + generated_relation_methods.send(:define_method, method) do |*args, &block| + scoping { klass.public_send(method, *args, &block) } + end + end + end end extend ActiveSupport::Concern |