Module: IfdTools::ApiController::Customers

Extended by:
ActiveSupport::Concern
Defined in:
lib/ifd_tools/api_controller/customers.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#create_customerObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ifd_tools/api_controller/customers.rb', line 7

def create_customer
  if params[:customer]
    @customer = Customer.new params[:customer]
    @customer = instance_exec @customer, &self.class.create_customer_block
    if @customer.valid?
      @customer.save
      respond_to do |format|
        format.json { render json: { success: "Registered" }, status: :ok }
        format.xml { render xml: { success: "Registered" }, status: :ok }
      end
    else
      respond_to do |format|
        @failures = @customer.errors.keys.inject({}) { |failures, key| failures[key] = @customer.errors[key].uniq; failures }
        format.json { render json: { error: "validations failed", validations: @customer.errors.full_messages.uniq, failures: @failures }, status: :unprocessable_entity }
        format.xml { render status: :unprocessable_entity }
      end
    end
  else
    respond_to do |format|
      format.json { render json: { error: "Missing parameters" }, status: :unprocessable_entity }
      format.xml { render xml: { error: "Missing parameters" }, status: :unprocessable_entity }
    end
  end
end