mongoid-audit_log 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/lib/mongoid/audit_log.rb +11 -10
- data/lib/mongoid/audit_log/caches.rb +20 -0
- data/lib/mongoid/audit_log/config.rb +12 -0
- data/lib/mongoid/audit_log/entry.rb +9 -0
- data/lib/mongoid/audit_log/version.rb +1 -1
- data/spec/mongoid/audit_log_spec.rb +24 -0
- metadata +4 -3
- data/lib/mongoid/audit_log/actions.rb +0 -6
    
        checksums.yaml
    CHANGED
    
    | @@ -1,15 +1,15 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            !binary "U0hBMQ==":
         | 
| 3 3 | 
             
              metadata.gz: !binary |-
         | 
| 4 | 
            -
                 | 
| 4 | 
            +
                OGI3Njg4MzMwNTAyMGU5NGU4YTY3YWJiZDVjYThlYzhhNTA4YTY1YQ==
         | 
| 5 5 | 
             
              data.tar.gz: !binary |-
         | 
| 6 | 
            -
                 | 
| 6 | 
            +
                Y2FhZTBlOTY3MTNhY2JhM2RmZTUyOTAzN2E3NDdjOWM4ZjM3YTgyYg==
         | 
| 7 7 | 
             
            !binary "U0hBNTEy":
         | 
| 8 8 | 
             
              metadata.gz: !binary |-
         | 
| 9 | 
            -
                 | 
| 10 | 
            -
                 | 
| 11 | 
            -
                 | 
| 9 | 
            +
                MjhlZDZkNmZmODZhNTM3OTBhMzFiMjhiMjQ2MmZhZTg1ODFmYmZmZjM2OWIz
         | 
| 10 | 
            +
                ODUzN2ZlMjgyZTFlNDJiZTMxZTgyNjU1YTdjMmM4NDIxMGM3NmIxMDcyNDdl
         | 
| 11 | 
            +
                NjAyZmUwYjM4MzA3NjY0YmVhNTliNzNmYTQxZWVkMjY0ZGI4ODc=
         | 
| 12 12 | 
             
              data.tar.gz: !binary |-
         | 
| 13 | 
            -
                 | 
| 14 | 
            -
                 | 
| 15 | 
            -
                 | 
| 13 | 
            +
                N2VlYjBmZWQ3NTFmNzhjZTNiMzY1ZTNkZGNhNjkwMGFjNDBhNTk1NTc5ODRl
         | 
| 14 | 
            +
                NjMzOTU1MGI2NjI0OTk3ODhiNWI5MjA4NjFjZDNlYjcyOGM2ZTQ0Y2E1MTAy
         | 
| 15 | 
            +
                MDg0MDdlMjU5NTVkYjI1NWI5YjZhYWZiYTRiNjUyMzc4NzNkMTc=
         | 
    
        data/lib/mongoid/audit_log.rb
    CHANGED
    
    | @@ -1,6 +1,7 @@ | |
| 1 1 | 
             
            require "mongoid/audit_log/version"
         | 
| 2 | 
            -
            require "mongoid/audit_log/ | 
| 2 | 
            +
            require "mongoid/audit_log/config"
         | 
| 3 3 | 
             
            require "mongoid/audit_log/entry"
         | 
| 4 | 
            +
            require "mongoid/audit_log/caches"
         | 
| 4 5 | 
             
            require "mongoid/audit_log/changes"
         | 
| 5 6 | 
             
            require "mongoid/audit_log/embedded_changes"
         | 
| 6 7 |  | 
| @@ -8,9 +9,6 @@ module Mongoid | |
| 8 9 | 
             
              module AuditLog
         | 
| 9 10 | 
             
                extend ActiveSupport::Concern
         | 
| 10 11 |  | 
| 11 | 
            -
                mattr_accessor :modifier_class_name
         | 
| 12 | 
            -
                self.modifier_class_name = 'User'
         | 
| 13 | 
            -
             | 
| 14 12 | 
             
                included do
         | 
| 15 13 | 
             
                  has_many :audit_log_entries, :as => :audited,
         | 
| 16 14 | 
             
                    :class_name => 'Mongoid::AuditLog::Entry'
         | 
| @@ -49,12 +47,15 @@ module Mongoid | |
| 49 47 | 
             
                end
         | 
| 50 48 |  | 
| 51 49 | 
             
                def save_audit_log_entry(action)
         | 
| 52 | 
            -
                   | 
| 53 | 
            -
                     | 
| 54 | 
            -
             | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 50 | 
            +
                  unless action == :update && @_audit_log_changes.all.blank?
         | 
| 51 | 
            +
                    Mongoid::AuditLog::Entry.create!(
         | 
| 52 | 
            +
                      :action => action,
         | 
| 53 | 
            +
                      :audited_type => self.class,
         | 
| 54 | 
            +
                      :audited_id => id,
         | 
| 55 | 
            +
                      :tracked_changes => @_audit_log_changes.all,
         | 
| 56 | 
            +
                      :caches => Mongoid::AuditLog::Caches.new(self).all
         | 
| 57 | 
            +
                    )
         | 
| 58 | 
            +
                  end
         | 
| 58 59 | 
             
                end
         | 
| 59 60 | 
             
              end
         | 
| 60 61 | 
             
            end
         | 
| @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            module Mongoid
         | 
| 2 | 
            +
              module AuditLog
         | 
| 3 | 
            +
                class Caches
         | 
| 4 | 
            +
                  attr_reader :model
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                  def initialize(model)
         | 
| 7 | 
            +
                    @model = model
         | 
| 8 | 
            +
                  end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  def all
         | 
| 11 | 
            +
                    return nil unless Mongoid::AuditLog.cache_fields.present?
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                    Mongoid::AuditLog.cache_fields.inject({}) do |memo, field|
         | 
| 14 | 
            +
                      memo[field] = model.send(field) if model.respond_to?(field)
         | 
| 15 | 
            +
                      memo
         | 
| 16 | 
            +
                    end
         | 
| 17 | 
            +
                  end
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
              end
         | 
| 20 | 
            +
            end
         | 
| @@ -7,6 +7,7 @@ module Mongoid | |
| 7 7 | 
             
                  field :action, :type => Symbol
         | 
| 8 8 | 
             
                  field :tracked_changes, :type => Hash, :default => {}
         | 
| 9 9 | 
             
                  field :modifier_id, :type => String
         | 
| 10 | 
            +
                  field :caches, :type => Hash
         | 
| 10 11 |  | 
| 11 12 | 
             
                  belongs_to :audited, :polymorphic => true
         | 
| 12 13 |  | 
| @@ -48,6 +49,14 @@ module Mongoid | |
| 48 49 |  | 
| 49 50 | 
             
                    @modifier = modifier
         | 
| 50 51 | 
             
                  end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
                  def method_missing(sym, *args, &block)
         | 
| 54 | 
            +
                    if caches.present? && caches[sym.to_s].present?
         | 
| 55 | 
            +
                      caches[sym.to_s]
         | 
| 56 | 
            +
                    else
         | 
| 57 | 
            +
                      super
         | 
| 58 | 
            +
                    end
         | 
| 59 | 
            +
                  end
         | 
| 51 60 | 
             
                end
         | 
| 52 61 | 
             
              end
         | 
| 53 62 | 
             
            end
         | 
| @@ -51,6 +51,24 @@ module Mongoid | |
| 51 51 | 
             
                    end
         | 
| 52 52 | 
             
                  end
         | 
| 53 53 |  | 
| 54 | 
            +
                  it 'saves cache fields from configuration' do
         | 
| 55 | 
            +
                    tmp = Mongoid::AuditLog.cache_fields
         | 
| 56 | 
            +
                    Mongoid::AuditLog.cache_fields << :name
         | 
| 57 | 
            +
             | 
| 58 | 
            +
                    AuditLog.record do
         | 
| 59 | 
            +
                      product = Product.create!(:name => 'Foo bar')
         | 
| 60 | 
            +
                      product.update_attributes(:name => 'Bar baz')
         | 
| 61 | 
            +
                      product.destroy
         | 
| 62 | 
            +
             | 
| 63 | 
            +
                      product.audit_log_entries.count.should == 3
         | 
| 64 | 
            +
                      product.audit_log_entries.each do |entry|
         | 
| 65 | 
            +
                        entry.name.should be_present
         | 
| 66 | 
            +
                      end
         | 
| 67 | 
            +
                    end
         | 
| 68 | 
            +
             | 
| 69 | 
            +
                    Mongoid::AuditLog.cache_fields = tmp
         | 
| 70 | 
            +
                  end
         | 
| 71 | 
            +
             | 
| 54 72 | 
             
                  it 'saves the modifier if passed' do
         | 
| 55 73 | 
             
                    user = User.create!
         | 
| 56 74 | 
             
                    AuditLog.record(user) do
         | 
| @@ -120,6 +138,12 @@ module Mongoid | |
| 120 138 | 
             
                        'variants' => [{ 'sku' => ['sku', 'newsku'] }]
         | 
| 121 139 | 
             
                      }
         | 
| 122 140 | 
             
                    end
         | 
| 141 | 
            +
             | 
| 142 | 
            +
                    it 'does not save blank updates' do
         | 
| 143 | 
            +
                      product = Product.create!(:name => 'Foo bar')
         | 
| 144 | 
            +
                      product.update_attributes(:name => 'Foo bar')
         | 
| 145 | 
            +
                      product.audit_log_entries.length.should == 1
         | 
| 146 | 
            +
                    end
         | 
| 123 147 | 
             
                  end
         | 
| 124 148 |  | 
| 125 149 | 
             
                  context 'destroy' do
         | 
    
        metadata
    CHANGED
    
    | @@ -1,14 +1,14 @@ | |
| 1 1 | 
             
            --- !ruby/object:Gem::Specification
         | 
| 2 2 | 
             
            name: mongoid-audit_log
         | 
| 3 3 | 
             
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            -
              version: 0.0. | 
| 4 | 
            +
              version: 0.0.2
         | 
| 5 5 | 
             
            platform: ruby
         | 
| 6 6 | 
             
            authors:
         | 
| 7 7 | 
             
            - Ben Crouse
         | 
| 8 8 | 
             
            autorequire: 
         | 
| 9 9 | 
             
            bindir: bin
         | 
| 10 10 | 
             
            cert_chain: []
         | 
| 11 | 
            -
            date: 2013-10- | 
| 11 | 
            +
            date: 2013-10-14 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: mongoid
         | 
| @@ -66,8 +66,9 @@ files: | |
| 66 66 | 
             
            - README.md
         | 
| 67 67 | 
             
            - Rakefile
         | 
| 68 68 | 
             
            - lib/mongoid/audit_log.rb
         | 
| 69 | 
            -
            - lib/mongoid/audit_log/ | 
| 69 | 
            +
            - lib/mongoid/audit_log/caches.rb
         | 
| 70 70 | 
             
            - lib/mongoid/audit_log/changes.rb
         | 
| 71 | 
            +
            - lib/mongoid/audit_log/config.rb
         | 
| 71 72 | 
             
            - lib/mongoid/audit_log/embedded_changes.rb
         | 
| 72 73 | 
             
            - lib/mongoid/audit_log/entry.rb
         | 
| 73 74 | 
             
            - lib/mongoid/audit_log/version.rb
         |