Skip to content

Commit 78dab2a

Browse files
committed
Remove support to activerecord-deprecated_finders
1 parent e8615b9 commit 78dab2a

16 files changed

Lines changed: 64 additions & 130 deletions

RELEASING_RAILS.rdoc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ after some refactoring or bug fix, so it is important to check if the following
3333
are working with the versions that will be released:
3434

3535
* https://github.com/rails/protected_attributes
36-
* https://github.com/rails/activerecord-deprecated_finders
3736

3837
Do not release red plugins tests.
3938

activerecord/lib/active_record/associations/builder/association.rb

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
require 'active_support/core_ext/module/attribute_accessors'
2-
31
# This is the parent Association class which defines the variables
42
# used by all associations.
53
#
@@ -15,15 +13,10 @@ module ActiveRecord::Associations::Builder
1513
class Association #:nodoc:
1614
class << self
1715
attr_accessor :extensions
18-
# TODO: This class accessor is needed to make activerecord-deprecated_finders work.
19-
# We can move it to a constant in 5.0.
20-
attr_accessor :valid_options
2116
end
2217
self.extensions = []
2318

24-
self.valid_options = [:class_name, :class, :foreign_key, :validate]
25-
26-
attr_reader :name, :scope, :options
19+
VALID_OPTIONS = [:class_name, :class, :foreign_key, :validate] # :nodoc:
2720

2821
def self.build(model, name, scope, options, &block)
2922
if model.dangerous_attribute_method?(name)
@@ -32,57 +25,60 @@ def self.build(model, name, scope, options, &block)
3225
"Please choose a different association name."
3326
end
3427

35-
builder = create_builder model, name, scope, options, &block
36-
reflection = builder.build(model)
28+
extension = define_extensions model, name, &block
29+
reflection = create_reflection model, name, scope, options, extension
3730
define_accessors model, reflection
3831
define_callbacks model, reflection
3932
define_validations model, reflection
40-
builder.define_extensions model
4133
reflection
4234
end
4335

44-
def self.create_builder(model, name, scope, options, &block)
36+
def self.create_reflection(model, name, scope, options, extension = nil)
4537
raise ArgumentError, "association names must be a Symbol" unless name.kind_of?(Symbol)
4638

47-
new(model, name, scope, options, &block)
48-
end
49-
50-
def initialize(model, name, scope, options)
51-
# TODO: Move this to create_builder as soon we drop support to activerecord-deprecated_finders.
5239
if scope.is_a?(Hash)
5340
options = scope
5441
scope = nil
5542
end
5643

57-
# TODO: Remove this model argument as soon we drop support to activerecord-deprecated_finders.
58-
@name = name
59-
@scope = scope
60-
@options = options
44+
validate_options(options)
6145

62-
validate_options
46+
scope = build_scope(scope, extension)
47+
48+
ActiveRecord::Reflection.create(macro, name, scope, options, model)
49+
end
50+
51+
def self.build_scope(scope, extension)
52+
new_scope = scope
6353

6454
if scope && scope.arity == 0
65-
@scope = proc { instance_exec(&scope) }
55+
new_scope = proc { instance_exec(&scope) }
56+
end
57+
58+
if extension
59+
new_scope = wrap_scope new_scope, extension
6660
end
61+
62+
new_scope
6763
end
6864

69-
def build(model)
70-
ActiveRecord::Reflection.create(macro, name, scope, options, model)
65+
def self.wrap_scope(scope, extension)
66+
scope
7167
end
7268

73-
def macro
69+
def self.macro
7470
raise NotImplementedError
7571
end
7672

77-
def valid_options
78-
Association.valid_options + Association.extensions.flat_map(&:valid_options)
73+
def self.valid_options(options)
74+
VALID_OPTIONS + Association.extensions.flat_map(&:valid_options)
7975
end
8076

81-
def validate_options
82-
options.assert_valid_keys(valid_options)
77+
def self.validate_options(options)
78+
options.assert_valid_keys(valid_options(options))
8379
end
8480

85-
def define_extensions(model)
81+
def self.define_extensions(model, name)
8682
end
8783

8884
def self.define_callbacks(model, reflection)
@@ -133,8 +129,6 @@ def self.valid_dependent_options
133129
raise NotImplementedError
134130
end
135131

136-
private
137-
138132
def self.check_dependent_options(dependent)
139133
unless valid_dependent_options.include? dependent
140134
raise ArgumentError, "The :dependent option must be one of #{valid_dependent_options}, but is :#{dependent}"

activerecord/lib/active_record/associations/builder/belongs_to.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module ActiveRecord::Associations::Builder
22
class BelongsTo < SingularAssociation #:nodoc:
3-
def macro
3+
def self.macro
44
:belongs_to
55
end
66

7-
def valid_options
7+
def self.valid_options(options)
88
super + [:foreign_type, :polymorphic, :touch, :counter_cache]
99
end
1010

@@ -23,8 +23,6 @@ def self.define_accessors(mixin, reflection)
2323
add_counter_cache_methods mixin
2424
end
2525

26-
private
27-
2826
def self.add_counter_cache_methods(mixin)
2927
return if mixin.method_defined? :belongs_to_counter_cache_after_update
3028

activerecord/lib/active_record/associations/builder/collection_association.rb

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,11 @@ class CollectionAssociation < Association #:nodoc:
77

88
CALLBACKS = [:before_add, :after_add, :before_remove, :after_remove]
99

10-
def valid_options
10+
def self.valid_options(options)
1111
super + [:table_name, :before_add,
1212
:after_add, :before_remove, :after_remove, :extend]
1313
end
1414

15-
attr_reader :block_extension
16-
17-
def initialize(model, name, scope, options)
18-
super
19-
@mod = nil
20-
if block_given?
21-
@mod = Module.new(&Proc.new)
22-
@scope = wrap_scope @scope, @mod
23-
end
24-
end
25-
2615
def self.define_callbacks(model, reflection)
2716
super
2817
name = reflection.name
@@ -32,10 +21,11 @@ def self.define_callbacks(model, reflection)
3221
}
3322
end
3423

35-
def define_extensions(model)
36-
if @mod
24+
def self.define_extensions(model, name)
25+
if block_given?
3726
extension_module_name = "#{model.name.demodulize}#{name.to_s.camelize}AssociationExtension"
38-
model.parent.const_set(extension_module_name, @mod)
27+
extension = Module.new(&Proc.new)
28+
model.parent.const_set(extension_module_name, extension)
3929
end
4030
end
4131

@@ -78,9 +68,7 @@ def #{name.to_s.singularize}_ids=(ids)
7868
CODE
7969
end
8070

81-
private
82-
83-
def wrap_scope(scope, mod)
71+
def self.wrap_scope(scope, mod)
8472
if scope
8573
proc { |owner| instance_exec(owner, &scope).extending(mod) }
8674
else

activerecord/lib/active_record/associations/builder/has_and_belongs_to_many.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ def middle_reflection(join_model)
8787
middle_name = [lhs_model.name.downcase.pluralize,
8888
association_name].join('_').gsub(/::/, '_').to_sym
8989
middle_options = middle_options join_model
90-
hm_builder = HasMany.create_builder(lhs_model,
91-
middle_name,
92-
nil,
93-
middle_options)
94-
hm_builder.build lhs_model
90+
91+
HasMany.create_reflection(lhs_model,
92+
middle_name,
93+
nil,
94+
middle_options)
9595
end
9696

9797
private

activerecord/lib/active_record/associations/builder/has_many.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module ActiveRecord::Associations::Builder
22
class HasMany < CollectionAssociation #:nodoc:
3-
def macro
3+
def self.macro
44
:has_many
55
end
66

7-
def valid_options
7+
def self.valid_options(options)
88
super + [:primary_key, :dependent, :as, :through, :source, :source_type, :inverse_of, :counter_cache, :join_table, :foreign_type]
99
end
1010

activerecord/lib/active_record/associations/builder/has_one.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
module ActiveRecord::Associations::Builder
22
class HasOne < SingularAssociation #:nodoc:
3-
def macro
3+
def self.macro
44
:has_one
55
end
66

7-
def valid_options
7+
def self.valid_options(options)
88
valid = super + [:as, :foreign_type]
99
valid += [:through, :source, :source_type] if options[:through]
1010
valid
@@ -14,8 +14,6 @@ def self.valid_dependent_options
1414
[:destroy, :delete, :nullify, :restrict_with_error, :restrict_with_exception]
1515
end
1616

17-
private
18-
1917
def self.add_destroy_callbacks(model, reflection)
2018
super unless reflection.options[:through]
2119
end

activerecord/lib/active_record/associations/builder/singular_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module ActiveRecord::Associations::Builder
44
class SingularAssociation < Association #:nodoc:
5-
def valid_options
5+
def self.valid_options(options)
66
super + [:dependent, :primary_key, :inverse_of, :required]
77
end
88

activerecord/lib/active_record/associations/collection_association.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,7 @@ def destroy_all
218218

219219
# Count all records using SQL. Construct options and pass them with
220220
# scope to the target class's +count+.
221-
def count(column_name = nil, count_options = {})
222-
# TODO: Remove count_options argument as soon we remove support to
223-
# activerecord-deprecated_finders.
224-
column_name, count_options = nil, column_name if column_name.is_a?(Hash)
225-
221+
def count(column_name = nil)
226222
relation = scope
227223
if association_scope.distinct_value
228224
# This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL.

activerecord/lib/active_record/associations/collection_proxy.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -688,10 +688,8 @@ def distinct
688688
# # #<Pet id: 2, name: "Spook", person_id: 1>,
689689
# # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
690690
# # ]
691-
def count(column_name = nil, options = {})
692-
# TODO: Remove options argument as soon we remove support to
693-
# activerecord-deprecated_finders.
694-
@association.count(column_name, options)
691+
def count(column_name = nil)
692+
@association.count(column_name)
695693
end
696694

697695
# Returns the size of the collection. If the collection hasn't been loaded,

0 commit comments

Comments
 (0)