mongoid-audit_log 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- OWYyMWJjOWFkN2ExYzA0ZWNhODkzZjQ2MTIxZDcxODNmMjNkN2QwOQ==
4
+ OGI3Njg4MzMwNTAyMGU5NGU4YTY3YWJiZDVjYThlYzhhNTA4YTY1YQ==
5
5
  data.tar.gz: !binary |-
6
- MjJkOTRiNzcwNTI1NTg3NzI2ZDJjNDFlNjA5ZGQwMTM0OTI1YjdlMg==
6
+ Y2FhZTBlOTY3MTNhY2JhM2RmZTUyOTAzN2E3NDdjOWM4ZjM3YTgyYg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NzY2ODJiMmQ3YmQ2MThlYjhiOWJmMWFkN2JkOGVhYjRjYjQ2MmRjMjkzY2E1
10
- MjM3Y2Q2MGVlZjQzZmQ5MDg3OGUyZTY3MDA0OTA0OWIxNmFkNTA4ZTY3OWQw
11
- MTM5Yzk3YmVjMDYwNDBhMDg3NTc0MzJjODE0NjcyZWM3MWQ2ZTA=
9
+ MjhlZDZkNmZmODZhNTM3OTBhMzFiMjhiMjQ2MmZhZTg1ODFmYmZmZjM2OWIz
10
+ ODUzN2ZlMjgyZTFlNDJiZTMxZTgyNjU1YTdjMmM4NDIxMGM3NmIxMDcyNDdl
11
+ NjAyZmUwYjM4MzA3NjY0YmVhNTliNzNmYTQxZWVkMjY0ZGI4ODc=
12
12
  data.tar.gz: !binary |-
13
- Nzk0MDY1MGZmYzdmZjIzZjI5ZjQ4ZDJjNjIxNTM2YjBiMGVmMTk1YTliYzY1
14
- NmY0ZmJmNTk5NjM3NmYyZDNmNTQxOTQ2NjYzYTJlNmZmZjBlOTc4YTcxMzky
15
- YzY2ODQxODgyNDI5MGRjM2M5MTk0N2RmZjQ5Mjc4OTkxMWI0ZTY=
13
+ N2VlYjBmZWQ3NTFmNzhjZTNiMzY1ZTNkZGNhNjkwMGFjNDBhNTk1NTc5ODRl
14
+ NjMzOTU1MGI2NjI0OTk3ODhiNWI5MjA4NjFjZDNlYjcyOGM2ZTQ0Y2E1MTAy
15
+ MDg0MDdlMjU5NTVkYjI1NWI5YjZhYWZiYTRiNjUyMzc4NzNkMTc=
@@ -1,6 +1,7 @@
1
1
  require "mongoid/audit_log/version"
2
- require "mongoid/audit_log/actions"
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
- Mongoid::AuditLog::Entry.create!(
53
- :action => action,
54
- :audited_type => self.class,
55
- :audited_id => id,
56
- :tracked_changes => @_audit_log_changes.all
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
@@ -0,0 +1,12 @@
1
+ module Mongoid
2
+ module AuditLog
3
+ mattr_accessor :actions
4
+ self.actions = [:create, :update, :destroy]
5
+
6
+ mattr_accessor :modifier_class_name
7
+ self.modifier_class_name = 'User'
8
+
9
+ mattr_accessor :cache_fields
10
+ self.cache_fields = []
11
+ end
12
+ 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
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module AuditLog
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  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.1
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-10 00:00:00.000000000 Z
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/actions.rb
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
@@ -1,6 +0,0 @@
1
- module Mongoid
2
- module AuditLog
3
- mattr_accessor :actions
4
- self.actions = [:create, :update, :destroy]
5
- end
6
- end