Giter VIP home page Giter VIP logo

wisper-activerecord's Introduction

Wisper::ActiveRecord [WIP]

Build Status

Transparently publish all model changes to subscribers.

Installation

gem 'wisper-activerecord'

Usage

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

Contributing

Please submit a Pull Request with specs.

Running the specs

bundle exec rspec

wisper-activerecord's People

Contributors

krisleech avatar

Watchers

Neil Chambers avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.