From 9b78974bc9f0dad0242d057b69f543471af2b92d Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Tue, 16 May 2017 12:10:37 +0900 Subject: Fix association with extension issues This fixes the following issues. * `association_scope` doesn't include `default_scope`. Should use `scope` instead. * We can't use `method_missing` for customizing existing method. * We can't use `relation_delegate_class` for sharing extensions. Should extend per association. --- .../lib/active_record/associations/association.rb | 10 +++++++++ .../associations/association_scope.rb | 2 +- .../active_record/associations/collection_proxy.rb | 24 +++------------------- activerecord/lib/active_record/reflection.rb | 4 ++++ .../lib/active_record/relation/delegation.rb | 2 -- 5 files changed, 18 insertions(+), 24 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index 1cb2b2d7c6..e42162c740 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -133,6 +133,16 @@ module ActiveRecord AssociationRelation.create(klass, klass.arel_table, klass.predicate_builder, self).merge!(klass.all) end + def extensions + extensions = klass.all.extensions | reflection.extensions + + if scope = reflection.scope + extensions |= klass.unscoped.instance_exec(owner, &scope).extensions + end + + extensions + end + # Loads the \target if needed and returns it. # # This method is abstract in the sense that it relies on +find_target+, diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 120d75416c..1593b94f0c 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -24,7 +24,7 @@ module ActiveRecord alias_tracker = AliasTracker.create connection, association.klass.table_name, klass.type_caster chain_head, chain_tail = get_chain(reflection, association, alias_tracker) - scope.extending! Array(reflection.options[:extend]) + scope.extending! reflection.extensions add_constraints(scope, owner, reflection, chain_head, chain_tail) end diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 74a4d515c2..89cfa07be3 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -31,6 +31,9 @@ module ActiveRecord def initialize(klass, association) #:nodoc: @association = association super klass, klass.arel_table, klass.predicate_builder + + extensions = association.extensions + extend(*extensions) if extensions.any? end def target @@ -1121,19 +1124,6 @@ module ActiveRecord delegate(*delegate_methods, to: :scope) - module DelegateExtending # :nodoc: - private - def method_missing(method, *args, &block) - extending_values = association_scope.extending_values - if extending_values.any? && (extending_values - self.class.included_modules).any? - self.class.include(*extending_values) - public_send(method, *args, &block) - else - super - end - end - end - private def find_nth_with_limit(index, limit) @@ -1154,17 +1144,9 @@ module ActiveRecord @association.find_from_target? end - def association_scope - @association.association_scope - end - def exec_queries load_target end - - def respond_to_missing?(method, _) - association_scope.respond_to?(method) || super - end end end end diff --git a/activerecord/lib/active_record/reflection.rb b/activerecord/lib/active_record/reflection.rb index 65fdbc2fe4..0bfd59d7bd 100644 --- a/activerecord/lib/active_record/reflection.rb +++ b/activerecord/lib/active_record/reflection.rb @@ -581,6 +581,10 @@ module ActiveRecord seed + [self] end + def extensions + Array(options[:extend]) + end + protected def actual_source_reflection # FIXME: this is a horrible name diff --git a/activerecord/lib/active_record/relation/delegation.rb b/activerecord/lib/active_record/relation/delegation.rb index 257ae04ff4..8b4dd25689 100644 --- a/activerecord/lib/active_record/relation/delegation.rb +++ b/activerecord/lib/active_record/relation/delegation.rb @@ -25,8 +25,6 @@ module ActiveRecord def inherited(child_class) child_class.initialize_relation_delegate_cache - delegate = child_class.relation_delegate_class(ActiveRecord::Associations::CollectionProxy) - delegate.include ActiveRecord::Associations::CollectionProxy::DelegateExtending super end end -- cgit v1.2.3 From c45c9cfb011fce54c319555c8852d915ff2ef97a Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Wed, 17 May 2017 00:40:26 +0900 Subject: Cache the association proxy object Some third party modules expects that association returns same proxy object each time (e.g. for stubbing collection methods: https://github.com/rspec/rspec-rails/issues/1817). So I decided that cache the proxy object and reset scope in the proxy object each time. Related context: https://github.com/rails/rails/commit/c86a32d7451c5d901620ac58630460915292f88b#commitcomment-2784312 --- .../lib/active_record/associations/collection_association.rb | 3 ++- activerecord/lib/active_record/associations/collection_proxy.rb | 9 ++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 62c944fce3..edc53e2517 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -30,7 +30,8 @@ module ActiveRecord reload end - CollectionProxy.create(klass, self) + @proxy ||= CollectionProxy.create(klass, self) + @proxy.reset_scope end # Implements the writer method, e.g. foo.items= for Foo.has_many :items diff --git a/activerecord/lib/active_record/associations/collection_proxy.rb b/activerecord/lib/active_record/associations/collection_proxy.rb index 89cfa07be3..8cdb508c43 100644 --- a/activerecord/lib/active_record/associations/collection_proxy.rb +++ b/activerecord/lib/active_record/associations/collection_proxy.rb @@ -1087,9 +1087,8 @@ module ActiveRecord # person.pets(true) # fetches pets from the database # # => [#] def reload - @scope = nil proxy_association.reload - self + reset_scope end # Unloads the association. Returns +self+. @@ -1109,9 +1108,13 @@ module ActiveRecord # person.pets # fetches pets from the database # # => [#] def reset - @scope = nil proxy_association.reset proxy_association.reset_scope + reset_scope + end + + def reset_scope # :nodoc: + @scope = nil self end -- cgit v1.2.3 From d1249c1a91df614ceb10167155b0265b9578835e Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Sun, 28 May 2017 10:12:18 +0900 Subject: Refactor `default_scoped` to avoid creating extra relation and merging --- activerecord/lib/active_record/scoping/named.rb | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index 27cdf8cb7e..d03c23fe5c 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -30,13 +30,8 @@ module ActiveRecord end def default_scoped # :nodoc: - scope = build_default_scope - - if scope - relation.spawn.merge!(scope) - else - relation - end + scope = relation + build_default_scope(scope) || scope end # Adds a class method for retrieving and querying objects. -- cgit v1.2.3 From 5632f73042bc543d59e6f3e913e0d2cd44b54a65 Mon Sep 17 00:00:00 2001 From: Ryuta Kamizono Date: Tue, 30 May 2017 04:37:25 +0900 Subject: Extract `default_extensions` to avoid `klass.all` As @matthewd's suggestion, if `klass` has no default scope, it will more faster. --- activerecord/lib/active_record/associations/association.rb | 2 +- activerecord/lib/active_record/scoping/named.rb | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/associations/association.rb b/activerecord/lib/active_record/associations/association.rb index e42162c740..0f330af2f6 100644 --- a/activerecord/lib/active_record/associations/association.rb +++ b/activerecord/lib/active_record/associations/association.rb @@ -134,7 +134,7 @@ module ActiveRecord end def extensions - extensions = klass.all.extensions | reflection.extensions + extensions = klass.default_extensions | reflection.extensions if scope = reflection.scope extensions |= klass.unscoped.instance_exec(owner, &scope).extensions diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb index d03c23fe5c..93c6f1d87e 100644 --- a/activerecord/lib/active_record/scoping/named.rb +++ b/activerecord/lib/active_record/scoping/named.rb @@ -34,6 +34,14 @@ module ActiveRecord build_default_scope(scope) || scope end + def default_extensions # :nodoc: + if scope = current_scope || build_default_scope + scope.extensions + else + [] + end + end + # Adds a class method for retrieving and querying objects. # The method is intended to return an ActiveRecord::Relation # object, which is composable with other scopes. -- cgit v1.2.3