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 : )

[View source]

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

[View source]

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/ish_manager/leads_controller.rb', line 38

def create
  param_lead_tags = params[:lead].delete :lead_tags
  param_photo     = params[:lead].delete :photo
  @lead = ::Lead.new params[:lead].permit!
  authorize! :create, @lead

  param_lead_tags.delete ''
  lead_tags = LeadTag.where({ lead_id: @lead.id })
  if param_lead_tags.map(&:to_i).sort != lead_tags.map(&:term_id).sort
    lead_tags.map(&:destroy)
    param_lead_tags.each do |lt|
      LeadTag.create({ lead_id: @lead.id, term_id: lt })
    end
  end

  if param_photo
    photo = Photo.create photo: param_photo
    @lead.photo_id  = photo.id
    # @lead.photo_url = photo.photo.url(:small)
  end

  if @lead.save
    flash[:notice] = "created lead"
    redirect_to action: :show, id: @lead.id
  else
    flash[:alert] = "Cannot create lead: #{@lead.errors.messages}"
    render action: :new
  end
end

#editObject

[View source]

68
69
70
71
# File 'app/controllers/ish_manager/leads_controller.rb', line 68

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

#importObject

[View source]

73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/ish_manager/leads_controller.rb', line 73

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

[View source]

84
85
86
87
88
89
90
91
92
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
121
122
123
124
125
126
127
# File 'app/controllers/ish_manager/leads_controller.rb', line 84

def index
  authorize! :index, ::Lead
  @leads = ::Lead.kept.includes( :company )
  if params[:q].present?
    @leads = @leads.where(" email LIKE ? or name LIKE ? ", "%#{params[:q]}%", "%#{params[:q]}%" )
  end
  if params[:q_tag_ids].present?
    carry = nil
    params[:q_tag_ids].each do |term_id|
      lts = LeadTag.where({ term_id: term_id }).map(&:lead_id)
      if carry
        carry = carry & lts
      else
        carry = lts
      end
    end
    @leads = Lead.where({ :id.in => carry })
  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

[View source]

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

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

#showObject

[View source]

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

def show
  @lead      = Lead.find params[:id]
  authorize! :show, @lead
  @schs      = Sch.where( lead_id: @lead.id )
  @ctxs      = Ctx.where( lead_id: @lead.id )
  @convs     = Conv.find( Office::EmailConversationLead.where( lead_id: @lead.id ).map( &:email_conversation_id ) )
  @msgs      = Msg.where( from: @lead.email )
  @galleries = @lead.galleries.page( params[:galleries_page] ).per( current_profile.per_page )
  @videos    = @lead.videos.page( params[:videos_page]       ).per( current_profile.per_page )
end

#updateObject

[View source]

145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/ish_manager/leads_controller.rb', line 145

def update
  param_lead_tags = params[:lead].delete :lead_tags
  param_photo     = params[:lead].delete :photo
  @lead = ::Lead.find params[:id]
  authorize! :update, @lead

  param_lead_tags.delete ''
  lead_tags = LeadTag.where({ lead_id: @lead.id })
  if param_lead_tags.map(&:to_i).sort != lead_tags.map(&:term_id).sort
    lead_tags.map(&:destroy)
    param_lead_tags.each do |lt|
      LeadTag.create({ lead_id: @lead.id, term_id: lt })
    end
  end

  if param_photo
    photo = Photo.create photo: param_photo
    @lead.photo_id  = photo.id
    # @lead.photo_url = photo.photo.url(:small)
  end

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