mongoid-audit 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -13,6 +13,7 @@ ____________
13
13
 
14
14
  Add this line to your application's Gemfile:
15
15
 
16
+ # must be after rails_admin, if you use it
16
17
  gem 'mongoid-audit'
17
18
 
18
19
  And then execute:
@@ -26,6 +27,12 @@ Or install it yourself as:
26
27
  Usage
27
28
  -----
28
29
 
30
+ ** Rails Admin Integration **
31
+
32
+ # note that instead of a user class, you pass in history tracker table name as second argument
33
+ config.audit_with :mongoid_audit, 'HistoryTracker'
34
+
35
+
29
36
  **Create a history tracker**
30
37
 
31
38
  Create a new class to track histories. All histories are stored in this tracker. The name of the class can be anything you like. The only requirement is that it includes `Mongoid::Audit::Tracker`
data/lib/mongoid-audit.rb CHANGED
@@ -14,6 +14,10 @@ module Mongoid
14
14
  end
15
15
  end
16
16
 
17
+ if Object.const_defined?("RailsAdmin")
18
+ require "mongoid-audit/rails_admin"
19
+ end
20
+
17
21
  require 'mongoid-audit/tracker'
18
22
  require 'mongoid-audit/trackable'
19
23
  require 'mongoid-audit/sweeper'
@@ -0,0 +1,98 @@
1
+ module RailsAdmin
2
+ module Extensions
3
+ module MongoidAudit
4
+ class VersionProxy
5
+ def initialize(version)
6
+ @version = version
7
+ end
8
+
9
+ def message
10
+ @message = @version.action
11
+ @version.respond_to?(:modified) ? @message + " [" + @version.modified.to_a.map{ |c| c[0] + " = " + c[1].to_s }.join(", ") + "]" : @message
12
+ end
13
+
14
+ def created_at
15
+ @version.created_at
16
+ end
17
+
18
+ def table
19
+ @version.trackable.name
20
+ end
21
+
22
+ def username
23
+ @version.modifier.try(:email) || @version.modifier
24
+ end
25
+
26
+ def item
27
+ @version.trackable.id
28
+ end
29
+ end
30
+
31
+ class AuditingAdapter
32
+ COLUMN_MAPPING = {
33
+ :table => 'association_chain.name',
34
+ :username => 'modifier_id',
35
+ :item => 'association_chain.id',
36
+ :created_at => :created_at,
37
+ :message => :action
38
+ }
39
+
40
+ def initialize(controller, version_class = HistoryTracker)
41
+ @controller = controller
42
+ @version_class = version_class.to_s.constantize
43
+ end
44
+
45
+ def latest
46
+ @version_class.limit(100).map { |version| VersionProxy.new(version) }
47
+ end
48
+
49
+ def delete_object(object, model, user)
50
+ # do nothing
51
+ end
52
+
53
+ def update_object(object, model, user, changes)
54
+ # do nothing
55
+ end
56
+
57
+ def create_object(object, abstract_model, user)
58
+ # do nothing
59
+ end
60
+
61
+ def listing_for_model(model, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
62
+ history = @version_class.where('association_chain.name' => model.model_name)
63
+ history = history.any_of(action: /.*#{query}.*/, modifier_id: /.*#{query}.*/) if query
64
+ if sort
65
+ order = sort_reverse == "true" ? :desc : :asc
66
+ history = history.order_by(sort.to_sym => order)
67
+ else
68
+ history = history.order_by(created_at: :desc)
69
+ end
70
+
71
+ history = all ? history.entries : history.send(Kaminari.config.page_method_name, page.presence || "1").per(per_page)
72
+
73
+ history.map{|version| VersionProxy.new(version)}
74
+ end
75
+
76
+ def listing_for_object(model, object, query, sort, sort_reverse, all, page, per_page = (RailsAdmin::Config.default_items_per_page || 20))
77
+ history = @version_class.where('association_chain.name' => model.model_name, 'association_chain.id' => object.id)
78
+ history = history.any_of(message: /.*#{query}.*/, username: /.*#{query}.*/) if query
79
+ if sort
80
+ order = sort_reverse == "true" ? :desc : :asc
81
+ history = history.order_by(sort.to_sym => order)
82
+ else
83
+ history = history.order_by(created_at: :desc)
84
+ end
85
+ history = all ? history.entries : history.send(Kaminari.config.page_method_name, page.presence || "1").per(per_page)
86
+
87
+ history.map{|version| VersionProxy.new(version)}
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
93
+
94
+
95
+
96
+ RailsAdmin.add_extension(:mongoid_audit, RailsAdmin::Extensions::MongoidAudit, {
97
+ :auditing => true
98
+ })
@@ -7,7 +7,7 @@ module Mongoid::Audit
7
7
  scope_name = self.collection_name.to_s.singularize.to_sym
8
8
  default_options = {
9
9
  :on => :all,
10
- :except => [:created_at, :updated_at],
10
+ :except => [:created_at, :updated_at, :deleted_at, :c_at, :u_at],
11
11
  :modifier_field => :modifier,
12
12
  :version_field => :version,
13
13
  :scope => scope_name,
@@ -17,6 +17,8 @@ module Mongoid::Audit
17
17
 
18
18
  Mongoid::Audit.tracker_class_name = self.name.tableize.singularize.to_sym
19
19
 
20
+ index({'association_chain.name' => 1, 'association_chain.id' => 1})
21
+
20
22
  # install model observer and action controller filter
21
23
  Mongoid::Audit::Sweeper.send(:observe, Mongoid::Audit.tracker_class_name)
22
24
  if defined?(ActionController) and defined?(ActionController::Base)
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Audit
3
- VERSION = "0.0.1"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
@@ -18,10 +18,10 @@ Gem::Specification.new do |gem|
18
18
  gem.require_paths = ["lib"]
19
19
 
20
20
  gem.add_runtime_dependency('easy_diff', ">= 0")
21
- gem.add_runtime_dependency('mongoid', ">= 3.0.4")
21
+ gem.add_runtime_dependency('mongoid', ">= 3.0.0")
22
22
 
23
- gem.add_development_dependency('rspec', ["~> 2.12.0"])
24
- gem.add_development_dependency('bundler', ['>= 1.0.0'])
25
- gem.add_development_dependency('database_cleaner', [">= 0.8.0"])
26
- gem.add_development_dependency('activesupport', '~> 3.2.11')
23
+ gem.add_development_dependency('rspec', "~> 2.13.0")
24
+ gem.add_development_dependency('bundler', '>= 1.0.0')
25
+ gem.add_development_dependency('database_cleaner', "~> 0.9.1")
26
+ gem.add_development_dependency('activesupport', '~> 3.2.13')
27
27
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-audit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -34,7 +34,7 @@ dependencies:
34
34
  requirements:
35
35
  - - ! '>='
36
36
  - !ruby/object:Gem::Version
37
- version: 3.0.4
37
+ version: 3.0.0
38
38
  type: :runtime
39
39
  prerelease: false
40
40
  version_requirements: !ruby/object:Gem::Requirement
@@ -42,7 +42,7 @@ dependencies:
42
42
  requirements:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
- version: 3.0.4
45
+ version: 3.0.0
46
46
  - !ruby/object:Gem::Dependency
47
47
  name: rspec
48
48
  requirement: !ruby/object:Gem::Requirement
@@ -50,7 +50,7 @@ dependencies:
50
50
  requirements:
51
51
  - - ~>
52
52
  - !ruby/object:Gem::Version
53
- version: 2.12.0
53
+ version: 2.13.0
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
@@ -58,7 +58,7 @@ dependencies:
58
58
  requirements:
59
59
  - - ~>
60
60
  - !ruby/object:Gem::Version
61
- version: 2.12.0
61
+ version: 2.13.0
62
62
  - !ruby/object:Gem::Dependency
63
63
  name: bundler
64
64
  requirement: !ruby/object:Gem::Requirement
@@ -80,17 +80,17 @@ dependencies:
80
80
  requirement: !ruby/object:Gem::Requirement
81
81
  none: false
82
82
  requirements:
83
- - - ! '>='
83
+ - - ~>
84
84
  - !ruby/object:Gem::Version
85
- version: 0.8.0
85
+ version: 0.9.1
86
86
  type: :development
87
87
  prerelease: false
88
88
  version_requirements: !ruby/object:Gem::Requirement
89
89
  none: false
90
90
  requirements:
91
- - - ! '>='
91
+ - - ~>
92
92
  - !ruby/object:Gem::Version
93
- version: 0.8.0
93
+ version: 0.9.1
94
94
  - !ruby/object:Gem::Dependency
95
95
  name: activesupport
96
96
  requirement: !ruby/object:Gem::Requirement
@@ -98,7 +98,7 @@ dependencies:
98
98
  requirements:
99
99
  - - ~>
100
100
  - !ruby/object:Gem::Version
101
- version: 3.2.11
101
+ version: 3.2.13
102
102
  type: :development
103
103
  prerelease: false
104
104
  version_requirements: !ruby/object:Gem::Requirement
@@ -106,7 +106,7 @@ dependencies:
106
106
  requirements:
107
107
  - - ~>
108
108
  - !ruby/object:Gem::Version
109
- version: 3.2.11
109
+ version: 3.2.13
110
110
  description: Mongoid Audit
111
111
  email:
112
112
  - glebtv@gmail.com
@@ -123,6 +123,7 @@ files:
123
123
  - Rakefile
124
124
  - config/mongoid.yml
125
125
  - lib/mongoid-audit.rb
126
+ - lib/mongoid-audit/rails_admin.rb
126
127
  - lib/mongoid-audit/sweeper.rb
127
128
  - lib/mongoid-audit/trackable.rb
128
129
  - lib/mongoid-audit/tracker.rb