aboutsummaryrefslogtreecommitdiffstats
path: root/railties/guides/source/active_support_core_extensions.textile
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-08-10 03:59:26 +0200
committerXavier Noria <fxn@hashref.com>2010-08-10 04:10:32 +0200
commit5fc8af4712ea5b5f7a2ee8bedb3b431901de814d (patch)
tree618b8f9aaa43947623adc0c39d73f4d68c6dcf34 /railties/guides/source/active_support_core_extensions.textile
parentae147b45bb4aa2168e104ffc989e42f5c9dbfd80 (diff)
downloadrails-5fc8af4712ea5b5f7a2ee8bedb3b431901de814d.tar.gz
rails-5fc8af4712ea5b5f7a2ee8bedb3b431901de814d.tar.bz2
rails-5fc8af4712ea5b5f7a2ee8bedb3b431901de814d.zip
AS guide: documents Module#redefine_method
Diffstat (limited to 'railties/guides/source/active_support_core_extensions.textile')
-rw-r--r--railties/guides/source/active_support_core_extensions.textile21
1 files changed, 21 insertions, 0 deletions
diff --git a/railties/guides/source/active_support_core_extensions.textile b/railties/guides/source/active_support_core_extensions.textile
index e0c69516cf..31433d50eb 100644
--- a/railties/guides/source/active_support_core_extensions.textile
+++ b/railties/guides/source/active_support_core_extensions.textile
@@ -1078,6 +1078,27 @@ self.field_helpers = (FormHelper.instance_method_names - ['form_for'])
NOTE: Defined in +active_support/core_ext/module/method_names.rb+
+h4. Redefining Methods
+
+There are cases where you need to define a method with +define_method+, but don't know whether a method with that name already exists. If it does, a warning is issued if they are enabled. No big deal, but not clean either.
+
+The method +redefine_method+ prevents such a potential warning, removing the existing method before if needed. Rails uses it in a few places, for instance when it generates an association's API:
+
+<ruby>
+redefine_method("#{reflection.name}=") do |new_value|
+ association = association_instance_get(reflection.name)
+
+ if association.nil? || association.target != new_value
+ association = association_proxy_class.new(self, reflection)
+ end
+
+ association.replace(new_value)
+ association_instance_set(reflection.name, new_value.nil? ? nil : association)
+end
+</ruby>
+
+NOTE: Defined in +active_support/core_ext/module/remove_method.rb+
+
h3. Extensions to +Class+
h4. Class Attributes