diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-09-12 22:12:12 +0100 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-09-13 00:01:58 +0100 |
commit | ac687ed651773fccecbc22cd6d8b07d5439ceb76 (patch) | |
tree | eba4a000c8053dbe32f648d6042a494a2d16095f /activemodel/lib/active_model | |
parent | cf115d2f8ef48764e095aa453f729b60705088f1 (diff) | |
download | rails-ac687ed651773fccecbc22cd6d8b07d5439ceb76.tar.gz rails-ac687ed651773fccecbc22cd6d8b07d5439ceb76.tar.bz2 rails-ac687ed651773fccecbc22cd6d8b07d5439ceb76.zip |
Let Ruby deal with method visibility.
Check respond_to_without_attributes? in method_missing. If there is any
method that responds (even private), let super handle it and raise
NoMethodError if necessary.
Diffstat (limited to 'activemodel/lib/active_model')
-rw-r--r-- | activemodel/lib/active_model/attribute_methods.rb | 24 |
1 files changed, 11 insertions, 13 deletions
diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index c5f7a21d3f..baebc91192 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -411,13 +411,18 @@ module ActiveModel # It's also possible to instantiate related objects, so a Client class # belonging to the clients table with a +master_id+ foreign key can # instantiate master through Client#master. - def method_missing(method_id, *args, &block) - method_name = method_id.to_s - if match = match_attribute_method?(method_name) - guard_private_attribute_method!(method_name, args) - return __send__(match.target, match.attr_name, *args, &block) + def method_missing(method, *args, &block) + if respond_to_without_attributes?(method, true) + super + else + match = match_attribute_method?(method.to_s) + + if match + __send__(match.target, match.attr_name, *args, &block) + else + super + end end - super end # A Person object with a name attribute can ask <tt>person.respond_to?(:name)</tt>, @@ -450,13 +455,6 @@ module ActiveModel match && attribute_method?(match.attr_name) ? match : nil end - # prevent method_missing from calling private methods with #send - def guard_private_attribute_method!(method_name, args) - if self.class.private_method_defined?(method_name) - raise NoMethodError.new("Attempt to call private method `#{method_name}'", method_name, args) - end - end - def missing_attribute(attr_name, stack) raise ActiveModel::MissingAttributeError, "missing attribute: #{attr_name}", stack end |