Sha256: 1c7963909bc8c6cb18a5f9c05d7534160d80d3f55a4ecd735917be36ee0f0324

Contents?: true

Size: 1.21 KB

Versions: 1

Compression:

Stored size: 1.21 KB

Contents

module IntercomRails

  class CurrentUser

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

    def self.locate_and_prepare_for_intercom(*args)
      new(*args).to_hash
    end

    attr_reader :search_object, :user

    def initialize(search_object)
      @search_object = search_object 
      @user = find_user
    end

    def find_user
      POTENTIAL_USER_OBJECTS.each do |potential_user|
        begin
          user = search_object.instance_eval &potential_user
          return user if user.present? && 
                         (user.email.present? || user.id.present?)
        rescue NameError
          next
        end
      end

      raise CurrentUserNotFoundError
    end

    def to_hash
      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

    private
    def attribute_present?(attribute)
      user.respond_to?(attribute) && user.send(attribute).present?
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
intercom-rails-0.0.9 lib/intercom-rails/current_user.rb