Ruby 2.0 changed the behavior of SimpleDelegator?

I upgraded the Rails 3.2 application from Ruby 1.9.3-p448 to 2.0.0-p451.

All automatic tests pass, bar one, with an error:

NameError: undefined local variable or method 'subject_path' for #...'<Administration::EntityAssociationsController::EntityAssociationsResponder:0x007fe007338d78>

The code here is a bit related, but essentially the method is subject_pathprovided, since it EntityAssociationsResponderinherits from SimpleDelegatorand is initialized by the current Rails controller, which implements subject_pathas a protected method.

The method is protected, so it does not receive Rails as a controller action.

This worked fine. Did Ruby 2.0 include this behavior, so only public methods are delegated? I can not find a link to such a change in the documentation.

Update:

To fix this error, I subclassed SimpleDelegatoras follows:

class Responder < SimpleDelegator

  # Override method_missing so protected methods can also be called.
  def method_missing(m, *args, &block)
    target = self.__getobj__
    begin
      if target.respond_to?(m) || target.protected_methods.include?(m)
        target.__send__(m, *args, &block)
      else
        super(m, *args, &block)
      end
    ensure
      $@.delete_if {|t| %r"\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}:"o =~ t} if $@
    end
  end

end
+4
1

, .

+3

Source: https://habr.com/ru/post/1531621/


All Articles