Class: IshManager::LeadsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/ish_manager/leads_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#home

Instance Method Details

#bulkopObject

alphabetized : )



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/controllers/ish_manager/leads_controller.rb', line 10

def bulkop
  authorize! :bulkop, ::Lead
  case params[:a]
  when 'add_to_campaign'
    c = EmailCampaign.find params[:email_campaign_id]
    params[:lead_ids].each do |lead_id|
      c_lead = EmailCampaignLead.new( lead_id: lead_id, email_campaign_id: c.id )
      flag = c_lead.save
      if !flag
        puts! c_lead.errors.full_messages.join(", ")
      end
    end
    flash[:notice] = 'Done acted; See logs.'
    redirect_to action: :index
  end
end

#createObject



27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/ish_manager/leads_controller.rb', line 27

def create
  @lead = ::Lead.new params[:lead].permit!
  authorize! :create, @lead
  if @lead.save
    flash[:notice] = "created lead"
  else
    flash[:alert] = "Cannot create lead: #{@lead.errors.messages}"
  end
  redirect_to :action => 'index'
end

#editObject



38
39
40
41
# File 'app/controllers/ish_manager/leads_controller.rb', line 38

def edit
  @lead = ::Lead.find params[:id]
  authorize! :edit, @lead
end

#importObject

0 1 2 3 4 5 6 7 8 fields: id, date, name, email, company url, source tag, phone, linkedin, comment



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/ish_manager/leads_controller.rb', line 45

def import
  authorize! :import, ::Lead
  file = params[:csv_file]
  flags = []
  errors = []
  CSV.read(file.path, headers: true).each do |row|
    company_url = row[4].presence
    company_url ||= "https://#{row[3].split('@')[1]}"
    company = ::Leadset.find_or_create_by({ company_url: company_url })
    lead = ::Lead.new({
      name: row[2] || 'there',
      full_name: row[2] || 'there',
      email: row[3],
      m3_leadset_id: company.id,
      phone: row[6],
    })
    flag = lead.save
    flags << flag
    if !flag
      errors << lead.errors.full_messages.join(", ")
    end
  end
  flash[:notice] = "Result: #{flags.inspect} ."
  flash[:alert] = errors
  redirect_to action: 'new'
end

#indexObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/controllers/ish_manager/leads_controller.rb', line 72

def index
  authorize! :index, ::Lead
  @leads = ::Lead.all # .includes( :leadset, :email_campaign_leads )
  lead_emails = @leads.map( &:email ).compact.reject(&:empty?)

  map = %Q{
    function() {
      emit(this.to_email, {count: 1})
    }
  }
  reduce = %Q{
    function(key, values) {
      var result = {count: 0};
      values.forEach(function(value) {
        result.count += value.count;
      });
      return result;
    }
  }
  @email_contexts = {}
  tmp_contexts = Ish::EmailContext.all.where( :to_email.in => lead_emails
    ).map_reduce( map, reduce
    ).out( inline: 1 ## From: https://www.mongodb.com/docs/mongoid/current/reference/map-reduce/
    ).to_a
  tmp_contexts.map { |x| @email_contexts[x[:_id]] = x[:value][:count].to_i }
  # puts! @email_contexts, '@email_contexts'

end

#newObject



101
102
103
104
# File 'app/controllers/ish_manager/leads_controller.rb', line 101

def new
  @new_lead = ::Lead.new
  authorize! :new, @new_lead
end

#showObject



106
107
108
109
# File 'app/controllers/ish_manager/leads_controller.rb', line 106

def show
  authorize! :redirect, IshManager::Ability
  redirect_to :action => :edit, :id => params[:id]
end

#updateObject



111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/ish_manager/leads_controller.rb', line 111

def update
  @lead = ::Lead.find params[:id]
  authorize! :update, @lead
  if @lead.update_attributes params[:lead].permit!
    flash[:notice] = 'Successfully updated lead.'
  else
    flash[:alert] = "Cannot update lead: #{@lead.errors.messages}"
  end
  redirect_to :action => 'index'
end