diff --git a/README.md b/README.md index 1c21c061..57336382 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,13 @@ Default Devise modules included in `Alchemy::User` model - `:timeoutable` - `:recoverable` -If you want to add additional modules into the Alchemy user class append them to `Alchemy.devise_modules` in an initializer in your app. +If you want to add additional modules into the Alchemy user class append them to `Alchemy::Devise.config.devise_modules` in an initializer in your app. ### Register additional modules example ```ruby # config/initializers/alchemy.rb -Alchemy.devise_modules << :registerable +Alchemy::Devise.config.devise_modules << :registerable ``` ### Using old encryption @@ -58,7 +58,7 @@ If your app uses an old encryption that needs the +devise-encryptable+ gem you a ```ruby # config/initializers/alchemy.rb -Alchemy.devise_modules << :encryptable +Alchemy::Devise.config.devise_modules << :encryptable ``` ## Testing diff --git a/app/controllers/alchemy/admin/passwords_controller.rb b/app/controllers/alchemy/admin/passwords_controller.rb index 61903cdb..29926bdf 100644 --- a/app/controllers/alchemy/admin/passwords_controller.rb +++ b/app/controllers/alchemy/admin/passwords_controller.rb @@ -9,7 +9,7 @@ class PasswordsController < ::Devise::PasswordsController helper "Alchemy::Admin::Base" - layout Alchemy::Devise.layout + layout Alchemy::Devise.config.layout private diff --git a/app/controllers/alchemy/admin/user_sessions_controller.rb b/app/controllers/alchemy/admin/user_sessions_controller.rb index 8ebcc057..4e9030d9 100644 --- a/app/controllers/alchemy/admin/user_sessions_controller.rb +++ b/app/controllers/alchemy/admin/user_sessions_controller.rb @@ -13,7 +13,7 @@ class UserSessionsController < ::Devise::SessionsController helper "Alchemy::Admin::Base" - layout Alchemy::Devise.layout + layout Alchemy::Devise.config.layout def create authenticate_user! @@ -49,7 +49,7 @@ def check_user_count # Overwriting the default of Devise def after_sign_out_path_for(resource_or_scope) - if request.referer.blank? || request.referer.to_s =~ /admin/ + if request.referer.blank? || request.referer.to_s.include?("admin") root_path else request.referer diff --git a/app/controllers/alchemy/admin/users_controller.rb b/app/controllers/alchemy/admin/users_controller.rb index c82035cc..e369e234 100644 --- a/app/controllers/alchemy/admin/users_controller.rb +++ b/app/controllers/alchemy/admin/users_controller.rb @@ -98,7 +98,7 @@ def signup_admin_or_redirect deliver_welcome_mail redirect_to admin_pages_path else - render :signup, status: :unprocessable_entity + render :signup, status: :unprocessable_content end end diff --git a/app/models/alchemy/user.rb b/app/models/alchemy/user.rb index 91e385d3..069f4d70 100644 --- a/app/models/alchemy/user.rb +++ b/app/models/alchemy/user.rb @@ -17,7 +17,7 @@ class User < ActiveRecord::Base :tag_list ] - devise(*Alchemy.devise_modules) + devise(*Alchemy::Devise.config.devise_modules) include Alchemy::Taggable diff --git a/lib/alchemy/devise.rb b/lib/alchemy/devise.rb index 1a304ebb..13b2d3ba 100644 --- a/lib/alchemy/devise.rb +++ b/lib/alchemy/devise.rb @@ -1,48 +1,33 @@ +require "alchemy/devise/configuration" require "alchemy/devise/engine" module Alchemy - # Devise modules included in +Alchemy::User+ model - # - # === Default modules - # - # [ - # . :database_authenticatable, - # :trackable, - # :validatable, - # :timeoutable, - # :recoverable - # . ] - # - # If you want to add additional modules into the Alchemy user class append - # them to this collection in an initializer in your app. - # - # === Example - # - # # config/initializers/alchemy.rb - # Alchemy.devise_modules << :registerable - # - # If your app uses an old encryption that needs the +devise-encryptable+ gem - # you also need to load the devise module. - # - # Alchemy.devise_modules << :encryptable - # - def self.devise_modules - @devise_modules ||= [ - :database_authenticatable, - :trackable, - :validatable, - :timeoutable, - :recoverable - ] - end - module Devise - def self.layout=(value) - @layout = value + extend self + + def deprecator + ActiveSupport::Deprecation.new("9.0", "Alchemy::Devise") end - def self.layout - @layout || "alchemy/admin" + delegate :layout=, to: :config + deprecate "layout=": "Use `Alchemy::Devise.config.layout=` instead.", + deprecator: Alchemy::Devise.deprecator + + delegate :layout, to: :config + deprecate layout: "Use `Alchemy::Devise.config.layout` instead.", + deprecator: Alchemy::Devise.deprecator + + def config + @config ||= Alchemy::Devise::Configuration.new + end + + def configure(&blk) + yield config end end + + extend self + + deprecate devise_modules: "Alchemy::Devise.config.devise_modules", deprecator: Alchemy::Devise.deprecator + delegate :devise_modules, to: "Alchemy::Devise.config" end diff --git a/lib/alchemy/devise/configuration.rb b/lib/alchemy/devise/configuration.rb new file mode 100644 index 00000000..a4fbc803 --- /dev/null +++ b/lib/alchemy/devise/configuration.rb @@ -0,0 +1,44 @@ +require "alchemy/configuration" + +module Alchemy + module Devise + class Configuration < Alchemy::Configuration + # Layout for Alchemy Devise controllers + # Default is +alchemy/admin+ + option :layout, :string, default: "alchemy/admin" + + # Devise modules included in +Alchemy::User+ model + # + # === Default modules + # + # [ + # :database_authenticatable, + # :trackable, + # :validatable, + # :timeoutable, + # :recoverable + # ] + # + # If you want to add additional modules into the Alchemy user class append + # them to this collection in an initializer in your app. + # + # === Example + # + # # config/initializers/alchemy.rb + # Alchemy::Devise.config.devise_modules << :registerable + # + # If your app uses an old encryption that needs the +devise-encryptable+ gem + # you also need to load the devise module. + # + # Alchemy::Devise.config.devise_modules << :encryptable + # + option :devise_modules, :collection, collection_class: Set, item_type: :symbol, default: [ + :database_authenticatable, + :trackable, + :validatable, + :timeoutable, + :recoverable + ] + end + end +end diff --git a/lib/generators/alchemy/devise/install/templates/devise.rb.tt b/lib/generators/alchemy/devise/install/templates/devise.rb.tt index dec72827..36dbb4a1 100644 --- a/lib/generators/alchemy/devise/install/templates/devise.rb.tt +++ b/lib/generators/alchemy/devise/install/templates/devise.rb.tt @@ -302,7 +302,7 @@ Devise.setup do |config| # apps is `200 OK` and `302 Found` respectively, but new apps are generated with # these new defaults that match Hotwire/Turbo behavior. # Note: These might become the new default in future versions of Devise. - config.responder.error_status = :unprocessable_entity + config.responder.error_status = :unprocessable_content config.responder.redirect_status = :see_other # ==> Configuration for :registerable diff --git a/spec/libraries/alchemy/devise_spec.rb b/spec/libraries/alchemy/devise_spec.rb new file mode 100644 index 00000000..79b0d3fe --- /dev/null +++ b/spec/libraries/alchemy/devise_spec.rb @@ -0,0 +1,19 @@ +require "rails_helper" + +RSpec.describe Alchemy::Devise do + describe ".layout" do + it "delegates to Alchemy::Devise.config.layout" do + Alchemy::Devise.deprecator.silenced do + expect(Alchemy::Devise.layout).to eq(Alchemy::Devise.config.layout) + end + end + + it "is deprecated" do + expect_any_instance_of(ActiveSupport::Deprecation).to receive(:warn).with( + a_string_including("layout is deprecated"), + an_instance_of(Array) + ) + Alchemy::Devise.layout + end + end +end diff --git a/spec/libraries/alchemy_spec.rb b/spec/libraries/alchemy_spec.rb new file mode 100644 index 00000000..9c97ef80 --- /dev/null +++ b/spec/libraries/alchemy_spec.rb @@ -0,0 +1,19 @@ +require "rails_helper" + +RSpec.describe Alchemy do + describe ".devise_modules" do + it "delegates to Alchemy::Devise.config.devise_modules" do + Alchemy::Devise.deprecator.silenced do + expect(Alchemy.devise_modules).to eq(Alchemy::Devise.config.devise_modules) + end + end + + it "is deprecated" do + expect_any_instance_of(ActiveSupport::Deprecation).to receive(:warn).with( + a_string_including("devise_modules is deprecated"), + an_instance_of(Array) + ) + Alchemy.devise_modules + end + end +end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 432d12fe..eb369253 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -284,7 +284,7 @@ module Alchemy describe "#logged_in?" do let(:user) { build(:alchemy_user) } - before { stub_alchemy_config(:auto_logout_time, 60) } + before { stub_alchemy_config(auto_logout_time: 60) } it "should return logged in status" do user.last_request_at = 30.minutes.ago @@ -296,7 +296,7 @@ module Alchemy describe "#logged_out?" do let(:user) { build(:alchemy_user) } - before { stub_alchemy_config(:auto_logout_time, 60) } + before { stub_alchemy_config(auto_logout_time: 60) } it "should return logged in status" do user.last_request_at = 2.hours.ago