Sha256: 80427d8ca98e2555e58cdb9e56ee8e43038d6fb334a82887bebca7e0d47668fc
Contents?: true
Size: 1.88 KB
Versions: 1
Compression:
Stored size: 1.88 KB
Contents
require 'mongo' require 'volt/models' class StoreTasks < Volt::TaskHandler def initialize(channel = nil, dispatcher = nil) @channel = channel @dispatcher = dispatcher end def db @@db ||= Volt::DataStore.fetch end def load_model(collection, path, data) model_name = collection.singularize.camelize # Fetch the model collection = store.send(:"_#{path[-2]}") # See if the model has already been made collection.find(_id: data[:_id]).fetch_first do |model| # Otherwise assign to the collection model ||= collection # Create a buffer buffer = model.buffer # Assign the changed data to the buffer buffer.assign_attributes(data, false, true) buffer end end def save(collection, path, data) data = data.symbolize_keys promise = nil Volt::Model.no_validate do promise = load_model(collection, path, data) end # On the backend, the promise is resolved before its returned, so we can # return from within it. # # Pass the channel as a thread-local so that we don't update the client # who sent the update. # # return another promise return promise.then do |model| Thread.current['in_channel'] = @channel save_promise = model.save!.then do |result| next nil end Thread.current['in_channel'] = nil next save_promise end end def delete(collection, id) # Load the model, then call .destroy on it store.send(:"_#{collection}").where(_id: id).fetch_first do |model| if model if model.can_delete? db[collection].remove('_id' => id) else raise "Permissions did not allow #{collection} #{id} to be deleted." end QueryTasks.live_query_pool.updated_collection(collection, @channel) else raise "Could not find #{id} in #{collection}" end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
volt-0.8.27.beta6 | app/volt/tasks/store_tasks.rb |