GithubHelp home page GithubHelp logo

luke-hawk / audited_async Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nocopo/audited_async

0.0 0.0 0.0 47 KB

Asynchronous audits for Rails

License: MIT License

Ruby 98.76% Shell 1.24%

audited_async's Introduction

AuditedAsync

CircleCI

AuditedAsync is a plugin for the audited gem which allows to create audits asynchronously using ActiveJob.

It works by injecting the async option into audited model option using functional programming. If enabled, it'll move audit creation logic into an ActiveJob instance, then it's sent to the queue to be executed later.

Installation

Add this line to your application's Gemfile, right after audited gem:

gem 'audited'
gem 'audited_async'

And then execute:

$ bundle

Usage

class Post < ApplicationRecord
  audited async: true
end

Depending on your active job adapter, you may need to make the queue name visible to the adapter.

Sidekiq

# config/sidekiq.yml
...
:queues:
  - [audits, 1] # add this line

All done! Although you can optionally configure some more stuff, check below.

Enabling it programmatically

# config/initializers/audited_async.rb

AuditedAsync.configure do |config|
  config.enabled = Rails.env.production?
end

Changing Job execution

# config/initializers/audited_async.rb

AuditedAsync.configure do |config|
  config.job_name  = 'JobityJob'
  config.job_options = { wait: 1.second }
end

Create your own job:

class JobityJob < ApplicationJob
  queue_as :audits

  def perform(audit_info)
    # audit_info = {
    #   class_name:      'Post',
    #   record_id:       2,
    #   audited_changes: "{\"json_stringified_changes\": \"with_values\"}",
    #   action:          one of %w[create update destroy],
    #   comment:         there will be some string here if audited comments are enabled,
    # }

    # ...
    # run your logic
    # ...

    # job must have this line at the end
    class_name.constantize.send(:write_audit, attributes)
    # attributes = {
    #   audited_changes: {hash_changes: :with_values},
    #   action:          one of %w[create update destroy],
    #   comment:         comment, if enabled
    # }
  end
end

Limitations

  • Audits for destroying an object are subject to soft delete, hard deleted records are ignored, so if you are using some library like paranoia or discard, destroying an object will still create audits, regardless model scoping.
  • Attributes passed down to job are limited to serializable attributes, you can find a list here, other than that would throw an error.

To see how the default job performs, look here.

Sample App

https://github.com/leonardofalk/audited_async_sample.git

Development

Checkout the repository, execute bundle install and you're good to go.

Testing

There are some unit tests now but integration tests are missing. You can run the suite by executing rspec.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/leonardofalk/audited_async. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

To Do

  • Elaborate integration test cases.

License

The gem is available as open source under the terms of the MIT License.

audited_async's People

Contributors

courtsimas avatar imoskalets avatar leonardofalk avatar luke-hawk 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.