module Mongoid::CollectionSnapshot
Constants
- DEFAULT_COLLECTION_KEY_NAME
- VERSION
Public Instance Methods
collection_snapshot(name = nil)
click to toggle source
# File lib/mongoid-collection-snapshot.rb, line 75 def collection_snapshot(name = nil) if name snapshot_session["#{collection.name}.#{name}.#{slug}"] else snapshot_session["#{collection.name}.#{slug}"] end end
documents(name = nil)
click to toggle source
Mongoid
documents on this snapshot.
# File lib/mongoid-collection-snapshot.rb, line 29 def documents(name = nil) self.class.document_classes ||= {} class_name = "#{self.class.name}#{id}#{name}".underscore.camelize key = "#{class_name}-#{name || DEFAULT_COLLECTION_KEY_NAME}" self.class.document_classes[key] ||= begin document_block = document_blocks[name || DEFAULT_COLLECTION_KEY_NAME] if document_blocks collection_name = collection_snapshot(name).name klass = Class.new do include Mongoid::Document if Mongoid::Compatibility::Version.mongoid5? cattr_accessor :mongo_client else cattr_accessor :mongo_session end instance_eval(&document_block) if document_block end if Mongoid::Compatibility::Version.mongoid6? # assign a name to the snapshot session session_id = snapshot_session.object_id.to_s Mongoid::Clients.set session_id, snapshot_session # tell the class to use the client by name klass.class_variable_set :@@storage_options, client: session_id, collection: collection_name elsif Mongoid::Compatibility::Version.mongoid5? klass.store_in collection: collection_name klass.mongo_client = snapshot_session else klass.store_in collection: collection_name klass.mongo_session = snapshot_session end Object.const_set(class_name, klass) klass end end
drop_snapshot_collections()
click to toggle source
# File lib/mongoid-collection-snapshot.rb, line 83 def drop_snapshot_collections collections = Mongoid::Compatibility::Version.mongoid5? || Mongoid::Compatibility::Version.mongoid6? ? snapshot_session.database.collections : snapshot_session.collections collections.each do |collection| collection.drop if collection.name =~ /^#{self.collection.name}\.([^\.]+\.)?#{slug}$/ end end
ensure_at_most_max_instances_exist()
click to toggle source
Since we should always be using the latest instance of this class, this method is called after each save - making sure only at most two instances exists should be sufficient to ensure that this data can be rebuilt live without corrupting any existing computations that might have a handle to the previous “latest” instance.
# File lib/mongoid-collection-snapshot.rb, line 94 def ensure_at_most_max_instances_exist all_instances = self.class.order_by([[:created_at, :desc]]).to_a max_collection_snapshot_instances = self.class.max_collection_snapshot_instances || 2 return unless all_instances.length > max_collection_snapshot_instances all_instances[max_collection_snapshot_instances..-1].each(&:destroy) end
snapshot_session()
click to toggle source
Override to supply custom database connection for snapshots
# File lib/mongoid-collection-snapshot.rb, line 102 def snapshot_session Mongoid::Compatibility::Version.mongoid5? || Mongoid::Compatibility::Version.mongoid6? ? Mongoid.default_client : Mongoid.default_session end