Class: Condo::Backend::Mongoid

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document, Mongoid::Timestamps
Defined in:
lib/condo/backend/mongoid.rb

Overview

The following data needs to be stored in any backend

> provider_namespace (for handling multiple upload controllers, defaults to global)

> provider_name (amazon, rackspace, google, azure etc)

> provider_location (US West (Oregon) Region, Asia Pacific (Singapore) Region etc)

> user_id (the identifier for the current user as a string)

> file_name (the original upload file name)

> file_size (the file size as indicated by the client)

> file_id (some sort of identifying hash provided by the client)

> bucket_name (the name of the users bucket)

> object_key (the path to the object in the bucket)

> object_options (custom options that were applied to this object - public/private etc)

> resumable_id (the id of the chunked upload)

> resumable (true if a resumable upload - must be set)

> custom_params (application specific data - needs to be serialised and de-serialised)

> date_created (the date the upload was started)

> Each backend should have an ID that uniquely identifies an entry - id or upload_id

Backends should inherit this class, set themselves as the backend and define the following:

Class Methods:

> check_exists (upload_id) returns nil or an entry where all fields match

check_exists (file_name, file_size, file_id) so same logic for this

> add_entry (file_name, file_size, file_id, provider_name, provider_location, bucket_name, object_key)

Instance Methods:

> update_entry (resumable_id)

> remove_entry (upload_id)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_entry(params) ⇒ Object

Adds a new upload entry into the database



79
80
81
82
83
84
85
86
# File 'lib/condo/backend/mongoid.rb', line 79

def self.add_entry(params)
	params = {}.merge(params)
	params.delete(:upload_id) if params[:upload_id].present?
	params.delete(:id) if params[:id].present?
	params.delete(:resumable_id) if params[:resumable_id].present?
	
	self.create!(params)
end

.check_exists(params) ⇒ Object

Checks for an exact match in the database given a set of parameters



69
70
71
72
73
74
# File 'lib/condo/backend/mongoid.rb', line 69

def self.check_exists(params)
	params = {}.merge(params)
	params[:user_id] = params[:user_id] if params[:user_id].present?
	params[:id] = params.delete(:upload_id) if params[:upload_id].present?
	self.where(params).first
end

Instance Method Details

#date_createdObject



112
113
114
# File 'lib/condo/backend/mongoid.rb', line 112

def date_created
	self[:created_at]
end

#remove_entryObject

Deletes reference to the upload



100
101
102
# File 'lib/condo/backend/mongoid.rb', line 100

def remove_entry
	self.destroy
end

#update_entry(params) ⇒ Object

Updates self with the passed in parameters

Raises:

  • (Mongoid::Errors::UnsavedDocument)


91
92
93
94
95
# File 'lib/condo/backend/mongoid.rb', line 91

def update_entry(params)
	result = self.update_attributes(params)
	raise Mongoid::Errors::UnsavedDocument.new(self, self) if result == false
	self
end

#upload_idObject

Attribute accessors to comply with the backend spec



108
109
110
# File 'lib/condo/backend/mongoid.rb', line 108

def upload_id
	self[:id]
end