sorcery: Master version fails with "To use reset_password submodule, you must define a mailer"
Configuration
- Sorcery Version: c30cefa7
- Ruby Version:
2.5.1
- Framework:
Rails
, versions6.0.2.2
,5.2.4
- Platform:
Mac OS Mojave 10.14.6
Expected Behavior
No error To use reset_password submodule, you must define a mailer
in the rails development console
Actual Behavior
Any operation in the rails development console fails with
validate_mailer_defined': To use reset_password submodule, you must define a mailer (config.reset_password_mailer = YourMailerClass). (ArgumentError)
Steps to Reproduce
There is code snippet that reproduces the issue:
# issue_with_sorcery.rb
require "bundler/inline"
gemfile(false) do
gem 'rails', '6.0.2.2'
gem 'sorcery', github: 'Sorcery/sorcery', branch: 'master', require: false
gem 'sqlite3', '1.4.2'
end
require 'rails/all'
require 'sorcery'
module TestApp
class Application < Rails::Application
config.load_defaults 6.0
end
end
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table "users", force: :cascade do |t|
t.string "email", null: false
t.string "name"
end
end
Rails.application.config.sorcery.submodules = [:reset_password]
class UserMailer < ActionMailer::Base
def reset_password_email(user)
mail(to: user.email, subject: 'Welcome') do |f|
f.html { render plain: "Hi Dear #{user.name}" }
end
end
end
Rails.application.config.sorcery.configure do |config|
config.user_config do |user|
user.reset_password_mailer = UserMailer
config.user_class = "User"
end
end
class User < ActiveRecord::Base
authenticates_with_sorcery!
end
u = User.create(email: 'jane@example.org', name: 'Jane')
puts u.attributes
It is expected that this script will print user attribute values when you run it with ruby issue_with_sorcery.rb
but it fails with
~/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/bundler/gems/sorcery-c30cefa751c3/lib/sorcery/model/submodules/reset_password.rb:77:in `validate_mailer_defined': To use reset_password submodule, you must define a mailer (config.reset_password_mailer = YourMailerClass). (ArgumentError)
I started seeing this error after I upgraded to the master branch to fix deprecation warnings in Rails 6
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 21 (12 by maintainers)
Commits related to this issue
- Call sorcery config block where it is defined [#227] Before this commit sorcery config block was saved into a variable and called after including `Sorcery::Controller` into `ActionController::Base` (... — committed to Hirurg103/sorcery by Hirurg103 4 years ago
- Add back changes that fix deprecation warning related to autoloading in Rails 6 [#209] Deprecation warning "Initialization autoloaded the constants ActionText::ContentHelper and ActionText::TagHelper... — committed to Hirurg103/sorcery by Hirurg103 4 years ago
- Revert on_load change due to breaking existing applications (#234) — committed to Sorcery/sorcery by joshbuker 4 years ago
@athix absolutely. I’m actually in the middle of completely removing Sorcery and the UserMailer. I’m going drop and re-create my database, reinstall Sorcery, and see if the issue persists. If so, I’ll upload the config file. If this fixes it, I’ll let you know as well.
@athix looks like I didn’t answer your question. 0.15 does fix the issue for me, thanks. Don’t know why @revickulous2001 still has problems.
@athix Looks like this error occurs due to load order.
Loading
User
class in the development console causesauthenticates_with_sorcery!
to be called. I inspected and found out that at this stage::Sorcery::Controller::Config.user_config
is nil. This happens becauseActionController::Base
has not been loaded yet and thusSorcery :: Controller.included
has not been called. And thusConfig.configure!
has not been called. And therefore the configuration block in the sorcery initializer has not been evaluated.If I type the rails console
ActionController::Base
and thenUser
all works OK.Is there any reason you save sorcery config block in a variable and then evaluate it when
ActionController::Base
is loaded? Why not set all configuration options when sorcery initializer file evaluates?