Class: API::V1::ContactsController

Inherits:
Faalis::APIController
  • Object
show all
Defined in:
app/controllers/api/v1/contacts_controller.rb

Instance Method Summary collapse

Instance Method Details

#build_resourceObject



63
64
65
# File 'app/controllers/api/v1/contacts_controller.rb', line 63

def build_resource
  @contact = ::Contact.new(resource_params)
end

#createObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/controllers/api/v1/contacts_controller.rb', line 10

def create
  details = params[:contact][:details] || []
  details_list = []
  details.each do |detail|
    contact_detail = ContactDetails.create!(:detail_field => detail[:field],
                                            :detail_type => detail[:type],
                                            :detail_value => detail[:value])
    details_list << contact_detail
  end

  @contact.details = details_list

  if @contact.save
    respond_with(@contact)
  else
    respond_to do |format|
      format.json { render :json => {:fields => @contact.errors}, :status => :unprocessable_entity }
    end
  end
end

#destroyObject



56
57
58
59
60
61
# File 'app/controllers/api/v1/contacts_controller.rb', line 56

def destroy
  ids = params[:id].split(",")
  @contacts = ::Contact.where(:id => ids)
  authorize! :destroy, @contacts
  @contacts.destroy_all
end

#indexObject

GET /api/v1/contacts



6
7
8
# File 'app/controllers/api/v1/contacts_controller.rb', line 6

def index
  respond_with(@contacts)
end

#resource_paramsObject



67
68
69
# File 'app/controllers/api/v1/contacts_controller.rb', line 67

def resource_params
  params.require(:contact).permit(:id, :prefix, :first_name, :middle_name, :last_name, :suffix, :organization, :is_organization)
end

#showObject



31
32
33
# File 'app/controllers/api/v1/contacts_controller.rb', line 31

def show
  respond_with(@contact)
end

#updateObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/controllers/api/v1/contacts_controller.rb', line 35

def update
  details = params[:contact][:details] || []
  details_list = []
  details.each do |detail|
    contact_detail = ContactDetails.create!(:detail_field => detail[:field],
                                            :detail_type => detail[:type],
                                            :detail_value => detail[:value])
    details_list << contact_detail
  end

  @contact.details = details_list

  if @contact.update(resource_params)
    respond_with(@contact)
  else
    respond_to do |format|
      format.json { render :json => {:fields => @contact.errors}, :status => :unprocessable_entity }
    end
  end
end