From c1cd3424569cf76b0e19e2854ec2829d87ed02bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Wed, 9 Oct 2013 20:48:28 -0300 Subject: Extract the scope building to a class method --- .../active_record/associations/builder/association.rb | 19 +++++++++++++++++-- .../associations/builder/collection_association.rb | 8 -------- 2 files changed, 17 insertions(+), 10 deletions(-) (limited to 'activerecord/lib/active_record/associations/builder') diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index df9cc65758..4efd5aa8ba 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -42,14 +42,29 @@ module ActiveRecord::Associations::Builder def initialize(name, scope, options, extension) @name = name - @scope = scope @options = options self.class.validate_options(options) + @scope = self.class.build_scope(scope, extension) + end + + def self.build_scope(scope, extension) + new_scope = scope + if scope && scope.arity == 0 - @scope = proc { instance_exec(&scope) } + new_scope = proc { instance_exec(&scope) } + end + + if extension + new_scope = wrap_scope new_scope, extension end + + new_scope + end + + def self.wrap_scope(scope, extension) + scope end def build(model) diff --git a/activerecord/lib/active_record/associations/builder/collection_association.rb b/activerecord/lib/active_record/associations/builder/collection_association.rb index 2676037e80..521597e822 100644 --- a/activerecord/lib/active_record/associations/builder/collection_association.rb +++ b/activerecord/lib/active_record/associations/builder/collection_association.rb @@ -12,14 +12,6 @@ module ActiveRecord::Associations::Builder :after_add, :before_remove, :after_remove, :extend] end - def initialize(name, scope, options, extension) - super - - if extension - @scope = self.class.wrap_scope @scope, extension - end - end - def self.define_callbacks(model, reflection) super name = reflection.name -- cgit v1.2.3