Giter Site home page Giter Site logo

mongoid-audit_log's People

Contributors

bencrouse avatar mmartyn avatar mttdffy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mongoid-audit_log's Issues

Changes from embedded relationships

Ran into two issues around embedded relationships.

The first I'm not sure what the best solution is. If #save is called on the embedded relationship, it doesn't fire the parent callbacks and the audit log entry is not saved. I "fixed" it by adding a module thats included in embedded relationships that I want to fire a log entry that just call up to the parents methods before and after update.

The second issue is with how changes are logged for embedded relationships when they do save. When tracking changes from an embedded relationship, they end up looking something like

{
  "skus" => [
    { "inventory" => [10, 8] },
    { "inventory" => [10, 5] }
  ]
}

Theres no way of knowing which embedded document had what changes.

I patched the Mongoid::AuditLog::Changes.extract_from method to look like this:

  def self.extract_from(value)
    if value.is_a?(Hash)
      raise ArgumentError, 'does not support hashes'
    elsif value.is_a?(Enumerable)
      changes = value.map do |model|
        change = Mongoid::AuditLog::Changes.new(model).all
        change.present? ? { id: model.id.to_s, changes: change } : change
      end

      changes.reject(&:blank?)
    else
      Mongoid::AuditLog::Changes.new(value).all
    end
  end

where if its passed an Enumerable, the change becomes a Hash with two keys, id and changes. So the above would turn into something like:

{
  "skus" => [
    { 
      "id" => "12345", 
      "changes" => { "inventory" => [10, 8] } 
    },
    { 
      "id" => "6789", 
      "changes" => { "inventory" => [10, 5] } 
    }
  ]
}

I don't think its a great solution, but is in the neighbor of what I think it needs. Thoughts?

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.