Closed tobiasbhn closed 2 years ago
Interestingly, if I comment out Spina::Part.register(Spina::Parts::User)
in the Initializer, the Error does not occur and any rails command, like rails c
, runs perfectly fine. Using the exact same Code Snippet Spina::Part.register(Spina::Parts::User)
in the rails console itself, it does work and it adds the custom Part to Spina::PARTS
. No NameError
here
Maybe there is something changed or wrong in the order of loading / require?
This is related to https://github.com/rails/rails/issues/40904 (I think!).
Actually I got an Deprecation warning for all custom Parts all time long in previous 6.1.4.1 Rails, which did not get displayed to console but in logs. It stated following:
DEPRECATION WARNING: Initialization autoloaded the constants Spina::Part, Spina::Parts, Spina::Parts::Base, and Spina::Parts::User.
Being able to do this is deprecated. Autoloading during initialization is going
to be an error condition in future versions of Rails.
Reloading does not reboot the application, and therefore code executed during
initialization does not run again. So, if you reload Spina::Part, for example,
the expected changes won't be reflected in that stale Class object.
These autoloaded constants have been unloaded.
In order to autoload safely at boot time, please wrap your code in a reloader
callback this way:
Rails.application.reloader.to_prepare do
# Autoload classes and modules needed at boot time here.
end
That block runs when the application boots, and every time there is a reload.
For historical reasons, it may run twice, so it has to be idempotent.
Check the "Autoloading and Reloading Constants" guide to learn more about how
Rails autoloads and reloads.
(called from <main> at /home/tobias/projects/nwdl-web/config/environment.rb:5)
For now I got it working as simple as its statet in the Deprecation Warning:
# config/intializers/spina_custom_parts.rb
# Load Custom Parts for Spina
Rails.application.reloader.to_prepare do
Spina::Part.register(Spina::Parts::User)
end
But I don't know if that is the intended way to do it in Rails (?), or whether something should be done differently according to convention. If Rails.application.reloader.to_prepare
is the final understanding, it should be corrected in the documentation.
I'm sorry, this is an error in our documentation. It was already edited in this guide: https://spinacms.com/guides/themes-content/how-to-create-custom-content-parts
You can use:
Rails.application.reloader.to_prepare do
Spina::Part.register(Spina::Parts::User)
end
I recently updated my application to Rails 7 (v7.0.2.2) using Ruby 3 (v3.0.0p0 revision 95aff21468) and got an
NameError
onSpina::Part
, whilst registering Custom Parts. It also happens in a new project. The Code which throws the Error worked fine before updating and was not changed during update.Environment
OS: Linux Ruby: 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux] Rails: 7.0.2.2 Bundler: version 2.2.3 Spina: 2.8.1
Steps to Reproduce
rails new spina-name-error --database=postgresql
gem 'spina', '~> 2.8.1'
to Gemfilebundle install
rails db:create
rails spina:install
user.rb
underapp/models/spina/parts/user.rb
Spina::Part.register(Spina::Parts::User)
toconfig/initializers/spina.rb
rails db:migrate
,rails s
,rails c
Expected Behaviour
The Spina installation is completed and any rails Command executes without any error.
Maybe Related SO
Found this SO which might be related, but no progress there. https://stackoverflow.com/questions/70655590/rails-7-uninitialized-constant-impressionistengineimpressionistcontroller
Complete Stacktrace
Content of user.rb