mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Revert the whole refactoring in the association builder classes.
This is to get activerecord-deprecated_finders work again
This commit is contained in:
parent
5853c64a4b
commit
7a036ebd30
8 changed files with 68 additions and 51 deletions
|
@ -15,23 +15,26 @@ module ActiveRecord::Associations::Builder
|
||||||
class Association #:nodoc:
|
class Association #:nodoc:
|
||||||
class << self
|
class << self
|
||||||
attr_accessor :extensions
|
attr_accessor :extensions
|
||||||
|
# TODO: This class accessor is needed to make activerecord-deprecated_finders work.
|
||||||
|
# We can move it to a constant in 5.0.
|
||||||
|
attr_accessor :valid_options
|
||||||
end
|
end
|
||||||
self.extensions = []
|
self.extensions = []
|
||||||
|
|
||||||
# TODO: This class accessor is needed to make activerecord-deprecated_finders work.
|
|
||||||
# We can move it to a constant in 5.0.
|
|
||||||
cattr_accessor :valid_options, instance_accessor: false
|
|
||||||
self.valid_options = [:class_name, :class, :foreign_key, :validate]
|
self.valid_options = [:class_name, :class, :foreign_key, :validate]
|
||||||
|
|
||||||
|
attr_reader :name, :scope, :options
|
||||||
|
|
||||||
def self.build(model, name, scope, options, &block)
|
def self.build(model, name, scope, options, &block)
|
||||||
extension = define_extensions model, name, &block
|
builder = create_builder model, name, scope, options, &block
|
||||||
reflection = create_reflection model, name, scope, options, extension
|
reflection = builder.build(model)
|
||||||
define_accessors model, reflection
|
define_accessors model, reflection
|
||||||
define_callbacks model, reflection
|
define_callbacks model, reflection
|
||||||
|
builder.define_extensions model
|
||||||
reflection
|
reflection
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.create_reflection(model, name, scope, options, extension = nil)
|
def self.create_builder(model, name, scope, options, &block)
|
||||||
raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)
|
raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)
|
||||||
|
|
||||||
if scope.is_a?(Hash)
|
if scope.is_a?(Hash)
|
||||||
|
@ -39,44 +42,39 @@ module ActiveRecord::Associations::Builder
|
||||||
scope = nil
|
scope = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
validate_options(options)
|
new(model, name, scope, options, &block)
|
||||||
|
end
|
||||||
|
|
||||||
scope = build_scope(scope, extension)
|
def initialize(model, name, scope, options)
|
||||||
|
# TODO: Remove this model argument as soon we drop support to activerecord-deprecated_finders.
|
||||||
|
@name = name
|
||||||
|
@scope = scope
|
||||||
|
@options = options
|
||||||
|
|
||||||
|
validate_options
|
||||||
|
|
||||||
|
if scope && scope.arity == 0
|
||||||
|
@scope = proc { instance_exec(&scope) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def build(model)
|
||||||
ActiveRecord::Reflection.create(macro, name, scope, options, model)
|
ActiveRecord::Reflection.create(macro, name, scope, options, model)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.build_scope(scope, extension)
|
def macro
|
||||||
new_scope = scope
|
|
||||||
|
|
||||||
if scope && scope.arity == 0
|
|
||||||
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 self.macro
|
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.build_valid_options(options)
|
def valid_options
|
||||||
self.valid_options + Association.extensions.flat_map(&:valid_options)
|
Association.valid_options + Association.extensions.flat_map(&:valid_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.validate_options(options)
|
def validate_options
|
||||||
options.assert_valid_keys(build_valid_options(options))
|
options.assert_valid_keys(valid_options)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.define_extensions(model, name)
|
def define_extensions(model)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.define_callbacks(model, reflection)
|
def self.define_callbacks(model, reflection)
|
||||||
|
@ -119,6 +117,8 @@ module ActiveRecord::Associations::Builder
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
def self.add_before_destroy_callbacks(model, reflection)
|
def self.add_before_destroy_callbacks(model, reflection)
|
||||||
unless valid_dependent_options.include? reflection.options[:dependent]
|
unless valid_dependent_options.include? reflection.options[:dependent]
|
||||||
raise ArgumentError, "The :dependent option must be one of #{valid_dependent_options}, but is :#{reflection.options[:dependent]}"
|
raise ArgumentError, "The :dependent option must be one of #{valid_dependent_options}, but is :#{reflection.options[:dependent]}"
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
module ActiveRecord::Associations::Builder
|
module ActiveRecord::Associations::Builder
|
||||||
class BelongsTo < SingularAssociation #:nodoc:
|
class BelongsTo < SingularAssociation #:nodoc:
|
||||||
def self.macro
|
def macro
|
||||||
:belongs_to
|
:belongs_to
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.build_valid_options(options)
|
def valid_options
|
||||||
super + [:foreign_type, :polymorphic, :touch, :counter_cache]
|
super + [:foreign_type, :polymorphic, :touch, :counter_cache]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -23,6 +23,8 @@ module ActiveRecord::Associations::Builder
|
||||||
add_counter_cache_methods mixin
|
add_counter_cache_methods mixin
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
def self.add_counter_cache_methods(mixin)
|
def self.add_counter_cache_methods(mixin)
|
||||||
return if mixin.method_defined? :belongs_to_counter_cache_after_create
|
return if mixin.method_defined? :belongs_to_counter_cache_after_create
|
||||||
|
|
||||||
|
|
|
@ -7,11 +7,22 @@ module ActiveRecord::Associations::Builder
|
||||||
|
|
||||||
CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]
|
CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]
|
||||||
|
|
||||||
def self.build_valid_options(options)
|
def valid_options
|
||||||
super + [:table_name, :before_add,
|
super + [:table_name, :before_add,
|
||||||
:after_add, :before_remove, :after_remove, :extend]
|
:after_add, :before_remove, :after_remove, :extend]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
attr_reader :block_extension
|
||||||
|
|
||||||
|
def initialize(model, name, scope, options)
|
||||||
|
super
|
||||||
|
@mod = nil
|
||||||
|
if block_given?
|
||||||
|
@mod = Module.new(&Proc.new)
|
||||||
|
@scope = wrap_scope @scope, @mod
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.define_callbacks(model, reflection)
|
def self.define_callbacks(model, reflection)
|
||||||
super
|
super
|
||||||
name = reflection.name
|
name = reflection.name
|
||||||
|
@ -21,11 +32,10 @@ module ActiveRecord::Associations::Builder
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.define_extensions(model, name)
|
def define_extensions(model)
|
||||||
if block_given?
|
if @mod
|
||||||
extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
|
extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
|
||||||
extension = Module.new(&Proc.new)
|
model.parent.const_set(extension_module_name, @mod)
|
||||||
model.parent.const_set(extension_module_name, extension)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -68,7 +78,9 @@ module ActiveRecord::Associations::Builder
|
||||||
CODE
|
CODE
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.wrap_scope(scope, mod)
|
private
|
||||||
|
|
||||||
|
def wrap_scope(scope, mod)
|
||||||
if scope
|
if scope
|
||||||
proc { |owner| instance_exec(owner, &scope).extending(mod) }
|
proc { |owner| instance_exec(owner, &scope).extending(mod) }
|
||||||
else
|
else
|
||||||
|
|
|
@ -84,11 +84,11 @@ module ActiveRecord::Associations::Builder
|
||||||
middle_name = [lhs_model.name.downcase.pluralize,
|
middle_name = [lhs_model.name.downcase.pluralize,
|
||||||
association_name].join('_').gsub(/::/, '_').to_sym
|
association_name].join('_').gsub(/::/, '_').to_sym
|
||||||
middle_options = middle_options join_model
|
middle_options = middle_options join_model
|
||||||
|
hm_builder = HasMany.create_builder(lhs_model,
|
||||||
HasMany.create_reflection(lhs_model,
|
middle_name,
|
||||||
middle_name,
|
nil,
|
||||||
nil,
|
middle_options)
|
||||||
middle_options)
|
hm_builder.build lhs_model
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
module ActiveRecord::Associations::Builder
|
module ActiveRecord::Associations::Builder
|
||||||
class HasMany < CollectionAssociation #:nodoc:
|
class HasMany < CollectionAssociation #:nodoc:
|
||||||
def self.macro
|
def macro
|
||||||
:has_many
|
:has_many
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.build_valid_options(options)
|
def valid_options
|
||||||
super + [:primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache]
|
super + [:primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
module ActiveRecord::Associations::Builder
|
module ActiveRecord::Associations::Builder
|
||||||
class HasOne < SingularAssociation #:nodoc:
|
class HasOne < SingularAssociation #:nodoc:
|
||||||
def self.macro
|
def macro
|
||||||
:has_one
|
:has_one
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.build_valid_options(options)
|
def valid_options
|
||||||
valid = super + [:order, :as]
|
valid = super + [:order, :as]
|
||||||
valid += [:through, :source, :source_type] if options[:through]
|
valid += [:through, :source, :source_type] if options[:through]
|
||||||
valid
|
valid
|
||||||
|
@ -14,6 +14,8 @@ module ActiveRecord::Associations::Builder
|
||||||
[:destroy, :delete, :nullify, :restrict_with_error, :restrict_with_exception]
|
[:destroy, :delete, :nullify, :restrict_with_error, :restrict_with_exception]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
def self.add_before_destroy_callbacks(model, reflection)
|
def self.add_before_destroy_callbacks(model, reflection)
|
||||||
super unless reflection.options[:through]
|
super unless reflection.options[:through]
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
module ActiveRecord::Associations::Builder
|
module ActiveRecord::Associations::Builder
|
||||||
class SingularAssociation < Association #:nodoc:
|
class SingularAssociation < Association #:nodoc:
|
||||||
def self.build_valid_options(options)
|
def valid_options
|
||||||
super + [:remote, :dependent, :primary_key, :inverse_of]
|
super + [:remote, :dependent, :primary_key, :inverse_of]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -75,6 +75,7 @@ class AssociationsExtensionsTest < ActiveRecord::TestCase
|
||||||
private
|
private
|
||||||
|
|
||||||
def extend!(model)
|
def extend!(model)
|
||||||
ActiveRecord::Associations::Builder::HasMany.define_extensions(model, :association_name) { }
|
builder = ActiveRecord::Associations::Builder::HasMany.new(model, :association_name, nil, {}) { }
|
||||||
|
builder.define_extensions(model)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue