Sha256: 21bed715e8dc22cc1ba6ff84c0fd2b74fc7cdb618b39be3643c3233ec7cbfe36

Contents?: true

Size: 1.08 KB

Versions: 20

Compression:

Stored size: 1.08 KB

Contents

module E9Crm::Rack
  #
  # Returns contacts and joins their primary user for their email if it exists,
  # and will return a string like "firstname lastname (email)".  If for whatever
  # reason the contact has no primary user, it will drop the email.
  #
  class CompanyAutoCompleter
    DEFAULT_LIMIT = 10

    def self.call(env)
      if env["PATH_INFO"] =~ /^\/autocomplete\/companies/
        params = Rack::Request.new(env).params
        
        if term = params['term']
          relation = Company.limit(params['limit'] || DEFAULT_LIMIT).select('name').attr_like('name', term, :matcher => '%s%%')

          companies = ::ActiveRecord::Base.connection.send(:select, relation.to_sql, 'Company Autocomplete').map do |row|
            { :label => row['name'], :value => row['name'] }
          end
        else
          companies = []
        end

        [200, {"Content-Type" => "application/json", "Cache-Control" => "max-age=3600, must-revalidate"}, [companies.to_json]]
      else
        [404, {"Content-Type" => "text/html", "X-Cascade" => "pass"}, ["Not Found"]]
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
e9_crm-0.1.34 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.33 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.32 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.31 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.30 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.29 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.28 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.27 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.26 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.25 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.24 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.23 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.22 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.21 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.20 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.19 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.18 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.17 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.16 lib/e9_crm/rack/company_auto_completer.rb
e9_crm-0.1.14 lib/e9_crm/rack/company_auto_completer.rb