Permalink
Browse files

Define the association extensions without need to have a builder

instance
  • Loading branch information...
rafaelfranca committed Oct 9, 2013
1 parent 941516d commit 0ee7331c35994e543df396548c5b455c00c96cb7
@@ -21,26 +21,26 @@ class << self
attr_reader :name, :scope, :options
def self.build(model, name, scope, options, &block)
- builder = create_builder model, name, scope, options, &block
+ extension = define_extensions model, name, &block
+ builder = create_builder model, name, scope, options, extension
reflection = builder.build(model)
define_accessors model, reflection
define_callbacks model, reflection
- builder.define_extensions model
reflection
end
- def self.create_builder(model, name, scope, options, &block)
+ def self.create_builder(model, name, scope, options, extension = nil)
raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)
if scope.is_a?(Hash)
options = scope
scope = nil
end
- new(name, scope, options, &block)
+ new(name, scope, options, extension)
end
- def initialize(name, scope, options)
+ def initialize(name, scope, options, extension)
@name = name
@scope = scope
@options = options
@@ -68,7 +68,7 @@ def validate_options
options.assert_valid_keys(valid_options)
end
- def define_extensions(model)
+ def self.define_extensions(model, name)
end
def self.define_callbacks(model, reflection)
@@ -14,12 +14,11 @@ def valid_options
attr_reader :block_extension
- def initialize(name, scope, options)
+ def initialize(name, scope, options, extension)
super
- @mod = nil
- if block_given?
- @mod = Module.new(&Proc.new)
- @scope = wrap_scope @scope, @mod
+
+ if extension
+ @scope = wrap_scope @scope, extension
end
end
@@ -32,10 +31,11 @@ def self.define_callbacks(model, reflection)
}
end
- def define_extensions(model)
- if @mod
+ def self.define_extensions(model, name)
+ if block_given?
extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
- model.parent.const_set(extension_module_name, @mod)
+ extension = Module.new(&Proc.new)
+ model.parent.const_set(extension_module_name, extension)
end
end
@@ -75,7 +75,6 @@ def test_proxy_association_after_scoped
private
def extend!(model)
- builder = ActiveRecord::Associations::Builder::HasMany.new(:association_name, nil, {}) { }
- builder.define_extensions(model)
+ ActiveRecord::Associations::Builder::HasMany.define_extensions(model, :association_name) { }
end
end

0 comments on commit 0ee7331

Please sign in to comment.