mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
AS guide: documents Module#redefine_method
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user