Module: Mongoid::CollectionSnapshot
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/mongoid_collection_snapshot.rb,
lib/mongoid_collection_snapshot/version.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- DEFAULT_COLLECTION_KEY_NAME =
'*'
- VERSION =
'1.2.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
68 69 70 71 72 73 74 |
# File 'lib/mongoid_collection_snapshot.rb', line 68 def collection_snapshot(name = nil) if name snapshot_session["#{collection.name}.#{name}.#{slug}"] else snapshot_session["#{collection.name}.#{slug}"] end end |
#drop_snapshot_collections ⇒ Object
76 77 78 79 80 81 |
# File 'lib/mongoid_collection_snapshot.rb', line 76 def drop_snapshot_collections collections = Mongoid::Compatibility::Version.mongoid5? ? 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_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.
87 88 89 90 91 |
# File 'lib/mongoid_collection_snapshot.rb', line 87 def ensure_at_most_two_instances_exist all_instances = self.class.order_by([[:created_at, :desc]]).to_a return unless all_instances.length > max_collection_snapshot_instances all_instances[max_collection_snapshot_instances..-1].each(&:destroy) end |