diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-10-09 20:48:28 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2013-10-09 20:48:28 -0300 |
commit | c1cd3424569cf76b0e19e2854ec2829d87ed02bd (patch) | |
tree | 20af8fc3f290f0d879475ece6515912576e38c04 | |
parent | 1a30b1ed4066cbefc9faacae081655c1da38836f (diff) | |
download | rails-c1cd3424569cf76b0e19e2854ec2829d87ed02bd.tar.gz rails-c1cd3424569cf76b0e19e2854ec2829d87ed02bd.tar.bz2 rails-c1cd3424569cf76b0e19e2854ec2829d87ed02bd.zip |
Extract the scope building to a class method
-rw-r--r-- | activerecord/lib/active_record/associations/builder/association.rb | 19 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/builder/collection_association.rb | 8 |
2 files changed, 17 insertions, 10 deletions
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 |