Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/alchemy/admin/passwords_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class PasswordsController < ::Devise::PasswordsController

helper "Alchemy::Admin::Base"

layout Alchemy::Devise.layout
layout Alchemy::Devise.config.layout

private

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/alchemy/admin/user_sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/alchemy/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/models/alchemy/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class User < ActiveRecord::Base
:tag_list
]

devise(*Alchemy.devise_modules)
devise(*Alchemy::Devise.config.devise_modules)

include Alchemy::Taggable

Expand Down
63 changes: 24 additions & 39 deletions lib/alchemy/devise.rb
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions lib/alchemy/devise/configuration.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/libraries/alchemy/devise_spec.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions spec/libraries/alchemy_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Loading