Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 16, 2019 09:27 pm GMT

Rails Auto Loading Magic Gone Wrong!

TL;DR, use explicit module namespacing or avoid complex namespacing as often as possible when inheriting classes.

Class and Module constants depend on the order of autoload-paths lookup (a Ruby on Rails feature), more on that below. Because of this, class discoverability can vary depending on the name and location of an inheriting class. When multiple classes share the same name, as is the case in our Rails app, this subtlety can impact implicit class inheritance.

Ruby on Rails has two main algorithms for constant lookup in its autoload_paths (by default all subdirectories of app/ in the application and engines present at boot time, and app/*/concern).

The values of autoload_paths can be inspected with the following command:

$ bin/rails r 'puts ActiveSupport::Dependencies.autoload_paths'.../app/assets.../app/controllers.../app/helpers.../app/mailers.../app/models.../app/resources # This is loaded before models/ .../app/controllers/concerns.../app/models/concerns.../test/mailers/previews

In my attempt to clean up some of our code, I unwittingly created a difficult bug to spot. Here is some example code to see the issue in action:

# app/models/campaign.rb # This class is completely irrelevant to our code but has the same class name as the other campaign class.class Campaign; end # app/resources/homepage/apple.rbmodule Resources  module Homepage    class Apple < Campaign      campaign_method!  # This fails because apple.rb is loaded before resources/homepage/campaign.rb     end  endend# app/resources/homepage/campaign.rbmodule Resources  module Homepage    class Campaign      def self.campaign_method!        "I should be callable"      end    end  endend# app/resources/homepage/yolo.rbmodule Resources  module Homepage    class Yolo < Resources::Homepage::Campaign       campaign_method!  # This works because the parent class is unambiguous    end  endend# app/resources/homepage/zolo.rbmodule Resources  module Homepage    class Zolo < Campaign       campaign_method!  # This works by luck because Zolo is loaded alphabetically after resources/homepage/campaign.rb    end  endend

I was able to debug this issue using rails console

pry(main)> Resources::Homepage::Yolo # returns a constant=> Resources::Homepage::Yolopry(main)> Resources::Homepage::Apple # returns an error messageNameError: undefined local variable or method `campaign_method!' for Resources::Homepage::Apple:Class`

After changing class Apple < Campaign to class Apple < Resources::Homepage::Campaign and reloading the console, the code loads correctly.

Explicitly loading the required file also works, which is what Rails AutoLoader (usually) does correctly for you. The Rails Guides discourages using require or require_relative for autoloaded files. Instead, use require_dependency

# resources/homepage/apple.rbrequire_dependency 'resources/homepage/campaign' module Resources  module Homepage    class Apple < Campaign      campaign_method!  # this works and loads campaign's class method    end  endend

Hopefully, Ruby on Rails autoloading is clearer now. You can always read more and improve Ruby on Rails documentation here. If you have any questions or found errors, please comment here or contact me on twitter!
In Rails 5, autoloading is disabled by default in production.


Original Link: https://dev.to/shushugah/rails-auto-loading-magic-gone-wrong-19kp

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To