Skip to content

Commit

Permalink
Remove support to activerecord-deprecated_finders
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelfranca committed Jan 2, 2015
1 parent e8615b9 commit 78dab2a
Show file tree
Hide file tree
Showing 16 changed files with 64 additions and 130 deletions.
1 change: 0 additions & 1 deletion RELEASING_RAILS.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ after some refactoring or bug fix, so it is important to check if the following
are working with the versions that will be released:

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

Do not release red plugins tests.

Expand Down
60 changes: 27 additions & 33 deletions activerecord/lib/active_record/associations/builder/association.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
require 'active_support/core_ext/module/attribute_accessors'

# This is the parent Association class which defines the variables
# used by all associations.
#
Expand All @@ -15,15 +13,10 @@ module ActiveRecord::Associations::Builder
class Association #:nodoc:
class << self
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
self.extensions = []

self.valid_options = [:class_name, :class, :foreign_key, :validate]

attr_reader :name, :scope, :options
VALID_OPTIONS = [:class_name, :class, :foreign_key, :validate] # :nodoc:

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

builder = create_builder model, name, scope, options, &block
reflection = builder.build(model)
extension = define_extensions model, name, &block
reflection = create_reflection model, name, scope, options, extension
define_accessors model, reflection
define_callbacks model, reflection
define_validations model, reflection
builder.define_extensions model
reflection
end

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

new(model, name, scope, options, &block)
end

def initialize(model, name, scope, options)
# TODO: Move this to create_builder as soon we drop support to activerecord-deprecated_finders.
if scope.is_a?(Hash)
options = scope
scope = nil
end

# TODO: Remove this model argument as soon we drop support to activerecord-deprecated_finders.
@name = name
@scope = scope
@options = options
validate_options(options)

validate_options
scope = build_scope(scope, extension)

ActiveRecord::Reflection.create(macro, name, scope, options, model)
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 build(model)
ActiveRecord::Reflection.create(macro, name, scope, options, model)
def self.wrap_scope(scope, extension)
scope
end

def macro
def self.macro
raise NotImplementedError
end

def valid_options
Association.valid_options + Association.extensions.flat_map(&:valid_options)
def self.valid_options(options)
VALID_OPTIONS + Association.extensions.flat_map(&:valid_options)
end

def validate_options
options.assert_valid_keys(valid_options)
def self.validate_options(options)
options.assert_valid_keys(valid_options(options))
end

def define_extensions(model)
def self.define_extensions(model, name)
end

def self.define_callbacks(model, reflection)
Expand Down Expand Up @@ -133,8 +129,6 @@ def self.valid_dependent_options
raise NotImplementedError
end

private

def self.check_dependent_options(dependent)
unless valid_dependent_options.include? dependent
raise ArgumentError, "The :dependent option must be one of #{valid_dependent_options}, but is :#{dependent}"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module ActiveRecord::Associations::Builder
class BelongsTo < SingularAssociation #:nodoc:
def macro
def self.macro
:belongs_to
end

def valid_options
def self.valid_options(options)
super + [:foreign_type, :polymorphic, :touch, :counter_cache]
end

Expand All @@ -23,8 +23,6 @@ def self.define_accessors(mixin, reflection)
add_counter_cache_methods mixin
end

private

def self.add_counter_cache_methods(mixin)
return if mixin.method_defined? :belongs_to_counter_cache_after_update

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,11 @@ class CollectionAssociation < Association #:nodoc:

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

def valid_options
def self.valid_options(options)
super + [:table_name, :before_add,
:after_add, :before_remove, :after_remove, :extend]
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)
super
name = reflection.name
Expand All @@ -32,10 +21,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

Expand Down Expand Up @@ -78,9 +68,7 @@ def #{name.to_s.singularize}_ids=(ids)
CODE
end

private

def wrap_scope(scope, mod)
def self.wrap_scope(scope, mod)
if scope
proc { |owner| instance_exec(owner, &scope).extending(mod) }
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ def middle_reflection(join_model)
middle_name = [lhs_model.name.downcase.pluralize,
association_name].join('_').gsub(/::/, '_').to_sym
middle_options = middle_options join_model
hm_builder = HasMany.create_builder(lhs_model,
middle_name,
nil,
middle_options)
hm_builder.build lhs_model

HasMany.create_reflection(lhs_model,
middle_name,
nil,
middle_options)
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module ActiveRecord::Associations::Builder
class HasMany < CollectionAssociation #:nodoc:
def macro
def self.macro
:has_many
end

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

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module ActiveRecord::Associations::Builder
class HasOne < SingularAssociation #:nodoc:
def macro
def self.macro
:has_one
end

def valid_options
def self.valid_options(options)
valid = super + [:as, :foreign_type]
valid += [:through, :source, :source_type] if options[:through]
valid
Expand All @@ -14,8 +14,6 @@ def self.valid_dependent_options
[:destroy, :delete, :nullify, :restrict_with_error, :restrict_with_exception]
end

private

def self.add_destroy_callbacks(model, reflection)
super unless reflection.options[:through]
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,7 @@ def destroy_all

# Count all records using SQL. Construct options and pass them with
# scope to the target class's +count+.
def count(column_name = nil, count_options = {})
# TODO: Remove count_options argument as soon we remove support to
# activerecord-deprecated_finders.
column_name, count_options = nil, column_name if column_name.is_a?(Hash)

def count(column_name = nil)
relation = scope
if association_scope.distinct_value
# This is needed because 'SELECT count(DISTINCT *)..' is not valid SQL.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -688,10 +688,8 @@ def distinct
# # #<Pet id: 2, name: "Spook", person_id: 1>,
# # #<Pet id: 3, name: "Choo-Choo", person_id: 1>
# # ]
def count(column_name = nil, options = {})
# TODO: Remove options argument as soon we remove support to
# activerecord-deprecated_finders.
@association.count(column_name, options)
def count(column_name = nil)
@association.count(column_name)
end

# Returns the size of the collection. If the collection hasn't been loaded,
Expand Down
21 changes: 1 addition & 20 deletions activerecord/lib/active_record/dynamic_matchers.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
module ActiveRecord
module DynamicMatchers #:nodoc:
# This code in this file seems to have a lot of indirection, but the indirection
# is there to provide extension points for the activerecord-deprecated_finders
# gem. When we stop supporting activerecord-deprecated_finders (from Rails 5),
# then we can remove the indirection.

def respond_to?(name, include_private = false)
if self == Base
super
Expand Down Expand Up @@ -72,26 +67,14 @@ def self.#{name}(#{signature})
CODE
end

def body
raise NotImplementedError
end
end
private

module Finder
# Extended in activerecord-deprecated_finders
def body
result
end

# Extended in activerecord-deprecated_finders
def result
"#{finder}(#{attributes_hash})"
end

# The parameters in the signature may have reserved Ruby words, in order
# to prevent errors, we start each param name with `_`.
#
# Extended in activerecord-deprecated_finders
def signature
attribute_names.map { |name| "_#{name}" }.join(', ')
end
Expand All @@ -109,7 +92,6 @@ def finder

class FindBy < Method
Method.matchers << self
include Finder

def self.prefix
"find_by"
Expand All @@ -122,7 +104,6 @@ def finder

class FindByBang < Method
Method.matchers << self
include Finder

def self.prefix
"find_by"
Expand Down
4 changes: 1 addition & 3 deletions activerecord/lib/active_record/null_relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ def maximum(*)
calculate :maximum, nil
end

def calculate(operation, _column_name, _options = {})
# TODO: Remove _options argument as soon we remove support to
# activerecord-deprecated_finders.
def calculate(operation, _column_name)
if [:count, :sum, :size].include? operation
group_values.any? ? Hash.new : 0
elsif [:average, :minimum, :maximum].include?(operation) && group_values.any?
Expand Down
Loading

0 comments on commit 78dab2a

Please sign in to comment.