Comments (4)
Here's a monkey patch we are using that find classes based on zeitwerk loader.
Our use case is for loading classes within namespaces. I'm not sure if it works with collapsed dirs, but maybe you can modify it to work.
Put in an initializer:
# frozen_string_literal: true
if Rails.env.development?
require 'annotate'
module AnnotateModelsWithZeitwerk
def get_model_class(file)
loader = Rails.autoloaders.main
root_dirs = loader.dirs(namespaces: true) # or `root_dirs = loader.root_dirs` with zeitwerk < 2.6.1
expanded_file = File.expand_path(file)
root_dir, namespace = root_dirs.find do |dir, _|
expanded_file.start_with?(dir)
end
_, filepath_relative_to_root_dir = expanded_file.split(root_dir)
filepath_relative_to_root_dir = filepath_relative_to_root_dir[1..].sub(/\.rb$/, '')
camelize = loader.inflector.camelize(filepath_relative_to_root_dir, nil)
namespace.const_get(camelize)
end
end
module AnnotateModels
class << self
prepend AnnotateModelsWithZeitwerk
end
end
end
from annotate_models.
I found that fork of this gem has solved those issues: https://github.com/drwl/annotaterb
I understand fork was done since this gem is unmaintained quite a long time now (2 years without release): https://www.reddit.com/r/rails/comments/13keyfm/i_spent_the_past_3_months_working_on_a_fork_of/
Nearly drop in replacement, but configuration file needs to be created instead of configuring in rake task.
from annotate_models.
I also ran into this issue earlier and had to adjust the example above slightly.
We're using collapsed directories as such:
# within config/application.rb
Rails.autoloaders.main.collapse("#{Rails.root}/app/domains/domain_name/models")
Rails.autoloaders.main.collapse("#{Rails.root}/app/domains/domain_name/controllers")
Rails.autoloaders.main.collapse("#{Rails.root}/app/domains/domain_name/lib")
Based on this, I adjusted the sample above to address const loading issues:
module AnnotateModelsWithZeitwerk
def get_model_class(file)
loader = Rails.autoloaders.main
root_dirs = loader.dirs(namespaces: true) # or `root_dirs = loader.root_dirs` with zeitwerk < 2.6.1
expanded_file = File.expand_path(file)
root_dir, namespace = root_dirs.find do |dir, _|
expanded_file.start_with?(dir)
end
_, filepath_relative_to_root_dir = expanded_file.split(root_dir)
filepath_relative_to_root_dir = filepath_relative_to_root_dir[1..].sub(/\.rb$/, "")
# changed
# once we have the filepath_relative_to_root_dir, we need to see if it
# falls within one of our Zeitwerk "collapsed" paths.
if loader.collapse.any? { |path| path.include?(root_dir) && file.include?(path.split(root_dir)[1]) }
# if the file is within a collapsed path, we then need to, for each
# collapsed path, remove the root dir
collapsed = loader.collapse.map { |path| path.split(root_dir)[1].sub(/^\//, "") }.to_set
collapsed.each do |collapse|
# next, we split the collapsed directory, e.g. `domain_name/models`, by
# slash, and discard the domain_name
_, *collapsed_namespace = collapse.split("/")
# if there are any collapsed namespaces, e.g. `models`, we then remove
# that from `filepath_relative_to_root_dir`.
#
# This would result in:
#
# previous filepath_relative_to_root_dir: domain_name/models/model_name
# new filepath_relative_to_root_dir: domain_name/model_name
if collapsed_namespace.any?
filepath_relative_to_root_dir.sub!("/#{collapsed_namespace.last}", "")
end
end
end
# end changed
camelize = loader.inflector.camelize(filepath_relative_to_root_dir, nil)
namespace.const_get(camelize)
end
end
from annotate_models.
@joshuaclayton @bazay, I took a stab at adding zeitwerk support in my fork, if you want to know more: drwl/annotaterb#82
from annotate_models.
Related Issues (20)
- Next release? HOT 2
- Active Admin not working when running migrations
- Is there any way to ignore attributes?
- Reformat Column comments HOT 1
- Allowing --frozen to show the diff in the CI
- Unable to annotate: ActiveRecord::Encryption::Errors::Decryption HOT 4
- Please add a ruby version constraint to gemfile
- How should I set up a CI job to ensure that annotations are up to date? HOT 3
- Column wrongly mistaken as a geometric point
- Fix code scanning alert - Polynomial regular expression used on uncontrolled data
- Rails 7, annotate --routes failed HOT 6
- Can't execute `annotate` or `bundle exec annotate` commands HOT 2
- Unable to annotate a single model HOT 1
- Make `with_comment_column` work with `Annotate.set_defaults` HOT 7
- Configuration for custom spec directory
- Does not find factories with default suffix example
- annotate_models doesn't auto-annotate on down / rollback migrations in multi-DB environments
- Throws error when using composite primary keys (supported natively by rails 7.1) HOT 1
- Output columns in database schema order, not A-Z order. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from annotate_models.