Sha256: 741df44236ded7562224153595f4069fd88908d904a36190ba63b17c411dac0e

Contents?: true

Size: 1.94 KB

Versions: 1

Compression:

Stored size: 1.94 KB

Contents

class API::V1::ContactsController < Faalis::APIController
  before_filter :build_resource, :only => :create
  load_and_authorize_resource :except => [:destory]

  # GET /api/v1/contacts
  def index
    respond_with(@contacts)
  end

  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

  def show
    respond_with(@contact)
  end

  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

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

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

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

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
faalis_contacts-0.1.0 app/controllers/api/v1/contacts_controller.rb