data_snapshots
Simple, flexible data snapshotting for your Rails app.
Installation
Run the following command to add data_snapshots to your Gemfile:
bundle add data_snapshotsOnce installed, copy and run the migration:
bundle exec rails data_snapshots_engine:install:migrations
bundle exec rails db:migrateRegistering a model snapshot
A model snapshot is a collection of methods that run against a model instance. Methods will be passed the instance when the snapshot is generated.
You can register your snapshots in an initializer:
DataSnapshots.configure do |c|
c.register_snapshot name: :user_assignments do |methods|
methods[:total] = ->(instance) { instance.assignments.count }
methods[:total_complete] = ->(instance) { instance.assignments.where(complete: true).count }
end
endRegistering a generic snapshot
A generic snapshot is a collection of methods that will not be passed a model instance when the snapshot is generated. You can declare a snapshot as generic by passing model: false as an argument when registering the snapshot:
DataSnapshots.configure do |c|
c.register_snapshot name: :totals, model: false do |methods|
methods[:total_users] = ->() { User.count }
methods[:total_assignments] = ->() { Assignments.count }
end
endGenerating model snapshots
Model snapshots can either be generated on the individual instance:
user = User.last
user.generate_snapshot(name: :user_assignments)Or you can generate them with a collection of records:
DataSnapshots.generate_snapshots(name: :user_assignments, collection: User.all)Generating generic snapshots
Generic snapshots can be created by passing the name of the snapshot to DataSnapshots.generate_snapshot
DataSnapshots.generate_snapshot(name: :total_users)Fetching snapshots
data_snapshots comes with a <NAME>_snapshots method so its easy to fetch all the snapshots taken against a particular instance:
user.user_assignments_snapshotsYou can fetch your generic snapshots by calling DataSnapshots.fetch_snapshots and passing the name of your snapshot:
DataSnapshots.fetch_snapshots(name: :total_users)This method can also be used to fetch ALL model snapshots with the given name.
Viewing snapshot data
Snapshot data is stored as JSON in the database and can be accessed easily:
snapshot.data # => { 'key' => 'value' }License
The gem is available as open source under the terms of the MIT License.