Permalink
Browse files

Make use of redefine_method, removed some more redefining warnings

  • Loading branch information...
1 parent 7637b71 commit 661fd98aad027b6171253828ed89115f30fac46f @lukaszx0 lukaszx0 committed with tenderlove Jul 17, 2010
@@ -1354,8 +1354,7 @@ def join_table_name(first_table_name, second_table_name)
end
def association_accessor_methods(reflection, association_proxy_class)
- remove_possible_method(reflection.name)
- define_method(reflection.name) do |*params|
+ redefine_method(reflection.name) do |*params|
force_reload = params.first unless params.empty?
association = association_instance_get(reflection.name)
@@ -1372,16 +1371,12 @@ def association_accessor_methods(reflection, association_proxy_class)
association.target.nil? ? nil : association
end
- method = "loaded_#{reflection.name}?"
- remove_possible_method(method)
- define_method(method) do
+ redefine_method("loaded_#{reflection.name}?") do
association = association_instance_get(reflection.name)
association && association.loaded?
end
-
- method = "#{reflection.name}="
- remove_possible_method(method)
- define_method(method) do |new_value|
+
+ redefine_method("#{reflection.name}=") do |new_value|
association = association_instance_get(reflection.name)
if association.nil? || association.target != new_value
@@ -1392,9 +1387,7 @@ def association_accessor_methods(reflection, association_proxy_class)
association_instance_set(reflection.name, new_value.nil? ? nil : association)
end
- method = "set_#{reflection.name}_target"
- remove_possible_method(method)
- define_method(method) do |target|
+ redefine_method("set_#{reflection.name}_target") do |target|
return if target.nil? and association_proxy_class == BelongsToAssociation
association = association_proxy_class.new(self, reflection)
association.target = target
@@ -1403,8 +1396,7 @@ def association_accessor_methods(reflection, association_proxy_class)
end
def collection_reader_method(reflection, association_proxy_class)
- remove_possible_method(reflection.name)
- define_method(reflection.name) do |*params|
+ redefine_method(reflection.name) do |*params|
force_reload = params.first unless params.empty?
association = association_instance_get(reflection.name)
@@ -1418,9 +1410,7 @@ def collection_reader_method(reflection, association_proxy_class)
association
end
- method = "#{reflection.name.to_s.singularize}_ids"
- remove_possible_method(method)
- define_method(method) do
+ redefine_method("#{reflection.name.to_s.singularize}_ids") do
if send(reflection.name).loaded? || reflection.options[:finder_sql]
send(reflection.name).map(&:id)
else
@@ -1440,26 +1430,22 @@ def collection_accessor_methods(reflection, association_proxy_class, writer = tr
collection_reader_method(reflection, association_proxy_class)
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
association = send(reflection.name)
association.replace(new_value)
association
end
- method = "#{reflection.name.to_s.singularize}_ids="
- remove_possible_method(method)
- define_method(method) do |new_value|
+ redefine_method("#{reflection.name.to_s.singularize}_ids=") do |new_value|
ids = (new_value || []).reject { |nid| nid.blank? }.map(&:to_i)
send("#{reflection.name}=", reflection.klass.find(ids).index_by(&:id).values_at(*ids))
end
end
end
def association_constructor_method(constructor, reflection, association_proxy_class)
- method = "#{constructor}_#{reflection.name}"
- remove_possible_method(method)
- define_method(method) do |*params|
+ redefine_method("#{constructor}_#{reflection.name}") do |*params|
attributees = params.first unless params.empty?
replace_existing = params[1].nil? ? true : params[1]
association = association_instance_get(reflection.name)
@@ -1500,9 +1486,8 @@ def add_counter_cache_callbacks(reflection)
end
def add_touch_callbacks(reflection, touch_attribute)
- method_name = "belongs_to_touch_after_save_or_destroy_for_#{reflection.name}".to_sym
- remove_possible_method(method_name)
- define_method(method_name) do
+ method_name = :"belongs_to_touch_after_save_or_destroy_for_#{reflection.name}"
+ redefine_method(method_name) do
association = send(reflection.name)
if touch_attribute == true
@@ -871,7 +871,7 @@ def setup_fixture_accessors(table_names = nil)
table_names.each do |table_name|
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
@fixture_cache[table_name] ||= {}
@@ -105,7 +105,7 @@ def scope(name, scope_options = {}, &block)
extension ? relation.extending(extension) : relation
end
- singleton_class.send :define_method, name, &scopes[name]
+ singleton_class.send(:redefine_method, name, &scopes[name])
end
def named_scope(*args, &block)
@@ -101,6 +101,7 @@ def with_real_execute
#we need to actually modify some data, so we make execute point to the original method
ActiveRecord::ConnectionAdapters::MysqlAdapter.class_eval do
alias_method :execute_with_stub, :execute
+ remove_method :execute
alias_method :execute, :execute_without_stub
end
yield

0 comments on commit 661fd98

Please sign in to comment.