Skip to content

Commit

Permalink
fix: do not preload Rails base classes
Browse files Browse the repository at this point in the history
It can break Rails initialization

Ref #255
  • Loading branch information
palkan committed Jan 17, 2024
1 parent 8204e9b commit 9546d02
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## master

- Do not preload Rails base classes, use load hooks everywhere. ([@palkan][])

## 0.6.7 (2023-09-13)

- Fix loading Rails extensions during eager load. ([@palkan][])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ def params_filter(...)
end
end

# Register params scope matcher
ActionPolicy::Base.scope_matcher :action_controller_params, ActionController::Parameters

# Add alias to base policy
ActionPolicy::Base.extend ActionPolicy::ScopeMatchers::ActionControllerParams

ActiveSupport.on_load(:action_controller) do
# Register params scope matcher
ActionPolicy::Base.scope_matcher :action_controller_params, ActionController::Parameters
end
24 changes: 13 additions & 11 deletions lib/action_policy/rails/scope_matchers/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,20 @@ def relation_scope(...)
end
end

# Register relation scope matcher
ActionPolicy::Base.scope_matcher :active_record_relation, ActiveRecord::Relation

# Add alias to base policy
ActionPolicy::Base.extend ActionPolicy::ScopeMatchers::ActiveRecord

ActiveRecord::Relation.include(Module.new do
def policy_name
if model.respond_to?(:policy_name)
model.policy_name.to_s
else
"#{model}Policy"
ActiveSupport.on_load(:active_record) do
# Register relation scope matcher
ActionPolicy::Base.scope_matcher :active_record_relation, ActiveRecord::Relation

ActiveRecord::Relation.include(Module.new do
def policy_name
if model.respond_to?(:policy_name)
model.policy_name.to_s
else
"#{model}Policy"
end
end
end
end)
end)
end
19 changes: 4 additions & 15 deletions lib/action_policy/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ def cache_store=(store)
app.config.action_policy.namespace_cache_enabled

ActiveSupport.on_load(:action_controller) do
require "action_policy/rails/scope_matchers/action_controller_params"

next unless app.config.action_policy.auto_inject_into_controller

ActionController::Base.include ActionPolicy::Controller
Expand All @@ -95,21 +93,12 @@ def cache_store=(store)
ActionCable::Channel::Base.authorize :user, through: :current_user
end

# Scope matchers
require "action_policy/rails/scope_matchers/action_controller_params"
require "action_policy/rails/scope_matchers/active_record"

ActiveSupport.on_load(:active_record) do
require "action_policy/rails/ext/active_record"
require "action_policy/rails/scope_matchers/active_record"
end

# Trigger load hooks of the components that extend ActionPolicy itself
# (e.g., scope matchers)
begin
::ActionController::Base
rescue NameError
end

begin
::ActiveRecord::Base
rescue NameError
end
end
end
Expand Down

0 comments on commit 9546d02

Please sign in to comment.