Permalink
Browse files

push module building to the constructor

  • Loading branch information...
1 parent 0a3724d commit 71aba97d8d0c1b0264646c64aa3dc5661a90424c @tenderlove tenderlove committed Aug 1, 2013
Showing with 20 additions and 14 deletions.
  1. +20 −14 activerecord/lib/active_record/associations/builder/collection_association.rb
@@ -14,9 +14,13 @@ def valid_options
attr_reader :block_extension
- def initialize(*args, &extension)
+ def initialize(*args)
super(*args)
- @block_extension = extension
+ @mod = nil
+ if block_given?
+ @mod = Module.new(&Proc.new)
+ @scope = wrap_scope @scope, @mod
+ end
end
def build
@@ -31,19 +35,9 @@ def define_callbacks(model, reflection)
end
def define_extensions(model)
- if block_extension
- mod = Module.new(&block_extension)
+ if @mod
extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
-
- model.parent.const_set(extension_module_name, mod)
-
- prev_scope = @scope
-
- if prev_scope
- @scope = proc { |owner| instance_exec(owner, &prev_scope).extending(mod) }
- else
- @scope = proc { extending(mod) }
- end
+ model.parent.const_set(extension_module_name, @mod)
end
end
@@ -86,5 +80,17 @@ def #{name.to_s.singularize}_ids=(ids)
end
CODE
end
+
+ private
+
+ def wrap_scope(scope, mod)
+ prev_scope = scope
+
+ if prev_scope
+ proc { |owner| instance_exec(owner, &prev_scope).extending(mod) }
+ else
+ proc { extending(mod) }
+ end
+ end
end
end

0 comments on commit 71aba97

Please sign in to comment.