Sha256: 373d5d59130c9ead116a501777bb596de61a2c438841425b7c46653117ec9d19

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

module IntercomRails

  module Proxy

    class User < Proxy

      PREDEFINED_POTENTIAL_USER_OBJECTS = [
        Proc.new { current_user },
        Proc.new { @user }
      ]

      def self.potential_user_objects
        if config.current.present?
          [Proc.new { instance_eval &IntercomRails.config.user.current }]
        else
          PREDEFINED_POTENTIAL_USER_OBJECTS
        end
      end

      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

6 entries across 6 versions & 1 rubygems

Version Path
intercom-rails-0.2.8 lib/intercom-rails/proxy/user.rb
intercom-rails-0.2.7 lib/intercom-rails/proxy/user.rb
intercom-rails-0.2.6 lib/intercom-rails/proxy/user.rb
intercom-rails-0.2.5 lib/intercom-rails/proxy/user.rb
intercom-rails-0.2.4 lib/intercom-rails/proxy/user.rb
intercom-rails-0.2.3 lib/intercom-rails/proxy/user.rb