Navigation Menu

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes Rails autoloader deprecation warning #354

Merged
merged 4 commits into from Apr 8, 2021

Conversation

jairovm
Copy link
Contributor

@jairovm jairovm commented Apr 7, 2021

Fixes #331

@coveralls
Copy link

coveralls commented Apr 7, 2021

Coverage Status

Coverage increased (+0.1%) to 90.684% when pulling 6b3474d on jairovm:331---fix-deprecation-warning into e67a411 on merit-gem:master.

@tute
Copy link
Member

tute commented Apr 7, 2021

Thanks! The build now fails with:

NameError: undefined local variable or method `extend_orm_with_has_merit' for #<#<Class:0x0000000002a12e38>:0x0000000003c9a4c0 @locked=false>
/home/travis/build/merit-gem/merit/lib/merit.rb:65:in `block (2 levels) in <class:Engine>'

Can you make CI pass?

@jairovm
Copy link
Contributor Author

jairovm commented Apr 7, 2021

@tute tests passed, let me know if you want me to change something else, thanks

@tute
Copy link
Member

tute commented Apr 8, 2021

Thanks! I just tested it, but when I launch the rails console, I still see the warning in the log/development.log file. This doesn't seem to fix that issue.

@jairovm
Copy link
Contributor Author

jairovm commented Apr 8, 2021

Actually, I just re-read the related issue and it looks like there's another Deprecation Warning I wasn't aware of

DEPRECATION WARNING: Initialization autoloaded the constant Merit::Badge.

This PR fixes this deprecation message ( Merit::BadgeRules, and Merit::PointRules are the ones we care about ).

DEPRECATION WARNING: Initialization autoloaded the constants ActionText::ContentHelper, ActionText::TagHelper, 
Merit::BadgeRules, and Merit::PointRules.

In order to see that message ActiveSupport.on_load(action_controller_hook) event should be triggered, here is what I did:

rails new merit --skip-spring
cd merit

echo "ActionController::Base" > config/initializers/action_controller.rb
echo "gem 'merit', '4.0.1'" >> Gemfile
bundle install
rails g merit:install
rails db:migrate

rails runner 1 # warning message added to tmp/development.log

When doing the same on my fix, this is what I get ( Merit::BadgeRules, and Merit::PointRules are not there anymore )

DEPRECATION WARNING: Initialization autoloaded the constants ActionText::ContentHelper and ActionText::TagHelper.

I changed my Gemfile to pull merit gem from my github

gem 'merit', git: 'https://github.com/jairovm/merit.git', ref: '31bcf79'

@jairovm
Copy link
Contributor Author

jairovm commented Apr 8, 2021

Regarding of Merit::Badge deprecation warning, I think that's caused because this being added to an initializer

# config/initializers/merit.rb

Merit::Badge.create!(
  id: 1,
  name: "year-member",
  description: "Active member for a year",
  custom_fields: { difficulty: :silver }
)

It should look something like this ( as specified in the logs )

# config/initializers/merit.rb

Rails.application.reloader.to_prepare do
  Merit::Badge.create!(
    id: 1,
    name: "year-member",
    description: "Active member for a year",
    custom_fields: { difficulty: :silver }
  )
end

I can update the Readme if you want.

@burmashave
Copy link

I had the same problem and solved it exactly as @jairovm suggested: by wrapping everything with Rails.application.reloader.to_prepare

See: https://guides.rubyonrails.org/autoloading_and_reloading_constants.html#autoloading-when-the-application-boots

@tute
Copy link
Member

tute commented Apr 8, 2021

This PR fixes this deprecation message ( Merit::BadgeRules, and Merit::PointRules are the ones we care about ).

Got it, thanks! I saw it, good to merge already.

I can update the Readme if you want.

Yes, please! Want to do that as part of this PR or a new one?

Thank you both.

@jairovm
Copy link
Contributor Author

jairovm commented Apr 8, 2021

Just changed it within this PR 😅. Thanks @tute

@tute tute merged commit e33e34c into merit-gem:master Apr 8, 2021
@tute
Copy link
Member

tute commented Apr 8, 2021

Thank you!!

tute pushed a commit that referenced this pull request Apr 8, 2021
Fixes:
```
DEPRECATION WARNING: Initialization autoloaded the constants
ActionText::ContentHelper, ActionText::TagHelper, Merit::BadgeRules, and
Merit::PointRules
```

Also fixes `DEPRECATION WARNING: Initialization autoloaded the constant Merit::Badge` by:

- Wrapping `Merit::Badge.create` with
  `Rails.application.reloader.to_prepare` in `README.md`
- Adding `Rails.application.reloader.to_prepare` to `merit.erb` template
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

DEPRECATION WARNING: Initialization autoloaded the constant Merit::Badge.
4 participants