Skip to content

Commit

Permalink
compat with rails 5's default belongs_to presence validation
Browse files Browse the repository at this point in the history
Rails 5 adds default presence validation on belongs_to associations:
rails/rails#18233

Application object need not be present for access token creation in Resource
Owner Password Credentials grant flow.

This is an alternative to doorkeeper-gem#780 for backwards compatibility with rails 4
  • Loading branch information
Aurel Canciu committed Feb 13, 2016
1 parent 56bfa57 commit a7afbc1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/doorkeeper/models/access_token_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,16 @@ module AccessTokenMixin
include ActiveModel::MassAssignmentSecurity if defined?(::ProtectedAttributes)

included do
belongs_to :application,
class_name: 'Doorkeeper::Application',
inverse_of: :access_tokens
belongs_to_options = {
class_name: 'Doorkeeper::Application',
inverse_of: :access_tokens
}

if defined?(ActiveRecord) && defined?(ActiveRecord::Base) && self < ActiveRecord::Base
belongs_to_options.merge! :optional => true if ActiveRecord::VERSION::MAJOR >= 5
end

belongs_to :application, belongs_to_options

validates :token, presence: true, uniqueness: true
validates :refresh_token, uniqueness: true, if: :use_refresh_token?
Expand Down
6 changes: 6 additions & 0 deletions spec/models/doorkeeper/access_token_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ module NoGenerate
subject.resource_owner_id = nil
expect(subject).to be_valid
end

it 'is valid without application_id' do
# For resource owner credentials flow
subject.application_id = nil
expect(subject).to be_valid
end
end

describe '#same_credential?' do
Expand Down

0 comments on commit a7afbc1

Please sign in to comment.