<commit_before>require 'addressable/uri'
require 'digest'


module Doorkeeper
  class Application < ::Couchbase::Model
    design_document :dk_app

    attribute :name,
              :uid,
              :secret,
              :redirect_uri
    attribute :scopes, default: ''

    attribute :created_at, :default => lambda { Time.now.to_i }

    view  :show_all,
          :by_user_id


    include OAuth::Helpers
    alias_method :uid, :id


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

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


    def self.by_uid_and_secret(uid, secret)
      app = find_by_id(uid)
      if app
        return app.secret == secret ? app : nil
      end
      nil
    end

    def self.by_uid(uid)
      find_by_id(uid)
    end


    def scopes=(value)
      write_attribute :scopes, value if value.present?
    end

    def self.authorized_for(resource_owner)
      AccessToken.where_owner_id(resource_owner.id)
    end

    

    ## TODO:: Where are these used
    def self.by_user(id)
      by_user_id({:key => [id], :stale => false})
    end
    
    def self.all
      show_all({:key => nil, :include_docs => true, :stale => false})
    end

    def uid
      self.id
    end

    private


    before_create :set_id
    def set_id
      self.id ||= Digest::MD5.hexdigest(self.redirect_uri)
      self.secret ||= UniqueToken.generate
    end

    # This is equivalent to has_many dependent: :destroy
    before_delete :clean_up
    def clean_up
      AccessToken.where_application_id(self.id).each do |at|
        at.delete!
      end
      AccessGrant.where_application_id(self.id).each do |at|
        at.delete!<commit_msg>default application id hash now includes name<commit_after>require 'addressable/uri'
require 'digest'


module Doorkeeper
  class Application < ::Couchbase::Model
    design_document :dk_app

    attribute :name,
              :uid,
              :secret,
              :redirect_uri
    attribute :scopes, default: ''

    attribute :created_at, :default => lambda { Time.now.to_i }

    view  :show_all,
          :by_user_id


    include OAuth::Helpers
    alias_method :uid, :id


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

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


    def self.by_uid_and_secret(uid, secret)
      app = find_by_id(uid)
      if app
        return app.secret == secret ? app : nil
      end
      nil
    end

    def self.by_uid(uid)
      find_by_id(uid)
    end


    def scopes=(value)
      write_attribute :scopes, value if value.present?
    end

    def self.authorized_for(resource_owner)
      AccessToken.where_owner_id(resource_owner.id)
    end

    

    ## TODO:: Where are these used
    def self.by_user(id)
      by_user_id({:key => [id], :stale => false})
    end
    
    def self.all
      show_all({:key => nil, :include_docs => true, :stale => false})
    end

    def uid
      self.id
    end

    private


    before_create :set_id
    def set_id
      self.id ||= Digest::MD5.hexdigest("#{self.name}#{self.redirect_uri}")
      self.secret ||= UniqueToken.generate
    end

    # This is equivalent to has_many dependent: :destroy
    before_delete :clean_up
    def clean_up
      AccessToken.where_application_id(self.id).each do |at|
        at.delete!
      end
      AccessGrant.where_application_id(self.id).each do |at|
        at.delete!
{'fraction_non_alphanumeric': 0.09802832546514857, 'fraction_numerical': 0.0011108025548458762, 'mean_word_length': 2.423954372623574, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '402627', 'n_tokens_mistral': 1195, 'n_tokens_neox': 1144, 'n_words': 275}