1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Make use of redefine_method, removed some more redefining warnings

This commit is contained in:
Łukasz Strzałkowski 2010-07-17 17:42:17 +08:00 committed by Aaron Patterson
parent 7637b7184a
commit 661fd98aad
4 changed files with 15 additions and 29 deletions

View file

@ -1354,8 +1354,7 @@ module ActiveRecord
end end
def association_accessor_methods(reflection, association_proxy_class) def association_accessor_methods(reflection, association_proxy_class)
remove_possible_method(reflection.name) redefine_method(reflection.name) do |*params|
define_method(reflection.name) do |*params|
force_reload = params.first unless params.empty? force_reload = params.first unless params.empty?
association = association_instance_get(reflection.name) association = association_instance_get(reflection.name)
@ -1372,16 +1371,12 @@ module ActiveRecord
association.target.nil? ? nil : association association.target.nil? ? nil : association
end end
method = "loaded_#{reflection.name}?" redefine_method("loaded_#{reflection.name}?") do
remove_possible_method(method)
define_method(method) do
association = association_instance_get(reflection.name) association = association_instance_get(reflection.name)
association && association.loaded? association && association.loaded?
end end
method = "#{reflection.name}=" redefine_method("#{reflection.name}=") do |new_value|
remove_possible_method(method)
define_method(method) do |new_value|
association = association_instance_get(reflection.name) association = association_instance_get(reflection.name)
if association.nil? || association.target != new_value if association.nil? || association.target != new_value
@ -1392,9 +1387,7 @@ module ActiveRecord
association_instance_set(reflection.name, new_value.nil? ? nil : association) association_instance_set(reflection.name, new_value.nil? ? nil : association)
end end
method = "set_#{reflection.name}_target" redefine_method("set_#{reflection.name}_target") do |target|
remove_possible_method(method)
define_method(method) do |target|
return if target.nil? and association_proxy_class == BelongsToAssociation return if target.nil? and association_proxy_class == BelongsToAssociation
association = association_proxy_class.new(self, reflection) association = association_proxy_class.new(self, reflection)
association.target = target association.target = target
@ -1403,8 +1396,7 @@ module ActiveRecord
end end
def collection_reader_method(reflection, association_proxy_class) def collection_reader_method(reflection, association_proxy_class)
remove_possible_method(reflection.name) redefine_method(reflection.name) do |*params|
define_method(reflection.name) do |*params|
force_reload = params.first unless params.empty? force_reload = params.first unless params.empty?
association = association_instance_get(reflection.name) association = association_instance_get(reflection.name)
@ -1418,9 +1410,7 @@ module ActiveRecord
association association
end end
method = "#{reflection.name.to_s.singularize}_ids" redefine_method("#{reflection.name.to_s.singularize}_ids") do
remove_possible_method(method)
define_method(method) do
if send(reflection.name).loaded? || reflection.options[:finder_sql] if send(reflection.name).loaded? || reflection.options[:finder_sql]
send(reflection.name).map(&:id) send(reflection.name).map(&:id)
else else
@ -1440,16 +1430,14 @@ module ActiveRecord
collection_reader_method(reflection, association_proxy_class) collection_reader_method(reflection, association_proxy_class)
if writer if writer
define_method("#{reflection.name}=") do |new_value| redefine_method("#{reflection.name}=") do |new_value|
# Loads proxy class instance (defined in collection_reader_method) if not already loaded # Loads proxy class instance (defined in collection_reader_method) if not already loaded
association = send(reflection.name) association = send(reflection.name)
association.replace(new_value) association.replace(new_value)
association association
end end
method = "#{reflection.name.to_s.singularize}_ids=" redefine_method("#{reflection.name.to_s.singularize}_ids=") do |new_value|
remove_possible_method(method)
define_method(method) do |new_value|
ids = (new_value || []).reject { |nid| nid.blank? }.map(&:to_i) ids = (new_value || []).reject { |nid| nid.blank? }.map(&:to_i)
send("#{reflection.name}=", reflection.klass.find(ids).index_by(&:id).values_at(*ids)) send("#{reflection.name}=", reflection.klass.find(ids).index_by(&:id).values_at(*ids))
end end
@ -1457,9 +1445,7 @@ module ActiveRecord
end end
def association_constructor_method(constructor, reflection, association_proxy_class) def association_constructor_method(constructor, reflection, association_proxy_class)
method = "#{constructor}_#{reflection.name}" redefine_method("#{constructor}_#{reflection.name}") do |*params|
remove_possible_method(method)
define_method(method) do |*params|
attributees = params.first unless params.empty? attributees = params.first unless params.empty?
replace_existing = params[1].nil? ? true : params[1] replace_existing = params[1].nil? ? true : params[1]
association = association_instance_get(reflection.name) association = association_instance_get(reflection.name)
@ -1500,9 +1486,8 @@ module ActiveRecord
end end
def add_touch_callbacks(reflection, touch_attribute) def add_touch_callbacks(reflection, touch_attribute)
method_name = "belongs_to_touch_after_save_or_destroy_for_#{reflection.name}".to_sym method_name = :"belongs_to_touch_after_save_or_destroy_for_#{reflection.name}"
remove_possible_method(method_name) redefine_method(method_name) do
define_method(method_name) do
association = send(reflection.name) association = send(reflection.name)
if touch_attribute == true if touch_attribute == true

View file

@ -871,7 +871,7 @@ module ActiveRecord
table_names.each do |table_name| table_names.each do |table_name|
table_name = table_name.to_s.tr('./', '_') table_name = table_name.to_s.tr('./', '_')
define_method(table_name) do |*fixtures| redefine_method(table_name) do |*fixtures|
force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload force_reload = fixtures.pop if fixtures.last == true || fixtures.last == :reload
@fixture_cache[table_name] ||= {} @fixture_cache[table_name] ||= {}

View file

@ -105,7 +105,7 @@ module ActiveRecord
extension ? relation.extending(extension) : relation extension ? relation.extending(extension) : relation
end end
singleton_class.send :define_method, name, &scopes[name] singleton_class.send(:redefine_method, name, &scopes[name])
end end
def named_scope(*args, &block) def named_scope(*args, &block)

View file

@ -101,6 +101,7 @@ class ActiveSchemaTest < ActiveRecord::TestCase
#we need to actually modify some data, so we make execute point to the original method #we need to actually modify some data, so we make execute point to the original method
ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
alias_method :execute_with_stub, :execute alias_method :execute_with_stub, :execute
remove_method :execute
alias_method :execute, :execute_without_stub alias_method :execute, :execute_without_stub
end end
yield yield