mongoid-audit 0.3.2 → 1.0.0.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,8 @@
1
- module Mongoid
2
- module Audit
3
- class Railtie < Rails::Railtie
4
- initializer "instantiate observers" do
5
- config.after_initialize do
6
- ::Mongoid::Audit.tracker_class.add_observer( ::Mongoid::Audit::Sweeper.instance )
7
- # install model observer and action controller filter
8
- if defined?(ActionController) and defined?(ActionController::Base)
9
- ActionController::Base.class_eval do
10
- before_filter { |controller| ::Mongoid::Audit::Sweeper.instance.before(controller) }
11
- after_filter { |controller| ::Mongoid::Audit::Sweeper.instance.after(controller) }
12
- end
13
- end
14
- end
15
- end
1
+ module Mongoid::Audit
2
+ class Railtie < Rails::Railtie
3
+ initializer "mongoid_audit.setup" do
4
+ #require_dependency 'history_tracker.rb' if Rails.env.development?
16
5
  end
17
6
  end
18
- end
7
+ end
8
+
@@ -1,264 +1,14 @@
1
- module Mongoid::Audit
2
- module Trackable
3
- extend ActiveSupport::Concern
4
-
5
- module ClassMethods
6
- def track_history(options={})
7
- scope_name = self.collection_name.to_s.singularize.to_sym
8
- default_options = {
9
- :on => :all,
10
- :except => [],
11
- :modifier_field => :modifier,
12
- :version_field => :version,
13
- :scope => scope_name,
14
- :track_create => false,
15
- :track_update => true,
16
- :track_destroy => false,
17
- }
18
-
19
- options = default_options.merge(options)
20
-
21
- # normalize except fields
22
- # manually ensure _id, id, version will not be tracked in history
23
- options[:except] = [options[:except]] unless options[:except].is_a? Array
24
- options[:except] << options[:version_field]
25
- options[:except] += [:created_at, :updated_at, :deleted_at, :c_at, :u_at]
26
- options[:except] << "#{options[:modifier_field]}_id".to_sym
27
- options[:except] += [:_id, :id]
28
- options[:except] = options[:except].map(&:to_s).flatten.compact.uniq
29
- options[:except].map(&:to_s)
30
-
31
- # normalize fields to track to either :all or an array of strings
32
- if options[:on] != :all
33
- options[:on] = [options[:on]] unless options[:on].is_a? Array
34
- options[:on] = options[:on].map(&:to_s).flatten.uniq
35
- end
36
-
37
- field options[:version_field].to_sym, :type => Integer
38
- belongs_to options[:modifier_field].to_sym, :class_name => Mongoid::Audit.modifier_class_name, inverse_of: nil
39
-
40
- include MyInstanceMethods
41
- extend SingletonMethods
42
-
43
- delegate :history_trackable_options, :to => 'self.class'
44
- delegate :track_history?, :to => 'self.class'
45
-
46
- before_update :track_update if options[:track_update]
47
- before_create :track_create if options[:track_create]
48
- before_destroy :track_destroy if options[:track_destroy]
49
-
50
- Mongoid::Audit.trackable_class_options ||= {}
51
- Mongoid::Audit.trackable_class_options[scope_name] = options
52
- end
53
-
54
- def track_history?
55
- enabled = Thread.current[track_history_flag]
56
- enabled.nil? ? true : enabled
57
- end
58
-
59
- def disable_tracking(&block)
60
- begin
61
- Thread.current[track_history_flag] = false
62
- yield
63
- ensure
64
- Thread.current[track_history_flag] = true
65
- end
66
- end
67
-
68
- def track_history_flag
69
- "mongoid_history_#{self.name.underscore}_trackable_enabled".to_sym
70
- end
71
- end
72
-
73
- module MyInstanceMethods
74
- def history_tracks
75
- @history_tracks ||= Mongoid::Audit.tracker_class.where(:scope => history_trackable_options[:scope], :association_chain => association_hash)
76
- end
77
-
78
- # undo :from => 1, :to => 5
79
- # undo 4
80
- # undo :last => 10
81
- def undo!(modifier, options_or_version=nil)
82
- versions = get_versions_criteria(options_or_version).to_a
83
- versions.sort!{|v1, v2| v2.version <=> v1.version}
84
-
85
- versions.each do |v|
86
- undo_attr = v.undo_attr(modifier)
87
- self.attributes = v.undo_attr(modifier)
88
- end
89
- save!
90
- end
91
-
92
- def redo!(modifier, options_or_version=nil)
93
- versions = get_versions_criteria(options_or_version).to_a
94
- versions.sort!{|v1, v2| v1.version <=> v2.version}
95
-
96
- versions.each do |v|
97
- redo_attr = v.redo_attr(modifier)
98
- self.attributes = redo_attr
99
- end
100
- save!
101
- end
102
-
103
- private
104
- def get_versions_criteria(options_or_version)
105
- if options_or_version.is_a? Hash
106
- options = options_or_version
107
- if options[:from] && options[:to]
108
- lower = options[:from] >= options[:to] ? options[:to] : options[:from]
109
- upper = options[:from] < options[:to] ? options[:to] : options[:from]
110
- versions = history_tracks.where( :version.in => (lower .. upper).to_a )
111
- elsif options[:last]
112
- versions = history_tracks.limit( options[:last] )
113
- else
114
- raise "Invalid options, please specify (:from / :to) keys or :last key."
115
- end
116
- else
117
- options_or_version = options_or_version.to_a if options_or_version.is_a?(Range)
118
- version_field_name = history_trackable_options[:version_field]
119
- version = options_or_version || self.attributes[version_field_name] || self.attributes[version_field_name.to_s]
120
- version = [ version ].flatten
121
- versions = history_tracks.where(:version.in => version)
122
- end
123
- versions.desc(:version)
124
- end
125
-
126
- def should_track_update?
127
- track_history? && !modified_attributes_for_update.blank?
128
- end
129
-
130
- def traverse_association_chain(node=self)
131
- list = node._parent ? traverse_association_chain(node._parent) : []
132
- list << association_hash(node)
133
- list
134
- end
135
-
136
- def association_hash(node=self)
137
-
138
- # We prefer to look up associations through the parent record because
139
- # we're assured, through the object creation, it'll exist. Whereas we're not guarenteed
140
- # the child to parent (embedded_in, belongs_to) relation will be defined
141
- if node._parent
142
- meta = _parent.relations.values.select do |relation|
143
- relation.class_name == node.class.to_s
144
- end.first
145
- end
146
-
147
- # if root node has no meta, and should use class name instead
148
- name = meta ? meta.key.to_s : node.class.name
149
-
150
- { 'name' => name, 'id' => node.id}
151
- end
152
-
153
- def modified_attributes_for_update
154
- @modified_attributes_for_update ||= if history_trackable_options[:on] == :all
155
- changes.reject do |k, v|
156
- history_trackable_options[:except].include?(k)
157
- end
158
- else
159
- changes.reject do |k, v|
160
- !history_trackable_options[:on].include?(k)
161
- end
162
-
163
- end
164
- end
165
-
166
- def modified_attributes_for_create
167
- @modified_attributes_for_create ||= attributes.inject({}) do |h, pair|
168
- k,v = pair
169
- h[k] = [nil, v]
170
- h
171
- end.reject do |k, v|
172
- history_trackable_options[:except].include?(k)
173
- end
174
- end
175
-
176
- def modified_attributes_for_destroy
177
- @modified_attributes_for_destroy ||= attributes.inject({}) do |h, pair|
178
- k,v = pair
179
- h[k] = [nil, v]
180
- h
181
- end
182
- end
183
-
184
- def history_tracker_attributes(method)
185
- return @history_tracker_attributes if @history_tracker_attributes
186
-
187
- @history_tracker_attributes = {
188
- :association_chain => traverse_association_chain,
189
- :scope => history_trackable_options[:scope],
190
- :modifier => send("#{history_trackable_options[:modifier_field].to_s}_id_changed?".to_sym) ? send(history_trackable_options[:modifier_field]) : nil,
191
- }
192
-
193
- original, modified = transform_changes(case method
194
- when :destroy then modified_attributes_for_destroy
195
- when :create then modified_attributes_for_create
196
- else modified_attributes_for_update
197
- end)
198
-
199
- @history_tracker_attributes[:original] = original
200
- @history_tracker_attributes[:modified] = modified
201
- @history_tracker_attributes
202
- end
203
-
204
- def track_update
205
- return unless should_track_update?
206
- current_version = (self.send(history_trackable_options[:version_field]) || 0 ) + 1
207
- self.send("#{history_trackable_options[:version_field]}=", current_version)
208
-
209
- track = Mongoid::Audit.tracker_class.create!(history_tracker_attributes(:update).merge(:version => current_version, :action => "update", :trackable => self))
210
-
211
- self.send("#{history_trackable_options[:modifier_field]}=", track.modifier)
212
-
213
- clear_memoization
214
- end
215
-
216
- def track_create
217
- return unless track_history?
218
- current_version = (self.send(history_trackable_options[:version_field]) || 0 ) + 1
219
- self.send("#{history_trackable_options[:version_field]}=", current_version)
220
-
221
- track = Mongoid::Audit.tracker_class.create!(history_tracker_attributes(:create).merge(:version => current_version, :action => "create", :trackable => self))
222
- self.send("#{history_trackable_options[:modifier_field]}=", track.modifier)
223
-
224
- clear_memoization
225
- end
226
-
227
- def track_destroy
228
- return unless track_history?
229
- current_version = (self.send(history_trackable_options[:version_field]) || 0 ) + 1
230
-
231
- track = Mongoid::Audit.tracker_class.create!(history_tracker_attributes(:destroy).merge(:version => current_version, :action => "destroy", :trackable => self))
232
- self.send("#{history_trackable_options[:modifier_field]}=", track.modifier)
233
-
234
- clear_memoization
235
- end
236
-
237
- def clear_memoization
238
- @history_tracker_attributes = nil
239
- @modified_attributes_for_create = nil
240
- @modified_attributes_for_update = nil
241
- @history_tracks = nil
242
- end
243
-
244
- def transform_changes(changes)
245
- original = {}
246
- modified = {}
247
- changes.each_pair do |k, v|
248
- o, m = v
249
- original[k] = o unless o.nil?
250
- modified[k] = m unless m.nil?
251
- end
252
-
253
- original.easy_diff modified
254
- end
255
-
256
- end
257
-
258
- module SingletonMethods
259
- def history_trackable_options
260
- @history_trackable_options ||= Mongoid::Audit.trackable_class_options[self.collection_name.to_s.singularize.to_sym]
261
- end
262
- end
1
+ module Trackable
2
+ extend ActiveSupport::Concern
3
+ include Mongoid::History::Trackable
4
+ included do
5
+ track_history({
6
+ track_create: true,
7
+ track_destroy: true,
8
+ track_update: true,
9
+ modifier_field: :updater,
10
+ except: ["created_at", "updated_at", "c_at", "u_at"],
11
+ })
263
12
  end
264
13
  end
14
+
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Audit
3
- VERSION = "0.3.2"
3
+ VERSION = "1.0.0.alpha.1"
4
4
  end
5
5
  end
@@ -17,14 +17,13 @@ Gem::Specification.new do |gem|
17
17
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
18
  gem.require_paths = ["lib"]
19
19
 
20
- gem.add_dependency 'easy_diff', '~> 0.0.2'
21
- gem.add_dependency 'mongoid', '~> 4.0.0.alpha1'
22
- gem.add_dependency 'rails-observers', '~> 0.1.2'
20
+ gem.add_dependency 'mongoid', '~> 4.0.0'
21
+ gem.add_dependency 'mongoid-history', '~> 0.4.4'
22
+ gem.add_dependency 'activesupport'
23
+ gem.add_dependency 'kaminari'
24
+ gem.add_dependency 'glebtv_mongoid_userstamp'
23
25
 
24
- gem.add_development_dependency "bundler", "~> 1.3"
26
+ gem.add_development_dependency "bundler"
25
27
  gem.add_development_dependency "rake"
26
- gem.add_development_dependency "rspec"
27
- gem.add_development_dependency "database_cleaner"
28
- gem.add_development_dependency "coveralls"
29
- gem.add_development_dependency "activesupport", '~> 4.0.0'
30
28
  end
29
+
metadata CHANGED
@@ -1,141 +1,113 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-audit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 1.0.0.alpha.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gleb Tv
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-03 00:00:00.000000000 Z
11
+ date: 2014-08-12 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: easy_diff
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - ~>
18
- - !ruby/object:Gem::Version
19
- version: 0.0.2
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - ~>
25
- - !ruby/object:Gem::Version
26
- version: 0.0.2
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: mongoid
29
15
  requirement: !ruby/object:Gem::Requirement
30
16
  requirements:
31
- - - ~>
17
+ - - "~>"
32
18
  - !ruby/object:Gem::Version
33
- version: 4.0.0.alpha1
19
+ version: 4.0.0
34
20
  type: :runtime
35
21
  prerelease: false
36
22
  version_requirements: !ruby/object:Gem::Requirement
37
23
  requirements:
38
- - - ~>
24
+ - - "~>"
39
25
  - !ruby/object:Gem::Version
40
- version: 4.0.0.alpha1
26
+ version: 4.0.0
41
27
  - !ruby/object:Gem::Dependency
42
- name: rails-observers
28
+ name: mongoid-history
43
29
  requirement: !ruby/object:Gem::Requirement
44
30
  requirements:
45
- - - ~>
31
+ - - "~>"
46
32
  - !ruby/object:Gem::Version
47
- version: 0.1.2
33
+ version: 0.4.4
48
34
  type: :runtime
49
35
  prerelease: false
50
36
  version_requirements: !ruby/object:Gem::Requirement
51
37
  requirements:
52
- - - ~>
38
+ - - "~>"
53
39
  - !ruby/object:Gem::Version
54
- version: 0.1.2
40
+ version: 0.4.4
55
41
  - !ruby/object:Gem::Dependency
56
- name: bundler
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '1.3'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ~>
67
- - !ruby/object:Gem::Version
68
- version: '1.3'
69
- - !ruby/object:Gem::Dependency
70
- name: rake
42
+ name: activesupport
71
43
  requirement: !ruby/object:Gem::Requirement
72
44
  requirements:
73
- - - '>='
45
+ - - ">="
74
46
  - !ruby/object:Gem::Version
75
47
  version: '0'
76
- type: :development
48
+ type: :runtime
77
49
  prerelease: false
78
50
  version_requirements: !ruby/object:Gem::Requirement
79
51
  requirements:
80
- - - '>='
52
+ - - ">="
81
53
  - !ruby/object:Gem::Version
82
54
  version: '0'
83
55
  - !ruby/object:Gem::Dependency
84
- name: rspec
56
+ name: kaminari
85
57
  requirement: !ruby/object:Gem::Requirement
86
58
  requirements:
87
- - - '>='
59
+ - - ">="
88
60
  - !ruby/object:Gem::Version
89
61
  version: '0'
90
- type: :development
62
+ type: :runtime
91
63
  prerelease: false
92
64
  version_requirements: !ruby/object:Gem::Requirement
93
65
  requirements:
94
- - - '>='
66
+ - - ">="
95
67
  - !ruby/object:Gem::Version
96
68
  version: '0'
97
69
  - !ruby/object:Gem::Dependency
98
- name: database_cleaner
70
+ name: glebtv_mongoid_userstamp
99
71
  requirement: !ruby/object:Gem::Requirement
100
72
  requirements:
101
- - - '>='
73
+ - - ">="
102
74
  - !ruby/object:Gem::Version
103
75
  version: '0'
104
- type: :development
76
+ type: :runtime
105
77
  prerelease: false
106
78
  version_requirements: !ruby/object:Gem::Requirement
107
79
  requirements:
108
- - - '>='
80
+ - - ">="
109
81
  - !ruby/object:Gem::Version
110
82
  version: '0'
111
83
  - !ruby/object:Gem::Dependency
112
- name: coveralls
84
+ name: bundler
113
85
  requirement: !ruby/object:Gem::Requirement
114
86
  requirements:
115
- - - '>='
87
+ - - ">="
116
88
  - !ruby/object:Gem::Version
117
89
  version: '0'
118
90
  type: :development
119
91
  prerelease: false
120
92
  version_requirements: !ruby/object:Gem::Requirement
121
93
  requirements:
122
- - - '>='
94
+ - - ">="
123
95
  - !ruby/object:Gem::Version
124
96
  version: '0'
125
97
  - !ruby/object:Gem::Dependency
126
- name: activesupport
98
+ name: rake
127
99
  requirement: !ruby/object:Gem::Requirement
128
100
  requirements:
129
- - - ~>
101
+ - - ">="
130
102
  - !ruby/object:Gem::Version
131
- version: 4.0.0
103
+ version: '0'
132
104
  type: :development
133
105
  prerelease: false
134
106
  version_requirements: !ruby/object:Gem::Requirement
135
107
  requirements:
136
- - - ~>
108
+ - - ">="
137
109
  - !ruby/object:Gem::Version
138
- version: 4.0.0
110
+ version: '0'
139
111
  description: Mongoid Audit
140
112
  email:
141
113
  - glebtv@gmail.com
@@ -143,33 +115,23 @@ executables: []
143
115
  extensions: []
144
116
  extra_rdoc_files: []
145
117
  files:
146
- - .coveralls.yml
147
- - .gitignore
148
- - .rspec
149
- - .ruby-gemset
150
- - .ruby-version
151
- - .travis.yml
118
+ - ".gitignore"
119
+ - ".ruby-gemset"
120
+ - ".ruby-version"
152
121
  - Gemfile
122
+ - Gemfile.lock
153
123
  - LICENSE.txt
154
124
  - README.md
155
125
  - Rakefile
156
126
  - config/mongoid.yml
157
127
  - gemfiles/mongoid-4.0.gemfile
158
128
  - lib/mongoid-audit.rb
159
- - lib/mongoid-audit/mongoid_observer.rb
129
+ - lib/mongoid-audit/history_tracker.rb
160
130
  - lib/mongoid-audit/rails_admin.rb
161
131
  - lib/mongoid-audit/railtie.rb
162
- - lib/mongoid-audit/sweeper.rb
163
132
  - lib/mongoid-audit/trackable.rb
164
- - lib/mongoid-audit/tracker.rb
165
133
  - lib/mongoid-audit/version.rb
166
134
  - mongoid-audit.gemspec
167
- - spec/integration/integration_spec.rb
168
- - spec/spec_helper.rb
169
- - spec/support/database_cleaner.rb
170
- - spec/support/mongoid.rb
171
- - spec/trackable_spec.rb
172
- - spec/tracker_spec.rb
173
135
  homepage: https://github.com/rs-pro/mongoid-audit
174
136
  licenses: []
175
137
  metadata: {}
@@ -179,24 +141,18 @@ require_paths:
179
141
  - lib
180
142
  required_ruby_version: !ruby/object:Gem::Requirement
181
143
  requirements:
182
- - - '>='
144
+ - - ">="
183
145
  - !ruby/object:Gem::Version
184
146
  version: '0'
185
147
  required_rubygems_version: !ruby/object:Gem::Requirement
186
148
  requirements:
187
- - - '>='
149
+ - - ">"
188
150
  - !ruby/object:Gem::Version
189
- version: '0'
151
+ version: 1.3.1
190
152
  requirements: []
191
153
  rubyforge_project:
192
- rubygems_version: 2.1.10
154
+ rubygems_version: 2.2.2
193
155
  signing_key:
194
156
  specification_version: 4
195
157
  summary: Easily track model change history and mantain audit log with mongoid
196
- test_files:
197
- - spec/integration/integration_spec.rb
198
- - spec/spec_helper.rb
199
- - spec/support/database_cleaner.rb
200
- - spec/support/mongoid.rb
201
- - spec/trackable_spec.rb
202
- - spec/tracker_spec.rb
158
+ test_files: []