Question

I have upgraded a Rails 3.2 app from Ruby 1.9.3-p448 to 2.0.0-p451.

All the automated tests are passing, bar one, with the error:

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

The code here is a little involved, but essentially the subject_path method is provided because the EntityAssociationsResponder inherits from SimpleDelegator, and is initialized with current Rails controller, which implements subject_path as a protected method.

The method is protected so it doesn’t get picked up by Rails as a controller action.

This used to work fine. Has Ruby 2.0 changed this behaviour so only public methods are delegated? I can’t find any reference to such a change in the documentation.

Update:

To fix this error, I have subclassed SimpleDelegator like so:

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
Was it helpful?

Solution

Yes, there was a change and there's currently an opened issue about this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top