Transparently publish all model changes to subscribers.
gem 'wisper-activerecord'
Our model
class Meeting < ActiveRecord::Base
end
Lets subscribe a listener
Wisper::ActiveRecord.subscribe(Meeting, to: Auditor.new)
Which simply logs all events in memory
class Auditor
include Singleton
attr_accessor :audt
def initialize
@audit = []
end
def on_create(subject)
audit.push(audit_for(‘create’, subject))
end
def on_update(subject)
audit.push(audit_for(‘update’, subject))
end
def on_destroy(subject)
audit.push(audit_for(‘destroy’, subject))
end
def self.audit
instance.audit
end
private
def audit_for(action, subject)
{
action: action,
subject_id: subject.id,
subject_class: subject.class.to_s,
changes: subject.changes,
created_at: Time.now
}
end
end
Do some CRUD
Meeting.create(:description => ‘Team Retrospective’, :starts_at => Time.now + 2.days)
meeting = Meeting.find(1)
meeting.starts_at = Time.now + 2.months
meeting.save
And check the audit
Auditor.audit # => […]
Supports:
- create, update, destroy
- update_attribute
- regular setter
Please submit a Pull Request with specs.
bundle exec rspec