Class: Rails::Contact::ContactsController

Inherits:
ApplicationController show all
Includes:
Filterable
Defined in:
app/controllers/rails/contact/contacts_controller.rb

Constant Summary collapse

METADATA_KEYS =
%w[
  prefix middle_name suffix nickname company job_title department website birthday notes
].freeze

Instance Method Summary collapse

Instance Method Details

#bulk_destroyObject



90
91
92
93
94
# File 'app/controllers/rails/contact/contacts_controller.rb', line 90

def bulk_destroy
  ids = params[:ids].to_s.split(",").map(&:to_i).reject(&:zero?).uniq
  Contact.where(id: ids).find_each(&:destroy!)
  redirect_to contacts_path, notice: "#{ids.size} contact(s) deleted."
end

#createObject



62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/rails/contact/contacts_controller.rb', line 62

def create
  @contact = Contact.new(contact_params)
  assign_labels(@contact)
  if @contact.save
    enqueue_index(@contact.id)
    redirect_to contact_path(@contact), notice: "Contact created."
  else
    render "rails/contact/new", status: :unprocessable_entity
  end
end

#destroyObject



83
84
85
86
87
88
# File 'app/controllers/rails/contact/contacts_controller.rb', line 83

def destroy
  id = @contact.id
  @contact.destroy!
  IndexContactJob.perform_later(id, remove: true)
  redirect_to contacts_path, notice: "Contact deleted."
end

#editObject



58
59
60
# File 'app/controllers/rails/contact/contacts_controller.rb', line 58

def edit
  render "rails/contact/edit"
end

#google_sync_rolling_windowObject



37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/rails/contact/contacts_controller.rb', line 37

def google_sync_rolling_window
  unless Rails::Contact.configuration.google_sync_enabled
    redirect_to contacts_path, alert: "Google Contacts sync is disabled."
    return
  end

  GoogleSyncJob.perform_later
  redirect_to contacts_path,
              notice: "Google re-sync has been queued for contacts in the recent window (creates and updates)."
end

#google_sync_unsyncedObject



27
28
29
30
31
32
33
34
35
# File 'app/controllers/rails/contact/contacts_controller.rb', line 27

def google_sync_unsynced
  unless Rails::Contact.configuration.google_sync_enabled
    redirect_to contacts_path, alert: "Google Contacts sync is disabled."
    return
  end

  GoogleSyncUnsyncedJob.perform_later
  redirect_to contacts_path, notice: "Google sync has been queued for contacts not yet linked."
end

#indexObject



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

def index
  @query = params[:q].to_s.strip
  result = Search::Query.new(
    @query,
    filters: normalized_filters(filter_params),
    page: params[:page]
  ).call
  @contacts = result.records
  @total_count = result.total_count
  @page = result.page
  @per_page = result.per_page
  @total_pages = result.total_pages
  @google_contacts_pending_sync = google_pending_sync_count
  render "rails/contact/index"
end

#mergeObject



96
97
98
99
100
101
102
103
# File 'app/controllers/rails/contact/contacts_controller.rb', line 96

def merge
  source_id = params[:source_id]
  target_id = params[:target_id]
  MergeContactsService.new(source_id: source_id, target_id: target_id).call
  redirect_to contact_path(target_id), notice: "Contacts merged."
rescue StandardError => e
  redirect_to contacts_path, alert: "Merge failed: #{e.message}"
end

#newObject



52
53
54
55
56
# File 'app/controllers/rails/contact/contacts_controller.rb', line 52

def new
  @contact = Contact.new
  build_default_associations
  render "rails/contact/new"
end

#showObject



48
49
50
# File 'app/controllers/rails/contact/contacts_controller.rb', line 48

def show
  render "rails/contact/show"
end

#updateObject



73
74
75
76
77
78
79
80
81
# File 'app/controllers/rails/contact/contacts_controller.rb', line 73

def update
  assign_labels(@contact)
  if @contact.update(contact_params)
    enqueue_index(@contact.id)
    redirect_to contact_path(@contact), notice: "Contact updated."
  else
    render "rails/contact/edit", status: :unprocessable_entity
  end
end