43551

Rails 6+, zeitwerk autoloader and namedspaced constants

<h3>Question</h3>

The Rails 6+ default autoloader is zeitwerk, which seems like a great improvement over previous approaches.

However, zeitwork follows the convention for Rails projects that anything in app/* is autoloaded and doesn't need to be namespaced.

This works great for app/models/user.rb because you don't have to use Models::User but can just reference User.

However, I added my own app/services directory and I namespace my service objects as Services::Users::Create, which would map to app/services/users/create.rb.

Zeitwork is throwing errors that my class constants don't exist, since it is expecting Users::Create (without the Services:: prefix).

Is there anyway to configure zeitwork to require the Services:: namespace in these instances? In my opinion, it is a much cleaner to read the code as Services::Users::Create and know that you're looking in the app/services/users/create.rb file.

If you just had Users::Create, an average Rails developer would probably look for the app/models/users/create.rb file.

I don't like the approach of naming it Users::CreateService, it just seems very inelegant to me.

I can't be the only one who uses conventions like this; has anyone else come across a solution? I'm still going through all the zeitwerk documentation looking for a solution but haven't found one yet.


<h3>Answer1:</h3>

https://guides.rubyonrails.org/upgrading_ruby_on_rails.html#having-app-in-the-autoload-paths

Some projects want something like app/api/base.rb to define API::Base, and add app to the autoload paths to accomplish that in classic mode. Since Rails adds all subdirectories of app to the autoload paths automatically, we have another situation in which there are nested root directories, so that setup no longer works. Similar principle we explained above with concerns.

If you want to keep that structure, you'll need to delete the subdirectory from the autoload paths in an initializer:

ActiveSupport::Dependencies.autoload_paths.delete("#{Rails.root}/app/api")

来源:https://stackoverflow.com/questions/58981862/rails-6-zeitwerk-autoloader-and-namedspaced-constants

Recommend

  • how to execute shell script in the same process in python [closed]
  • How to stop the python turtle from drawing
  • “*Certificate not found* (from client)” when calling Web Service that requires signing
  • Why doesn't this class property serialize?
  • Is js executed after form synchronized submit
  • Moving Circle on Live Wallpaper
  • Xamarin.Forms: How To Populate A Pie Chart From Web API Data?
  • Display a custom text if user has already bought the current product In Woocommerce
  • WooCommerce get order quantity in thank you page and redirect
  • can I build CMakeLists.txt from a set of smaller files (to improve the readability and maintainabili
  • configure openjpa on to spring boot
  • Special characters (ë) in JSON-response
  • Finding all XML nodes between each two processing instructions
  • ROR + MVC Disable Browser Cache
  • async GET request with body from browser
  • VS2010: Ctrl-PgUp / -PgDown like in browsers
  • How to display youtube video on HTML page? [closed]
  • I need a PHP mySQL Login script that directs user to specific URL based on username and password
  • Get name of days between two date in ios?
  • How to display content depending on dropdown menue user selection
  • git clone, upload-pack out of memory
  • Javascript inside HTML import not affecting imported HTML
  • Does hibernate load two seprate copies of same instance if they are loaded twice from database?
  • Julia 1.0 UndefVarError - Scope of Variable
  • How to use Kaminari pagination gem with Sinatra and Mongoid?
  • Ruby regex for matching simpliest Ruby's regexes
  • Is there a better way for handling SpatialPolygons that cross the antimeridian (date line)?
  • DataTables move rows between tables
  • How to call jQuery function in HTML returned by AJAX
  • How do I add a mouse over tooltip to an Image using .DrawImage()
  • How to integrate angular2-material (alpha 8.2) with angular2-Quickstart app
  • How to warp text around image in iOS?
  • Send array to next viewcontroller iOs xcode [duplicate]
  • Codeigniniter insert data through models and controller
  • Sql - ON DUPLICATE KEY UPDATE
  • Jersey serializes character value to ASCII equivalent numeric string
  • PHP Permalinks.. how to change?
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • Creating random wired topology for given arbitrary number of nodes on NS2
  • convert json to excel in java