audited-mongo_mapper 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
require 'audited/auditor'
|
3
|
+
require 'audited/adapters/mongo_mapper/audit'
|
4
|
+
|
5
|
+
module Audited::Auditor::ClassMethods
|
6
|
+
def default_ignored_attributes
|
7
|
+
['id', '_id']
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
::MongoMapper::Document.plugin Audited::Auditor
|
12
|
+
|
13
|
+
Audited.audit_class = Audited::Adapters::MongoMapper::Audit
|
14
|
+
|
15
|
+
require 'audited/sweeper'
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'audited/audit'
|
3
|
+
|
4
|
+
module Audited
|
5
|
+
module Adapters
|
6
|
+
module MongoMapper
|
7
|
+
# Audit saves the changes to ActiveRecord models. It has the following attributes:
|
8
|
+
#
|
9
|
+
# * <tt>auditable</tt>: the ActiveRecord model that was changed
|
10
|
+
# * <tt>user</tt>: the user that performed the change; a string or an ActiveRecord model
|
11
|
+
# * <tt>action</tt>: one of create, update, or delete
|
12
|
+
# * <tt>audited_changes</tt>: a serialized hash of all the changes
|
13
|
+
# * <tt>comment</tt>: a comment set with the audit
|
14
|
+
# * <tt>created_at</tt>: Time that the change was performed
|
15
|
+
#
|
16
|
+
class Audit
|
17
|
+
include ::MongoMapper::Document
|
18
|
+
include ActiveModel::Observing
|
19
|
+
|
20
|
+
key :auditable_id, ObjectId
|
21
|
+
key :auditable_type, String
|
22
|
+
key :associated_id, ObjectId
|
23
|
+
key :associated_type, String
|
24
|
+
key :user_id, ObjectId
|
25
|
+
key :user_type, String
|
26
|
+
key :username, String
|
27
|
+
key :action, String
|
28
|
+
key :audited_changes, Hash
|
29
|
+
key :version, Integer, :default => 0
|
30
|
+
key :comment, String
|
31
|
+
key :remote_address, String
|
32
|
+
key :created_at, Time
|
33
|
+
|
34
|
+
include Audited::Audit
|
35
|
+
|
36
|
+
before_create :set_created_at
|
37
|
+
|
38
|
+
def self.acending; sort(:version.asc); end
|
39
|
+
def self.ascending; sort(:version.asc); end
|
40
|
+
def self.descending; sort(:version.desc); end
|
41
|
+
def self.creates; where(:action => 'create'); end
|
42
|
+
def self.updates; where(:action => 'update'); end
|
43
|
+
def self.destroys; where(:action => 'destroy'); end
|
44
|
+
|
45
|
+
def self.up_until(date_or_time); where(:created_at.lte => date_or_time); end
|
46
|
+
def self.from_version(version); where(:version.gte => version); end
|
47
|
+
def self.to_version(version); where(:version.lte => version); end
|
48
|
+
|
49
|
+
class << self
|
50
|
+
|
51
|
+
# @private
|
52
|
+
def sanitize_for_time_with_zone(value)
|
53
|
+
case value
|
54
|
+
when Hash
|
55
|
+
value.inject({}){|h,(k,v)| h[k] = sanitize_for_time_with_zone(v); h }
|
56
|
+
when Array
|
57
|
+
value.map{|v| sanitize_for_time_with_zone(v) }
|
58
|
+
when ActiveSupport::TimeWithZone
|
59
|
+
value.utc
|
60
|
+
else
|
61
|
+
value
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def audited_changes=(value)
|
67
|
+
self[:audited_changes] = self.class.sanitize_for_time_with_zone(value)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Allows user to be set to either a string or an ActiveRecord object
|
71
|
+
# @private
|
72
|
+
def user_as_string=(user)
|
73
|
+
# reset both either way
|
74
|
+
self.user_as_model = self.username = nil
|
75
|
+
user.is_a?(::MongoMapper::Document) ?
|
76
|
+
self.user_as_model = user :
|
77
|
+
self.username = user
|
78
|
+
end
|
79
|
+
alias_method :user_as_model=, :user=
|
80
|
+
alias_method :user=, :user_as_string=
|
81
|
+
|
82
|
+
# @private
|
83
|
+
def user_as_string
|
84
|
+
self.user_as_model || self.username
|
85
|
+
end
|
86
|
+
alias_method :user_as_model, :user
|
87
|
+
alias_method :user, :user_as_string
|
88
|
+
|
89
|
+
# Return all audits older than the current one.
|
90
|
+
def ancestors
|
91
|
+
self.class.where(:auditable_id => auditable_id, :auditable_type => auditable_type, :version.lte => version)
|
92
|
+
end
|
93
|
+
|
94
|
+
def set_created_at
|
95
|
+
self[:created_at] = Time.now.utc if !persisted? && !created_at?
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: audited-mongo_mapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.0.0.rc1
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brandon Keepers
|
9
|
+
- Kenneth Kalmer
|
10
|
+
- Daniel Morrison
|
11
|
+
- Brian Ryckbost
|
12
|
+
- Steve Richert
|
13
|
+
- Ryan Glover
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
date: 2012-04-25 00:00:00.000000000 Z
|
18
|
+
dependencies:
|
19
|
+
- !ruby/object:Gem::Dependency
|
20
|
+
name: audited
|
21
|
+
requirement: !ruby/object:Gem::Requirement
|
22
|
+
none: false
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0.rc1
|
27
|
+
prerelease: 6
|
28
|
+
type: :runtime
|
29
|
+
prerelease: false
|
30
|
+
version_requirements: !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - '='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.0.0.rc1
|
36
|
+
prerelease: 6
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: mongo_mapper
|
39
|
+
requirement: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0.11'
|
45
|
+
type: :runtime
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ~>
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0.11'
|
53
|
+
description: Log all changes to your MongoMapper models
|
54
|
+
email: info@collectiveidea.com
|
55
|
+
executables: []
|
56
|
+
extensions: []
|
57
|
+
extra_rdoc_files: []
|
58
|
+
files:
|
59
|
+
- lib/audited-mongo_mapper.rb
|
60
|
+
- lib/audited/adapters/mongo_mapper.rb
|
61
|
+
- lib/audited/adapters/mongo_mapper/audit.rb
|
62
|
+
homepage: https://github.com/collectiveidea/audited
|
63
|
+
licenses: []
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options: []
|
66
|
+
require_paths:
|
67
|
+
- lib
|
68
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
75
|
+
none: false
|
76
|
+
requirements:
|
77
|
+
- - ! '>'
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: 1.3.1
|
80
|
+
requirements: []
|
81
|
+
rubyforge_project:
|
82
|
+
rubygems_version: 1.8.21
|
83
|
+
signing_key:
|
84
|
+
specification_version: 3
|
85
|
+
summary: Log all changes to your MongoMapper models
|
86
|
+
test_files: []
|