Sha256: bda4fa10fb0425c811393689ec66233b328d1753e36eff58fef4047fbd18b578

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

module IntercomRails

  module Proxy

    class User < Proxy

      POTENTIAL_USER_OBJECTS = [
        Proc.new { instance_eval &IntercomRails.config.user.current if IntercomRails.config.user.current.present? },
        Proc.new { current_user },
        Proc.new { @user }
      ]

      def self.current_in_context(search_object)
        POTENTIAL_USER_OBJECTS.each do |potential_object|
          begin
            user_proxy = new(search_object.instance_eval(&potential_object), search_object)
            return user_proxy if user_proxy.valid?
          rescue NameError
            next
          end
        end

        raise NoUserFoundError
      end

      def standard_data
        hsh = {}

        hsh[:user_id] = user.id if attribute_present?(:id) 
        [:email, :name, :created_at].each do |attribute|
          hsh[attribute] = user.send(attribute) if attribute_present?(attribute)
        end

        hsh
      end

      def valid?
        return false if user.blank?
        return true if user.respond_to?(:id) && user.id.present?
        return true if user.respond_to?(:email) && user.email.present?
        false
      end

    end

  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
intercom-rails-0.2.1 lib/intercom-rails/proxy/user.rb
intercom-rails-0.2.0 lib/intercom-rails/proxy/user.rb