module Fakecrm::Drop

Public Instance Methods

create_one(resource, data) click to toggle source
# File lib/fakecrm/drop.rb, line 3
def create_one(resource, data)
  response = {}
  resource.transaction do |t|
    instance = resource.new
    instance.attributes = data

    if instance.save
      response = ResourceView.decorate(instance)
    else
      status 422
      response = instance.errors.to_hash
    end
  end

  response.to_json
rescue ArgumentError => e
  status 422
  ::Fakecrm.logger.error("Unable to set unknown attribute: #{e.inspect}")
  {:error => "Unknown attribute #{e.inspect}"}.to_json 
end
destroy_one(resource, primary_key) click to toggle source
# File lib/fakecrm/drop.rb, line 49
def destroy_one(resource, primary_key)
  resource.transaction do |t|
    resource.get!(primary_key).destroy
  end
  body {}.to_json
rescue ::DataMapper::ObjectNotFoundError
  status 404
end
update_one(resource, primary_key, data) click to toggle source
# File lib/fakecrm/drop.rb, line 24
def update_one(resource, primary_key, data)
  response = {}
  resource.transaction do |t|
    instance = resource.get!(primary_key)
    ::Fakecrm.logger.debug("Updating resource with: #{data.inspect}")
    instance.attributes = data

    if instance.save
      response = {}
    else
      status 422
      ::Fakecrm.logger.debug("Updating resouce failed: #{instance.errors.to_hash.inspect}")
      response = instance.errors.to_hash
    end
  end

  body response.to_json
rescue ::DataMapper::ObjectNotFoundError
  status 404
rescue ArgumentError => e
  status 422
  ::Fakecrm.logger.error("Unable to set unknown attribute: #{e.inspect}")
  {:error => "Unknown attribute #{e.inspect}"}.to_json 
end