Sha256: 832cf28f834994ad206c7ea2895ce5aca2048f3189d802f2d3aa1cc74386048d

Contents?: true

Size: 1.4 KB

Versions: 5

Compression:

Stored size: 1.4 KB

Contents

module Challah
  module Authorization
    def challah_authorization
      unless included_modules.include?(InstanceMethods)
        include InstanceMethods
        extend ClassMethods
      end
    end

    module InstanceMethods
      def user
        return nil unless self.user_id
        @user ||= ::User.where(id: self.user_id).first
      end
    end

    module ClassMethods
      # Remove an authorization
      def del(options = {})
        provider  = options.fetch(:provider)
        user_id   = options.fetch(:user_id)

        where(provider: provider, user_id: user_id).delete_all
      end

      # Grab the user/provider record
      def get(options = {})
        provider  = options.fetch(:provider)
        user_id   = options.fetch(:user_id)

        where(provider: provider, user_id: user_id).first
      end

      # Create a new authorization record for the given user
      def set(options = {})
        provider    = options.fetch(:provider)
        user_id     = options.fetch(:user_id).to_i
        uid         = options.fetch(:uid)
        token       = options.fetch(:token)
        expires_at  = options.fetch(:expires_at, nil)

        del(options)

        record = self.new()
        record.provider = provider
        record.user_id = user_id
        record.uid = uid
        record.token = token
        record.expires_at = expires_at
        record.save!
        record
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
challah-1.0.0.beta2 lib/challah/authorization.rb
challah-1.0.0.beta lib/challah/authorization.rb
challah-0.9.1.beta.3 lib/challah/authorization.rb
challah-0.9.1.beta.2 lib/challah/authorization.rb
challah-0.9.1.beta lib/challah/authorization.rb