Sha256: a5a482b44276f4e17ce0466a9f8a34990199dba4c22e5aa7b78588834c8a0635

Contents?: true

Size: 1.89 KB

Versions: 2

Compression:

Stored size: 1.89 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 ||= self.class.user_model.where(id: self.user_id).first
      end
    end

    module ClassMethods
      def hashable_attributes
        protected_attributes = %w( user_id provider last_session_at last_session_ip session_count created_at updated_at )
        @hashable_attributes ||= self.columns.map(&:name) - protected_attributes
      end

      # 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.delete(:provider)
        user_id     = options.delete(:user_id).to_i
        uid         = options.delete(:uid)
        token       = options.delete(:token)
        expires_at  = options.delete(:expires_at) || nil

        del(provider: provider, user_id: user_id)

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

        record.attributes = options if options.any?

        record.save!
        record
      end
    end

    def users_table_name
      @users_table_name ||= user_model.table_name
    end

    def user_model
      @user_model ||= Challah.user
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
challah-1.1.1 lib/challah/authorization.rb
challah-1.1.0 lib/challah/authorization.rb