diff options
author | Jon Leighton <j@jonathanleighton.com> | 2012-07-13 21:20:12 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2012-07-13 21:20:26 +0100 |
commit | 7fa9cb58c6e3b9311f0306173c48874ace7a6b04 (patch) | |
tree | 5fccd177bb0f63279e4f6ced60c1f4ac8b5f28ff /activerecord/lib/active_record | |
parent | 8baaa452fd8b3688719573977f05b89a9f5b1e6d (diff) | |
download | rails-7fa9cb58c6e3b9311f0306173c48874ace7a6b04.tar.gz rails-7fa9cb58c6e3b9311f0306173c48874ace7a6b04.tar.bz2 rails-7fa9cb58c6e3b9311f0306173c48874ace7a6b04.zip |
fix association :extend option
Diffstat (limited to 'activerecord/lib/active_record')
4 files changed, 20 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/associations/association_scope.rb b/activerecord/lib/active_record/associations/association_scope.rb index 1c06fb2681..1303822868 100644 --- a/activerecord/lib/active_record/associations/association_scope.rb +++ b/activerecord/lib/active_record/associations/association_scope.rb @@ -15,9 +15,7 @@ module ActiveRecord def scope scope = klass.unscoped - scope.extending!(*Array(options[:extend])) scope.merge! eval_scope(klass, reflection.scope) if reflection.scope - add_constraints(scope) end @@ -120,10 +118,8 @@ module ActiveRecord end def eval_scope(klass, scope) - return scope if scope.is_a?(Relation) - - if scope.arity == 0 - klass.unscoped.instance_exec(&scope) + if scope.is_a?(Relation) + scope else klass.unscoped.instance_exec(owner, &scope) end diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index 331b724814..4a78a9c076 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -23,6 +23,11 @@ module ActiveRecord::Associations::Builder @scope = nil @options = scope end + + if @scope && @scope.arity == 0 + prev_scope = @scope + @scope = proc { instance_exec(&prev_scope) } + end end def mixin diff --git a/activerecord/lib/active_record/associations/builder/collection_association.rb b/activerecord/lib/active_record/associations/builder/collection_association.rb index 98ca94b42a..af81af4ad2 100644 --- a/activerecord/lib/active_record/associations/builder/collection_association.rb +++ b/activerecord/lib/active_record/associations/builder/collection_association.rb @@ -6,7 +6,7 @@ module ActiveRecord::Associations::Builder super + [:table_name, :finder_sql, :counter_sql, :before_add, :after_add, :before_remove, :after_remove] end - attr_reader :block_extension + attr_reader :block_extension, :extension_module def initialize(*args, &extension) super(*args) @@ -27,18 +27,24 @@ module ActiveRecord::Associations::Builder private def wrap_block_extension - options[:extend] = Array(options[:extend]) - if block_extension + @extension_module = mod = Module.new(&block_extension) silence_warnings do - model.parent.const_set(extension_module_name, Module.new(&block_extension)) + model.parent.const_set(extension_module_name, mod) + end + + prev_scope = @scope + + if prev_scope + @scope = proc { |owner| instance_exec(owner, &prev_scope).extending(mod) } + else + @scope = proc { extending(mod) } end - options[:extend].push("#{model.parent}::#{extension_module_name}".constantize) end end def extension_module_name - @extension_module_name ||= "#{model.to_s.demodulize}#{name.to_s.camelize}AssociationExtension" + @extension_module_name ||= "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension" end def define_callback(callback_name) diff --git a/activerecord/lib/active_record/relation/query_methods.rb b/activerecord/lib/active_record/relation/query_methods.rb index c567cc7cc4..c958fdb5d5 100644 --- a/activerecord/lib/active_record/relation/query_methods.rb +++ b/activerecord/lib/active_record/relation/query_methods.rb @@ -529,7 +529,7 @@ module ActiveRecord def extending!(*modules, &block) modules << Module.new(&block) if block_given? - self.extending_values = modules.flatten + self.extending_values += modules.flatten extend(*extending_values) if extending_values.any? self |