Skip to content

Commit

Permalink
Move polymorphic resource owner and application owner associations in…
Browse files Browse the repository at this point in the history
… a lazy ORM hooks
  • Loading branch information
nbulaj committed Jan 12, 2023
1 parent 1794b50 commit c27b7ef
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ User-visible changes worth mentioning.
- [#1605] Fix URI validation for Ruby 3.2+.
- [#1625] Exclude endless access tokens from `StaleRecordsCleaner`.
- [#1626] Remove deprecated `active_record_options` config option.
- [#1627] Move polymorphic resource owner and application owner associations in a lazy ORM hooks.

## 5.6.2

Expand Down
1 change: 1 addition & 0 deletions lib/doorkeeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ module Models
autoload :Expirable, "doorkeeper/models/concerns/expirable"
autoload :ExpirationTimeSqlMath, "doorkeeper/models/concerns/expiration_time_sql_math"
autoload :Orderable, "doorkeeper/models/concerns/orderable"
autoload :PolymorphicResourceOwner, "doorkeeper/models/concerns/polymorphic_resource_owner"
autoload :Scopes, "doorkeeper/models/concerns/scopes"
autoload :Reusable, "doorkeeper/models/concerns/reusable"
autoload :ResourceOwnerable, "doorkeeper/models/concerns/resource_ownerable"
Expand Down
30 changes: 30 additions & 0 deletions lib/doorkeeper/models/concerns/polymorphic_resource_owner.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Doorkeeper
module Models
module PolymorphicResourceOwner
module ForAccessGrant
extend ActiveSupport::Concern

included do
if Doorkeeper.config.polymorphic_resource_owner?
belongs_to :resource_owner, polymorphic: true, optional: false
else
validates :resource_owner_id, presence: true
end
end
end

module ForAccessToken
extend ActiveSupport::Concern

included do
if Doorkeeper.config.polymorphic_resource_owner?
belongs_to :resource_owner, polymorphic: true, optional: true
end
end
end
end
end
end

11 changes: 10 additions & 1 deletion lib/doorkeeper/orm/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,16 @@ module Mixins
end

def self.run_hooks
# nop
initialize_configured_associations
end

def self.initialize_configured_associations
if Doorkeeper.config.enable_application_owner?
Doorkeeper.config.application_model.include ::Doorkeeper::Models::Ownership
end

Doorkeeper.config.access_grant_model.include ::Doorkeeper::Models::PolymorphicResourceOwner::ForAccessGrant
Doorkeeper.config.access_token_model.include ::Doorkeeper::Models::PolymorphicResourceOwner::ForAccessToken
end
end
end
Expand Down
6 changes: 0 additions & 6 deletions lib/doorkeeper/orm/active_record/mixins/access_grant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ module AccessGrant
optional: true,
inverse_of: :access_grants

if Doorkeeper.config.polymorphic_resource_owner?
belongs_to :resource_owner, polymorphic: true, optional: false
else
validates :resource_owner_id, presence: true
end

validates :application_id,
:token,
:expires_in,
Expand Down
4 changes: 0 additions & 4 deletions lib/doorkeeper/orm/active_record/mixins/access_token.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ module AccessToken
inverse_of: :access_tokens,
optional: true

if Doorkeeper.config.polymorphic_resource_owner?
belongs_to :resource_owner, polymorphic: true, optional: true
end

validates :token, presence: true, uniqueness: { case_sensitive: true }
validates :refresh_token, uniqueness: { case_sensitive: true }, if: :use_refresh_token?

Expand Down
4 changes: 0 additions & 4 deletions lib/doorkeeper/orm/active_record/mixins/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ module Application

include ::Doorkeeper::ApplicationMixin

if Doorkeeper.config.enable_application_owner?
include ::Doorkeeper::Models::Ownership
end

has_many :access_grants,
foreign_key: :application_id,
dependent: :delete_all,
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,13 @@
end

Object.const_set("ApplicationWithOwner", application_with_owner_class)
# Manually reload Doorkeeper ORM setup because we're patching classes
Doorkeeper.run_orm_hooks
end

after do
Object.send(:remove_const, :ApplicationWithOwner)
Doorkeeper.run_orm_hooks
end

it "adds support for application owner" do
Expand All @@ -404,10 +407,13 @@
end

Object.const_set("ApplicationWithOwner", application_with_owner_class)
# Manually reload Doorkeeper ORM setup because we're patching classes
Doorkeeper.run_orm_hooks
end

after do
Object.send(:remove_const, :ApplicationWithOwner)
Doorkeeper.run_orm_hooks
end

it "adds support for application owner" do
Expand Down
6 changes: 6 additions & 0 deletions spec/models/doorkeeper/application_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,13 @@ def self.generate
end

Object.const_set("ApplicationWithOwner", application_with_owner_class)
# Manually reload Doorkeeper ORM setup because we're patching classes
Doorkeeper.run_orm_hooks
end

after do
Object.send(:remove_const, :ApplicationWithOwner)
Doorkeeper.run_orm_hooks
end

it "is valid given valid attributes" do
Expand All @@ -135,10 +138,13 @@ def self.generate
@owner = FactoryBot.build_stubbed(:doorkeeper_testing_user)

Object.const_set("ApplicationWithOwner", application_with_owner_class)
# Manually reload Doorkeeper ORM setup because we're patching classes
Doorkeeper.run_orm_hooks
end

after do
Object.send(:remove_const, :ApplicationWithOwner)
Doorkeeper.run_orm_hooks
end

it "is invalid without an owner" do
Expand Down

0 comments on commit c27b7ef

Please sign in to comment.