fixed error with indexes if collection does not exist

This commit is contained in:
Anton Martynov 2015-09-18 12:28:16 +03:00
parent 947c7cdac4
commit 7b1a2c1dc5
12 changed files with 71 additions and 12 deletions

View File

@ -6,10 +6,24 @@ require "exceptions/invalid_privileges"
module Connectors module Connectors
class Base class Base
# если хотим создавать индексы при старте приложения, нужно сначала создать коллекцию
def initialize db
names = db.collection_names
unless names.include?(self.collection_name)
db.create_collection(self.collection_name)
puts "Collection '#{self.collection_name}' has been created"
end
self.collection = db.collection(self.collection_name)
end
def create_indexes def create_indexes
end end
def collection_name
raise "owerride me"
end
# Yes, we can implement connectors without attr_accessor, storing collection directly # Yes, we can implement connectors without attr_accessor, storing collection directly
# in instance variable like # in instance variable like
# @collection = db.collection('users') # @collection = db.collection('users')

View File

@ -2,7 +2,11 @@ module Connectors
class Filter < Base class Filter < Base
def initialize(db) def initialize(db)
self.collection = db.collection('filters') super(db)
end
def collection_name
'filters'
end end
def available_images provider def available_images provider

View File

@ -9,7 +9,11 @@ module Connectors
Helpers::UpdateCommand Helpers::UpdateCommand
def initialize(db) def initialize(db)
self.collection = db.collection('images') super(db)
end
def collection_name
'images'
end end
def images(provider=nil) def images(provider=nil)

View File

@ -7,7 +7,11 @@ module Connectors
Helpers::DeleteCommand Helpers::DeleteCommand
def initialize(db) def initialize(db)
self.collection = db.collection('keys') super(db)
end
def collection_name
'keys'
end end
def key(id, scope=nil) def key(id, scope=nil)

View File

@ -8,7 +8,11 @@ module Connectors
def initialize(db) def initialize(db)
@collection = db.collection('projects') super(db)
end
def collection_name
'projects'
end end
def is_project_exists?(project) def is_project_exists?(project)

View File

@ -8,7 +8,11 @@ module Connectors
def initialize(db) def initialize(db)
@collection = db.collection('project_templates') super(db)
end
def collection_name
'project_templates'
end end
end end
end end

View File

@ -6,7 +6,11 @@ module Connectors
Helpers::ListCommand Helpers::ListCommand
def initialize(db) def initialize(db)
self.collection = db.collection('reports') super(db)
end
def collection_name
'reports'
end end
def save_report r def save_report r

View File

@ -5,7 +5,11 @@ module Connectors
def initialize(db) def initialize(db)
self.collection = db.collection('servers') super(db)
end
def collection_name
'servers'
end end
def servers_find(query, fields=nil) def servers_find(query, fields=nil)

View File

@ -7,7 +7,11 @@ module Connectors
Helpers::UpdateCommand Helpers::UpdateCommand
def initialize(db) def initialize(db)
self.collection = db.collection('stacks') super(db)
end
def collection_name
'stacks'
end end
def stacks(options={}) def stacks(options={})

View File

@ -8,7 +8,11 @@ module Connectors
Helpers::UpdateCommand Helpers::UpdateCommand
def initialize(db) def initialize(db)
self.collection = db.collection('stack_templates') super(db)
end
def collection_name
"stack_templates"
end end
def stack_templates(provider=nil) def stack_templates(provider=nil)

View File

@ -2,7 +2,11 @@ module Connectors
class Statistic < Base class Statistic < Base
def initialize(db) def initialize(db)
self.collection = db.collection('statistic') super(db)
end
def collection_name
'statistic'
end end
def insert_statistic(user, path, method, body, response_code) def insert_statistic(user, path, method, body, response_code)
@ -45,7 +49,8 @@ module Connectors
private private
def mongo_sort_order(order) def mongo_sort_order(order)
raise "Wrong sort order" unless %w(asc desc).include?(order) # asc by default and if order id invalid value, it is not a reason for response with code 500
#raise "Wrong sort order" unless %w(asc desc).include?(order)
order == 'asc' ? 1 : -1 order == 'asc' ? 1 : -1
end end

View File

@ -8,7 +8,11 @@ module Connectors
Helpers::UpdateCommand Helpers::UpdateCommand
def initialize(db) def initialize(db)
self.collection = db.collection('users') super(db)
end
def collection_name
'users'
end end
def user_auth user, password def user_auth user, password