Skip to content

Commit 1b0afb4

Browse files
committed
Convert auth_accessors.rb into config object
Creates a configuration object for authentication that can be initialized outside of a `config.to_prepare` block. Configuration is now in `Alchemy.config.auth`.
1 parent 0e55171 commit 1b0afb4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+459
-334
lines changed

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,15 @@ The best practice is to use an initializer:
146146

147147
```ruby
148148
# config/initializers/alchemy.rb
149-
Alchemy.user_class_name = 'YourUserClass' # Defaults to 'User'
150-
Alchemy.current_user_method = 'current_admin_user' # Defaults to 'current_user'
151-
Alchemy.signup_path = '/your/signup/path' # Defaults to '/signup'
152-
Alchemy.login_path = '/your/login/path' # Defaults to '/login'
153-
Alchemy.logout_path = '/your/logout/path' # Defaults to '/logout'
154-
Alchemy.logout_method = 'http_verb_for_logout' # Defaults to 'delete'
155-
Alchemy.unauthorized_path = '/some/public/page' # Defaults to '/'
149+
Alchemy.configure do |config|
150+
config.user_class = 'YourUserClass' # This has to be configured
151+
config.current_user_method = 'current_admin_user' # Defaults to 'current_user'
152+
config.signup_path = '/your/signup/path' # Defaults to '/signup'
153+
config.login_path = '/your/login/path' # Defaults to '/login'
154+
config.logout_path = '/your/logout/path' # Defaults to '/logout'
155+
config.logout_method = 'http_verb_for_logout' # Defaults to 'delete'
156+
config.unauthorized_path = '/some/public/page' # Defaults to '/'
157+
end
156158
```
157159

158160
The only thing Alchemy needs to know from your user class is the `alchemy_roles` method.

app/controllers/alchemy/admin/base_controller.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ def show_error_notice(error)
8080
end
8181

8282
def set_stamper
83-
if Alchemy.user_class.respond_to?(:stamper=)
84-
Alchemy.user_class.stamper = current_alchemy_user
83+
if Alchemy.config.user_class.respond_to?(:stamper=)
84+
Alchemy.config.user_class.stamper = current_alchemy_user
8585
end
8686
end
8787

8888
def reset_stamper
89-
if Alchemy.user_class.respond_to?(:reset_stamper)
90-
Alchemy.user_class.reset_stamper
89+
if Alchemy.config.user_class.respond_to?(:reset_stamper)
90+
Alchemy.config.user_class.reset_stamper
9191
end
9292
end
9393

app/controllers/alchemy/admin/dashboard_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ class DashboardController < Alchemy::Admin::BaseController
1111
def index
1212
@last_edited_pages = Page.all_last_edited_from(current_alchemy_user)
1313
@all_locked_pages = Page.locked
14-
if Alchemy.user_class.respond_to?(:logged_in)
15-
@online_users = Alchemy.user_class.logged_in.to_a - [current_alchemy_user]
14+
if Alchemy.config.user_class.respond_to?(:logged_in)
15+
@online_users = Alchemy.config.user_class.logged_in.to_a - [current_alchemy_user]
1616
end
1717
if current_alchemy_user.respond_to?(:sign_in_count) && current_alchemy_user.respond_to?(:last_sign_in_at)
1818
@last_sign_at = current_alchemy_user.last_sign_in_at

app/controllers/alchemy/base_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def handle_redirect_for_user
6767
if can?(:index, :alchemy_admin_dashboard)
6868
redirect_or_render_notice
6969
else
70-
redirect_to Alchemy.unauthorized_path
70+
redirect_to Alchemy.config.unauthorized_path
7171
end
7272
end
7373

@@ -95,7 +95,7 @@ def handle_redirect_for_guest
9595
render :permission_denied
9696
else
9797
store_location
98-
redirect_to Alchemy.login_path
98+
redirect_to Alchemy.config.login_path
9999
end
100100
end
101101

app/controllers/alchemy/pages_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,8 @@ def caching_options
208208
end
209209

210210
def signup_required?
211-
if Alchemy.user_class.respond_to?(:admins)
212-
Alchemy.user_class.admins.empty?
211+
if Alchemy.config.user_class.respond_to?(:admins)
212+
Alchemy.config.user_class.admins.empty?
213213
end
214214
end
215215

app/models/alchemy/attachment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Attachment < BaseRecord
2727

2828
include Alchemy.storage_adapter.attachment_class_methods
2929

30-
stampable stamper_class_name: Alchemy.user_class_name
30+
stampable stamper_class_name: Alchemy.config.user_class_name
3131

3232
scope :by_file_type, ->(*file_type) do
3333
Alchemy.storage_adapter.by_file_type_scope(file_type)

app/models/alchemy/element.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Element < BaseRecord
5555
#
5656
acts_as_list scope: [:page_version_id, :fixed, :parent_element_id]
5757

58-
stampable stamper_class_name: Alchemy.user_class_name
58+
stampable stamper_class_name: Alchemy.config.user_class_name
5959

6060
before_destroy :delete_all_nested_elements
6161

app/models/alchemy/folded_page.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
module Alchemy
1414
class FoldedPage < BaseRecord
1515
belongs_to :page, inverse_of: :folded_pages
16-
belongs_to :user, inverse_of: :folded_pages, class_name: Alchemy.user_class_name
16+
belongs_to :user, inverse_of: :folded_pages, class_name: Alchemy.config.user_class_name
1717

1818
def self.folded_for_user(user)
19-
return none unless Alchemy.user_class.respond_to?(:where)
19+
return none unless Alchemy.config.user_class.respond_to?(:where)
2020

2121
where(user: user, folded: true)
2222
end

app/models/alchemy/node.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Node < BaseRecord
88
before_destroy :check_if_related_node_ingredients_present
99

1010
acts_as_nested_set scope: "language_id", touch: true
11-
stampable stamper_class_name: Alchemy.user_class_name
11+
stampable stamper_class_name: Alchemy.config.user_class_name
1212

1313
belongs_to :language, class_name: "Alchemy::Language"
1414
belongs_to :page, class_name: "Alchemy::Page", optional: true, inverse_of: :nodes

app/models/alchemy/page.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,25 @@ class Page < BaseRecord
8989

9090
acts_as_nested_set(dependent: :destroy, scope: [:layoutpage, :language_id])
9191

92-
stampable stamper_class_name: Alchemy.user_class_name
92+
stampable stamper_class_name: Alchemy.config.user_class_name
9393

9494
belongs_to :language
9595

9696
belongs_to :creator,
97-
primary_key: Alchemy.user_class_primary_key,
98-
class_name: Alchemy.user_class_name,
97+
primary_key: Alchemy.config.user_class_primary_key,
98+
class_name: Alchemy.config.user_class_name,
9999
foreign_key: :creator_id,
100100
optional: true
101101

102102
belongs_to :updater,
103-
primary_key: Alchemy.user_class_primary_key,
104-
class_name: Alchemy.user_class_name,
103+
primary_key: Alchemy.config.user_class_primary_key,
104+
class_name: Alchemy.config.user_class_name,
105105
foreign_key: :updater_id,
106106
optional: true
107107

108108
belongs_to :locker,
109-
primary_key: Alchemy.user_class_primary_key,
110-
class_name: Alchemy.user_class_name,
109+
primary_key: Alchemy.config.user_class_primary_key,
110+
class_name: Alchemy.config.user_class_name,
111111
foreign_key: :locked_by,
112112
optional: true
113113

0 commit comments

Comments
 (0)