Class: Wco::LeadsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/wco/leads_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'app/controllers/wco/leads_controller.rb', line 5

def create
  params[:lead][:tag_ids]&.delete ''
  params[:lead].delete :leadset_id if params[:lead][:leadset_id].blank?

  @lead = Wco::Lead.new params[:lead].permit!
  authorize! :create, @lead

  if params[:lead][:photo]
    photo = Wco::Photo.new photo: params[:lead][:photo]
    photo.is_public = true
    if photo.save
      @lead.photo = photo
    end
    params[:lead].delete :photo
  end

  if @lead.save
    flash_notice 'ok'
  else
    flash_alert @lead
  end
  redirect_to action: :index
end

#create_importObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/controllers/wco/leads_controller.rb', line 29

def create_import
  authorize! :create, Wco::Lead
  file = params[:file]
  selected_tag_ids = params[:tags] || []

  if file.nil?
    redirect_back fallback_location: root_path, alert: "No file selected" and return
  end

  CSV.foreach(file.path, headers: true) do |row|
    lead_attrs = {
      email:   row['email'] || row['Email'],
      name:    row['name'] || row['Name'],
      phone:   row['phone'] || row['Phone'],
      address: row['address'] || row['Address']
    }.compact ## skip missing columns
    lead_attrs[:email] = lead_attrs[:email].downcase

    if lead_attrs[:email]
      lead   = Wco::Lead.find_by( email: lead_attrs[:email] ) rescue nil
      lead ||= Wco::Lead.create(lead_attrs)

      selected_tag_ids.each do |tag_id|
        lead.tags << Wco::Tag.find(tag_id)
      end
    end
  end

  redirect_to wco.leads_path, notice: "Leads imported successfully"
rescue => e
  redirect_back fallback_location: root_path, alert: "Error: #{e.message}"
end

#editObject



62
63
64
65
# File 'app/controllers/wco/leads_controller.rb', line 62

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

#indexObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/controllers/wco/leads_controller.rb', line 67

def index
  authorize! :index, Wco::Lead
  @leads = Wco::Lead.all

  if params[:q].present?
    q = params[:q].downcase
    @leads = @leads.any_of(
      { email: /#{q}/i },
      { name:  /#{q}/i },
    );

    if 1 == @leads.length
      redirect_to controller: 'wco/leads', action: 'show', id: @leads[0].id
      return
    end
  end

  @leads = @leads.includes( :tags )
  @leads = @leads.page( params[:leads_page ] ).per( current_profile.per_page )
end

#newObject



88
89
90
91
# File 'app/controllers/wco/leads_controller.rb', line 88

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

#new_importObject



93
94
95
# File 'app/controllers/wco/leads_controller.rb', line 93

def new_import
  authorize! :new, Wco::Lead
end

#showObject



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/wco/leads_controller.rb', line 97

def show
  @lead      = Wco::Lead.where({ id: params[:id] }).first
  @lead    ||= Wco::Lead.where({ email: params[:id] }).first
  authorize! :show, @lead
  if !@lead
    flash_alert "This lead does not exist"
    redirect_to request.referrer
    return
  end
  @ctxs  = @lead.ctxs.page(          params[:ctxs_page]  ).per( current_profile.per_page )
  @convs = @lead.conversations.page( params[:convs_page] ).per( current_profile.per_page )

end

#updateObject



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/controllers/wco/leads_controller.rb', line 111

def update
  params[:lead][:tag_ids]&.delete ''
  params[:lead].delete :leadset_id if params[:lead][:leadset_id].blank?

  @lead = Wco::Lead.find params[:id]
  authorize! :update, @lead

  if params[:lead][:photo]
    photo = Wco::Photo.new photo: params[:lead][:photo]
    photo.is_public = true
    if photo.save
      @lead.photo = photo
    end
    params[:lead].delete :photo
  end

  if @lead.update params[:lead].permit!
    flash_notice 'ok'
  else
    puts! @lead.errors.full_messages.join(", "), 'cannot update lead'
    flash_alert @lead
  end
  redirect_to action: :show, id: @lead.id
end