Skip to content

Rails 5 : Syntax fix recursive loop where prepend-ed modules exist #224

@simon-smartdata

Description

@simon-smartdata

HI
in_controller.rb line 600 ish
ancestors[1..-1] is the issue.

protected
     def filter_access_permissions # :nodoc:
        unless filter_access_permissions?
         ancestors[1..-1].reverse.each do |mod|
           mod.filter_access_permissions if mod.respond_to?(:filter_access_permissions, true)
         end
       end
       class_variable_set(:@@declarative_authorization_permissions, {}) unless filter_access_permissions?
       class_variable_get(:@@declarative_authorization_permissions)[self.name] ||= []
     end

This is a recursive method and ancestors[1..-1] appears to be designed to remove the current object's class from the iterator, with the assumption that the current object is its own first ancestor.
eg if ancestors returns
[ ActionController::Base, ActiveRecord::Railties::ControllerRuntime, ....... ]
[1..-1] removes the first element which is assumed to be the current object, in this case ActionController::Base.

However this fails if other gem modules (eg wicked_pdf) are designed to prepend to the front of the ancestors chain.

See Nice Module#prepend explanation here

For example: prepend excerpt from the wicked_pdf gem:

class WickedPdf
  if defined?(Rails)
    if Rails::VERSION::MAJOR >= 5
      class WickedRailtie < Rails::Railtie
        initializer 'wicked_pdf.register' do |_app|
          ActionController::Base.send :prepend, PdfHelper
          ActionView::Base.send :include, WickedPdfHelper::Assets
        end
      end

ancestors returns
[WickedPdf::PdfHelper, ActionController::Base, ActiveRecord::Railties::ControllerRuntime, .......
[1..-1] removes the first element, in this case the prepend-ed WickedPdf::PdfHelper, leaving the current object ( ActionController::Base) in the iterator which causes the infinite loop.

FIX: Replace

ancestors[1..-1].reverse.each do |mod|

WITH

(ancestors - [self] ).reverse.each do |mod|

Thanks
Simon

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions