Module: Mongoid::CollectionSnapshot
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid_collection_snapshot/version.rb,
lib/mongoid_collection_snapshot.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- VERSION =
'1.0.1'
Instance Method Summary collapse
- #collection_snapshot(name = nil) ⇒ Object
- #drop_snapshot_collections ⇒ Object
-
#ensure_at_most_two_instances_exist ⇒ Object
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.
-
#snapshot_session ⇒ Object
Override to supply custom database connection for snapshots.
Instance Method Details
#collection_snapshot(name = nil) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/mongoid_collection_snapshot.rb', line 29 def collection_snapshot(name=nil) if name snapshot_session["#{self.collection.name}.#{name}.#{slug}"] else snapshot_session["#{self.collection.name}.#{slug}"] end end |
#drop_snapshot_collections ⇒ Object
37 38 39 40 41 |
# File 'lib/mongoid_collection_snapshot.rb', line 37 def drop_snapshot_collections snapshot_session.collections.each do |collection| collection.drop if collection.name =~ /^#{self.collection.name}\.([^\.]+\.)?#{slug}$/ end end |
#ensure_at_most_two_instances_exist ⇒ Object
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.
47 48 49 50 51 52 |
# File 'lib/mongoid_collection_snapshot.rb', line 47 def ensure_at_most_two_instances_exist all_instances = self.class.order_by([[:created_at, :desc]]).to_a if all_instances.length > self.max_collection_snapshot_instances all_instances[self.max_collection_snapshot_instances..-1].each { |instance| instance.destroy } end end |
#snapshot_session ⇒ Object
Override to supply custom database connection for snapshots
55 56 57 |
# File 'lib/mongoid_collection_snapshot.rb', line 55 def snapshot_session Mongoid.default_session end |