Class: IshManager::LeadsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#basic_auth, #home

Instance Method Details

#bulkopObject

alphabetized : )



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/ish_manager/leads_controller.rb', line 8

def bulkop
  authorize! :bulkop, ::Lead
  case params[:op]
  when Lead::OP_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

  when Lead::OP_DELETE
    outs = []
    params[:lead_ids].each do |lead_id|
      lead = Lead.find( lead_id )
      outs.push lead.discard
    end
    flash[:notice] = "Outcomes: #{outs.inspect}."
    redirect_to action: :index

  else
    throw "Unknown op: #{params[:op]}."

  end
end

#createObject



38
39
40
41
42
43
44
45
46
47
# File 'app/controllers/ish_manager/leads_controller.rb', line 38

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



49
50
51
52
# File 'app/controllers/ish_manager/leads_controller.rb', line 49

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

#importObject

0 1 2 3 4 5 6 7 Fields: id, date, name, email, tag, phone, linkedin, comment



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/controllers/ish_manager/leads_controller.rb', line 56

def import
  authorize! :import, ::Lead
  file   = params[:csv_file]
  flags  = []
  errors = []
  CSV.read(file.path, headers: true).each do |row|

    leadset = ::Leadset.find_or_create_by({ company_url: row[3].split('@')[1] })
    lead = ::Lead.new({
      name: row[2].presence || row[3].split('@')[0],
      full_name: row[2].presence || row[3].split('@')[0],
      email: row[3],
      m3_leadset_id: leadset.id,
      phone: row[5],
    })
    flag = lead.save
    flags << flag
    if !flag
      errors << lead.errors.full_messages.join(", ")
    end

    if row[4].present?
      tags = row[4].split(",").map do |tag_name|
        WpTag.my_find_or_create({ name: tag_name })
      end
      puts! tags, 'tags'
      tags.each do |tag|
        LeadTag.create({ wp_tag: tag, lead: lead })
      end
    end

  end
  flash[:notice] = "Result: #{flags.inspect}."
  flash[:alert] = errors
  redirect_to action: 'new'
end

#indexObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'app/controllers/ish_manager/leads_controller.rb', line 93

def index
  authorize! :index, ::Lead
  @leads = ::Lead.kept.includes( :company )
  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



122
123
124
125
# File 'app/controllers/ish_manager/leads_controller.rb', line 122

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

#showObject



127
128
129
130
131
132
133
# File 'app/controllers/ish_manager/leads_controller.rb', line 127

def show
  @lead = Lead.find params[:id]
  authorize! :show, @lead
  @schs = Sch.where( lead_id: @lead.id )
  @ctxs = Ctx.where( lead_id: @lead.id )
  @msgs = Msg.where( from: @lead.email )
end

#updateObject



135
136
137
138
139
140
141
142
143
144
# File 'app/controllers/ish_manager/leads_controller.rb', line 135

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