Class: E9Crm::Rack::EmailCampaignAutoCompleter

Inherits:
Object
  • Object
show all
Defined in:
lib/e9_crm/rack/email_campaign_auto_completer.rb

Overview

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.

Constant Summary collapse

DEFAULT_LIMIT =
10

Class Method Summary collapse

Class Method Details

.call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/e9_crm/rack/email_campaign_auto_completer.rb', line 10

def self.call(env)
  params = Rack::Request.new(env).params
  
  if term = params['term']
    relation = EmailCampaign.
                 select('name, code').
                 limit(params['limit'] || DEFAULT_LIMIT).
                 attr_like(:code, term, :matcher => '%s%%')

    json = ::ActiveRecord::Base.connection.send(:select, relation.to_sql, 'Email Campaign Autocomplete').map do |row|
      { :label => "#{row['code']} - #{row['name']}", :value => row['code'] }
    end.to_json
  else
    json = []
  end

  [200, {"Content-Type" => "application/json", "Cache-Control" => "no-cache"}, [json]]
end