Sha256: 1131079472ac7b0b8355f130aef09fab27127f9ff748e14b8aced8124d1f759d

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

module OData
  class CreateOperation < Operation

    def handle_operation_response(response)
      # Grab the id, and put back into the active record instance
      if response.headers['OData-EntityId']
        id = response.headers['OData-EntityId'].scan(/\(([\w-]*)\)/)
        @ar.id = id[0][0] unless id.nil? || id[0].nil?
        @ar.errors[:base] << "Failed to #{operation_callback_name} entity. [http code #{response.code}]" if @ar.id.nil?
      else
        @ar.errors[:base] << "Could not #{operation_callback_name} entity. [http code #{response.code}]" if @ar.id.nil?
      end
      check_response_errors(response)
    end

    def operation_body
      body = {}
      # Add changed fields and values
      @ar.changes.each do |field, values|
        # If a belongs to field, add association the way OData wants it
        if @ar.class.belongs_to_field?(field)
          belongs_to_field = @ar.class.belongs_to_field(field)
          body["#{belongs_to_field.options[:crm_key]}@odata.bind"] = "/#{belongs_to_field.class_name.downcase}s(#{values[1]})"
        else
          body[field.downcase] = values[1]
        end
      end
      body.to_json
    end

    def operation_method
      :post
    end

    def operation_url
      "#{base_url}#{entity_name}"
    end

    def operation_callback_name
      :create
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord_sqlserver_crm-4.2.1 lib/odata/create_operation.rb
activerecord_sqlserver_crm-0.0.1 lib/odata/create_operation.rb