Skip to content

Commit c00dc42

Browse files
authored
Merge pull request #236 from AlchemyCMS/alchemy-config
Use Alchemy::Configuration for config
2 parents 8a1be05 + 7c1e67a commit c00dc42

File tree

11 files changed

+117
-50
lines changed

11 files changed

+117
-50
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ Default Devise modules included in `Alchemy::User` model
4343
- `:timeoutable`
4444
- `:recoverable`
4545

46-
If you want to add additional modules into the Alchemy user class append them to `Alchemy.devise_modules` in an initializer in your app.
46+
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.
4747

4848
### Register additional modules example
4949

5050
```ruby
5151
# config/initializers/alchemy.rb
52-
Alchemy.devise_modules << :registerable
52+
Alchemy::Devise.config.devise_modules << :registerable
5353
```
5454

5555
### Using old encryption
@@ -58,7 +58,7 @@ If your app uses an old encryption that needs the +devise-encryptable+ gem you a
5858

5959
```ruby
6060
# config/initializers/alchemy.rb
61-
Alchemy.devise_modules << :encryptable
61+
Alchemy::Devise.config.devise_modules << :encryptable
6262
```
6363

6464
## Testing

app/controllers/alchemy/admin/passwords_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class PasswordsController < ::Devise::PasswordsController
99

1010
helper "Alchemy::Admin::Base"
1111

12-
layout Alchemy::Devise.layout
12+
layout Alchemy::Devise.config.layout
1313

1414
private
1515

app/controllers/alchemy/admin/user_sessions_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class UserSessionsController < ::Devise::SessionsController
1313

1414
helper "Alchemy::Admin::Base"
1515

16-
layout Alchemy::Devise.layout
16+
layout Alchemy::Devise.config.layout
1717

1818
def create
1919
authenticate_user!
@@ -49,7 +49,7 @@ def check_user_count
4949

5050
# Overwriting the default of Devise
5151
def after_sign_out_path_for(resource_or_scope)
52-
if request.referer.blank? || request.referer.to_s =~ /admin/
52+
if request.referer.blank? || request.referer.to_s.include?("admin")
5353
root_path
5454
else
5555
request.referer

app/controllers/alchemy/admin/users_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def signup_admin_or_redirect
9898
deliver_welcome_mail
9999
redirect_to admin_pages_path
100100
else
101-
render :signup, status: :unprocessable_entity
101+
render :signup, status: :unprocessable_content
102102
end
103103
end
104104

app/models/alchemy/user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class User < ActiveRecord::Base
1717
:tag_list
1818
]
1919

20-
devise(*Alchemy.devise_modules)
20+
devise(*Alchemy::Devise.config.devise_modules)
2121

2222
include Alchemy::Taggable
2323

lib/alchemy/devise.rb

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,33 @@
1+
require "alchemy/devise/configuration"
12
require "alchemy/devise/engine"
23

34
module Alchemy
4-
# Devise modules included in +Alchemy::User+ model
5-
#
6-
# === Default modules
7-
#
8-
# [
9-
# . :database_authenticatable,
10-
# :trackable,
11-
# :validatable,
12-
# :timeoutable,
13-
# :recoverable
14-
# . ]
15-
#
16-
# If you want to add additional modules into the Alchemy user class append
17-
# them to this collection in an initializer in your app.
18-
#
19-
# === Example
20-
#
21-
# # config/initializers/alchemy.rb
22-
# Alchemy.devise_modules << :registerable
23-
#
24-
# If your app uses an old encryption that needs the +devise-encryptable+ gem
25-
# you also need to load the devise module.
26-
#
27-
# Alchemy.devise_modules << :encryptable
28-
#
29-
def self.devise_modules
30-
@devise_modules ||= [
31-
:database_authenticatable,
32-
:trackable,
33-
:validatable,
34-
:timeoutable,
35-
:recoverable
36-
]
37-
end
38-
395
module Devise
40-
def self.layout=(value)
41-
@layout = value
6+
extend self
7+
8+
def deprecator
9+
ActiveSupport::Deprecation.new("9.0", "Alchemy::Devise")
4210
end
4311

44-
def self.layout
45-
@layout || "alchemy/admin"
12+
delegate :layout=, to: :config
13+
deprecate "layout=": "Use `Alchemy::Devise.config.layout=` instead.",
14+
deprecator: Alchemy::Devise.deprecator
15+
16+
delegate :layout, to: :config
17+
deprecate layout: "Use `Alchemy::Devise.config.layout` instead.",
18+
deprecator: Alchemy::Devise.deprecator
19+
20+
def config
21+
@config ||= Alchemy::Devise::Configuration.new
22+
end
23+
24+
def configure(&blk)
25+
yield config
4626
end
4727
end
28+
29+
extend self
30+
31+
deprecate devise_modules: "Alchemy::Devise.config.devise_modules", deprecator: Alchemy::Devise.deprecator
32+
delegate :devise_modules, to: "Alchemy::Devise.config"
4833
end
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
require "alchemy/configuration"
2+
3+
module Alchemy
4+
module Devise
5+
class Configuration < Alchemy::Configuration
6+
# Layout for Alchemy Devise controllers
7+
# Default is +alchemy/admin+
8+
option :layout, :string, default: "alchemy/admin"
9+
10+
# Devise modules included in +Alchemy::User+ model
11+
#
12+
# === Default modules
13+
#
14+
# [
15+
# :database_authenticatable,
16+
# :trackable,
17+
# :validatable,
18+
# :timeoutable,
19+
# :recoverable
20+
# ]
21+
#
22+
# If you want to add additional modules into the Alchemy user class append
23+
# them to this collection in an initializer in your app.
24+
#
25+
# === Example
26+
#
27+
# # config/initializers/alchemy.rb
28+
# Alchemy::Devise.config.devise_modules << :registerable
29+
#
30+
# If your app uses an old encryption that needs the +devise-encryptable+ gem
31+
# you also need to load the devise module.
32+
#
33+
# Alchemy::Devise.config.devise_modules << :encryptable
34+
#
35+
option :devise_modules, :collection, collection_class: Set, item_type: :symbol, default: [
36+
:database_authenticatable,
37+
:trackable,
38+
:validatable,
39+
:timeoutable,
40+
:recoverable
41+
]
42+
end
43+
end
44+
end

lib/generators/alchemy/devise/install/templates/devise.rb.tt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ Devise.setup do |config|
302302
# apps is `200 OK` and `302 Found` respectively, but new apps are generated with
303303
# these new defaults that match Hotwire/Turbo behavior.
304304
# Note: These might become the new default in future versions of Devise.
305-
config.responder.error_status = :unprocessable_entity
305+
config.responder.error_status = :unprocessable_content
306306
config.responder.redirect_status = :see_other
307307

308308
# ==> Configuration for :registerable
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Alchemy::Devise do
4+
describe ".layout" do
5+
it "delegates to Alchemy::Devise.config.layout" do
6+
Alchemy::Devise.deprecator.silenced do
7+
expect(Alchemy::Devise.layout).to eq(Alchemy::Devise.config.layout)
8+
end
9+
end
10+
11+
it "is deprecated" do
12+
expect_any_instance_of(ActiveSupport::Deprecation).to receive(:warn).with(
13+
a_string_including("layout is deprecated"),
14+
an_instance_of(Array)
15+
)
16+
Alchemy::Devise.layout
17+
end
18+
end
19+
end

spec/libraries/alchemy_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
require "rails_helper"
2+
3+
RSpec.describe Alchemy do
4+
describe ".devise_modules" do
5+
it "delegates to Alchemy::Devise.config.devise_modules" do
6+
Alchemy::Devise.deprecator.silenced do
7+
expect(Alchemy.devise_modules).to eq(Alchemy::Devise.config.devise_modules)
8+
end
9+
end
10+
11+
it "is deprecated" do
12+
expect_any_instance_of(ActiveSupport::Deprecation).to receive(:warn).with(
13+
a_string_including("devise_modules is deprecated"),
14+
an_instance_of(Array)
15+
)
16+
Alchemy.devise_modules
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)