Sha256: 09c2cecc100f81ce609afb1d8afb2a61d3e9775433a843eaec2439ea8737ae60

Contents?: true

Size: 1.5 KB

Versions: 9

Compression:

Stored size: 1.5 KB

Contents

module Doorkeeper
  module ApplicationMixin
    extend ActiveSupport::Concern

    include OAuth::Helpers
    include Models::Scopes

    included do
      has_many :access_grants, dependent: :destroy, class_name: 'Doorkeeper::AccessGrant'
      has_many :access_tokens, dependent: :destroy, class_name: 'Doorkeeper::AccessToken'

      validates :name, :secret, :uid, presence: true
      validates :uid, uniqueness: true
      validates :redirect_uri, redirect_uri: true

      before_validation :generate_uid, :generate_secret, on: :create

      if ::Rails.version.to_i < 4 || defined?(::ProtectedAttributes)
        attr_accessible :name, :redirect_uri
      end
    end

    module ClassMethods
      def by_uid_and_secret(uid, secret)
        where(uid: uid, secret: secret).limit(1).to_a.first
      end

      def by_uid(uid)
        where(uid: uid).limit(1).to_a.first
      end
    end

    alias_method :original_scopes, :scopes
    def scopes
      if has_scopes?
        original_scopes
      else
        fail NameError, "Missing column: `applications.scopes`.", <<-MSG.squish
If you are using ActiveRecord run `rails generate doorkeeper:application_scopes
&& rake db:migrate` to add it.
        MSG
      end
    end

    private

    def has_scopes?
      Doorkeeper.configuration.orm != :active_record ||
        Application.new.attributes.include?("scopes")
    end

    def generate_uid
      self.uid ||= UniqueToken.generate
    end

    def generate_secret
      self.secret ||= UniqueToken.generate
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
doorkeeper-2.2.0 lib/doorkeeper/models/application_mixin.rb
doorkeeper-2.1.4 lib/doorkeeper/models/application_mixin.rb
doorkeeper-2.1.3 lib/doorkeeper/models/application_mixin.rb
doorkeeper-2.1.2 lib/doorkeeper/models/application_mixin.rb
doorkeeper-2.1.1 lib/doorkeeper/models/application_mixin.rb
doorkeeper-2.1.0 lib/doorkeeper/models/application_mixin.rb
doorkeeper-2.0.1 lib/doorkeeper/models/application_mixin.rb
doorkeeper-2.0.0 lib/doorkeeper/models/application_mixin.rb
doorkeeper-2.0.0.rc3 lib/doorkeeper/models/application_mixin.rb