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, #tinymce

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 = Ish::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



54
55
56
57
58
59
60
61
62
63
# File 'app/controllers/ish_manager/leads_controller.rb', line 54

def import
  authorize! :import, ::Lead
  file   = params[:csv_file]

  ## 2023-04-08
  flags = Lead.import_v1( file )

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

#indexObject



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
92
93
94
95
96
# File 'app/controllers/ish_manager/leads_controller.rb', line 65

def index
  authorize! :index, ::Lead
  @leads = ::Lead.kept.includes( :company )
  if params[:q].present?
    @leads = @leads.where(" email LIKE ? ", "%#{params[:q]}%" )
  end
  @leads = @leads.page( params[:leads_page ] ).per( current_profile.per_page )

  # @email_contexts = {}
  # # lead_emails = @leads.map( &:email ).compact.reject(&:empty?)
  # lead_ids = @leads.map( &:id )
  # map = %Q{
  #   function() {
  #     emit(''+this.lead_id, {count: 1})
  #   }
  # }
  # reduce = %Q{
  #   function(key, values) {
  #     var result = {count: 0};
  #     values.forEach(function(value) {
  #       result.count += value.count;
  #     });
  #     return result;
  #   }
  # }
  # tmp_contexts = Ish::EmailContext.all.where( :lead_id.in => lead_ids
  #   ).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 }

end

#newObject



98
99
100
101
# File 'app/controllers/ish_manager/leads_controller.rb', line 98

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

#showObject



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

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



111
112
113
114
115
116
117
118
119
120
121
# 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
  ## 2023-05-14 NOT redirecting to index anymore.
  redirect_to :action => 'show', id: @lead.id
end