Skip to content

Commit bddb4cd

Browse files
authored
Merge pull request #3418 from mamhoff/auth-configuration
Convert auth accessors to configuration object
2 parents 192921b + afb581f commit bddb4cd

50 files changed

Lines changed: 479 additions & 353 deletions

Some content is hidden

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

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 < ActiveRecord::Base
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 < ActiveRecord::Base
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
@@ -61,7 +61,7 @@ class Element < BaseRecord
6161
#
6262
acts_as_list scope: [:page_version_id, :fixed, :parent_element_id]
6363

64-
stampable stamper_class_name: Alchemy.user_class_name
64+
stampable stamper_class_name: Alchemy.config.user_class_name
6565

6666
before_destroy :delete_all_nested_elements
6767

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 < ActiveRecord::Base
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
@@ -98,25 +98,25 @@ class Page < BaseRecord
9898

9999
acts_as_nested_set(dependent: :destroy, scope: [:layoutpage, :language_id])
100100

101-
stampable stamper_class_name: Alchemy.user_class_name
101+
stampable stamper_class_name: Alchemy.config.user_class_name
102102

103103
belongs_to :language
104104

105105
belongs_to :creator,
106-
primary_key: Alchemy.user_class_primary_key,
107-
class_name: Alchemy.user_class_name,
106+
primary_key: Alchemy.config.user_class_primary_key,
107+
class_name: Alchemy.config.user_class_name,
108108
foreign_key: :creator_id,
109109
optional: true
110110

111111
belongs_to :updater,
112-
primary_key: Alchemy.user_class_primary_key,
113-
class_name: Alchemy.user_class_name,
112+
primary_key: Alchemy.config.user_class_primary_key,
113+
class_name: Alchemy.config.user_class_name,
114114
foreign_key: :updater_id,
115115
optional: true
116116

117117
belongs_to :locker,
118-
primary_key: Alchemy.user_class_primary_key,
119-
class_name: Alchemy.user_class_name,
118+
primary_key: Alchemy.config.user_class_primary_key,
119+
class_name: Alchemy.config.user_class_name,
120120
foreign_key: :locked_by,
121121
optional: true
122122

0 commit comments

Comments
 (0)