QueryDam
QueryDam is a tool for observing changes in ActiveRecord relation results.
Installation
Add it to your gemfile Gemfile:
gem 'query_dam'
Usage
First, include QueryDam::Trackable in every model you with to track.
class Item < ActiveRecord::Base
include QueryDam::Trackable
end
Then start watching updates of relation:
relation = Item.where(name: 'pencil')
tracking_key = QueryDam.watch_relation(relation)
To get updates, call QueryDam.get_updates(tracking_key).
The result is a hash with 3 keys:
:model - the model of the relation,
:updates - relation containing records that were updated or entered the scope of the relation
:exclusions - array of ids of records that left the scope of the relation
The call to get_updates clears updates/exclusions.
Item.create(name: 'pencil')
QueryDam.get_updates(tracking_key)
# =>
# {:model=>Item,
# :updates=>#<ActiveRecord::Relation [#<Item id: 1, name: "pencil">]>,
# :exclusions=>[]}
QueryDam.get_updates(tracking_key)
# =>
# {:model=>Item,
# :updates=>#<ActiveRecord::Relation []>,
# :exclusions=>[]}
Item.first.destory
QueryDam.get_updates(tracking_key)
# =>
# {:model=>Item,
# :updates=>#<ActiveRecord::Relation []>,
# :exclusions=>[1]}
Contributing
- Fork it ( https://github.com/[my-github-username]/query_dam/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request