Sha256: 294b98d9ed752b4c3d7ba877ca437924034338d95084cbb96ca19a09e50be49e

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 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? || user.respond_to?(:new_record?) && user.new_record?
        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

1 entries across 1 versions & 1 rubygems

Version Path
intercom-rails-0.2.2 lib/intercom-rails/proxy/user.rb