mongoid-audit 0.1.7 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 16dc7230bd554ec94129e00a7396c4809814ef4e
4
- data.tar.gz: 53ac0d33d6703176078ba5210e908763f1eb71eb
3
+ metadata.gz: 50bb3934bb4dae8960bb1513eee0c969db1c5984
4
+ data.tar.gz: 17bc52f155696941b1f9136eeafbb455e30232d2
5
5
  SHA512:
6
- metadata.gz: c05c11c7d6e1e196a71d1ce1381790f5d2d470a550bda13ea535f36e18532a3c802c91a9c9ea542c5d9f9ae1444d9da743950f7053fda6501730ff6ce0ea4e8d
7
- data.tar.gz: 4c15da80af5fb5c54758561eefb17ef4bf2b2c05ec7b86dabcdbe92027fa09a63585cd39e9c05b2a3c7ec5b355db2feaed798349a4952f43100374b5d04bf5ef
6
+ metadata.gz: 5926f65b34af90012152b9645f4f51a4a35fba2aac2c7369ce65a420880263e7773c3c05699cfbfce4a91788d5a5d09d25df3317bffb289e820076901d1fef82
7
+ data.tar.gz: 32ec2981cb51c405128b6858a63f4ee322b9baf7c11e25690606c4705a0955b7a0a157f8119f05ad57c04ed345ca8831b64ed08f6e3f94f1dc2619fe916d01c0
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ mongoid-audit
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p0
@@ -0,0 +1,172 @@
1
+ # encoding: utf-8
2
+ module Mongoid
3
+
4
+ # Observer classes respond to life cycle callbacks to implement trigger-like
5
+ # behavior outside the original class. This is a great way to reduce the
6
+ # clutter that normally comes when the model class is burdened with
7
+ # functionality that doesn't pertain to the core responsibility of the
8
+ # class. Mongoid's observers work similar to ActiveRecord's. Example:
9
+ #
10
+ # class CommentObserver < Mongoid::Observer
11
+ # def after_save(comment)
12
+ # Notifications.comment(
13
+ # "admin@do.com", "New comment was posted", comment
14
+ # ).deliver
15
+ # end
16
+ # end
17
+ #
18
+ # This Observer sends an email when a Comment#save is finished.
19
+ #
20
+ # class ContactObserver < Mongoid::Observer
21
+ # def after_create(contact)
22
+ # contact.logger.info('New contact added!')
23
+ # end
24
+ #
25
+ # def after_destroy(contact)
26
+ # contact.logger.warn("Contact with an id of #{contact.id} was destroyed!")
27
+ # end
28
+ # end
29
+ #
30
+ # This Observer uses logger to log when specific callbacks are triggered.
31
+ #
32
+ # == Observing a class that can't be inferred
33
+ #
34
+ # Observers will by default be mapped to the class with which they share a
35
+ # name. So CommentObserver will be tied to observing Comment,
36
+ # ProductManagerObserver to ProductManager, and so on. If you want to
37
+ # name your observer differently than the class you're interested in
38
+ # observing, you can use the Observer.observe class method which takes
39
+ # either the concrete class (Product) or a symbol for that class (:product):
40
+ #
41
+ # class AuditObserver < Mongoid::Observer
42
+ # observe :account
43
+ #
44
+ # def after_update(account)
45
+ # AuditTrail.new(account, "UPDATED")
46
+ # end
47
+ # end
48
+ #
49
+ # If the audit observer needs to watch more than one kind of object,
50
+ # this can be specified with multiple arguments:
51
+ #
52
+ # class AuditObserver < Mongoid::Observer
53
+ # observe :account, :balance
54
+ #
55
+ # def after_update(record)
56
+ # AuditTrail.new(record, "UPDATED")
57
+ # end
58
+ # end
59
+ #
60
+ # The AuditObserver will now act on both updates to Account and Balance
61
+ # by treating them both as records.
62
+ #
63
+ # == Available callback methods
64
+ #
65
+ # * after_initialize
66
+ # * before_validation
67
+ # * after_validation
68
+ # * before_create
69
+ # * around_create
70
+ # * after_create
71
+ # * before_update
72
+ # * around_update
73
+ # * after_update
74
+ # * before_upsert
75
+ # * around_upsert
76
+ # * after_upsert
77
+ # * before_save
78
+ # * around_save
79
+ # * after_save
80
+ # * before_destroy
81
+ # * around_destroy
82
+ # * after_destroy
83
+ #
84
+ # == Storing Observers in Rails
85
+ #
86
+ # If you're using Mongoid within Rails, observer classes are usually stored
87
+ # in +app/models+ with the naming convention of +app/models/audit_observer.rb+.
88
+ #
89
+ # == Configuration
90
+ #
91
+ # In order to activate an observer, list it in the +config.mongoid.observers+
92
+ # configuration setting in your +config/application.rb+ file.
93
+ #
94
+ # config.mongoid.observers = :comment_observer, :signup_observer
95
+ #
96
+ # Observers will not be invoked unless you define them in your
97
+ # application configuration.
98
+ #
99
+ # == Loading
100
+ #
101
+ # Observers register themselves with the model class that they observe,
102
+ # since it is the class that notifies them of events when they occur.
103
+ # As a side-effect, when an observer is loaded, its corresponding model
104
+ # class is loaded.
105
+ #
106
+ # Observers are loaded after the application initializers, so that
107
+ # observed models can make use of extensions. If by any chance you are
108
+ # using observed models in the initialization, you can
109
+ # still load their observers by calling +ModelObserver.instance+ before.
110
+ # Observers are singletons and that call instantiates and registers them.
111
+ class Observer < ActiveModel::Observer
112
+
113
+ private
114
+
115
+ # Adds the specified observer to the class.
116
+ #
117
+ # @example Add the observer.
118
+ # observer.add_observer!(Document)
119
+ #
120
+ # @param [ Class ] klass The child observer to add.
121
+ #
122
+ # @since 2.0.0.rc.8
123
+ def add_observer!(klass)
124
+ super
125
+ define_callbacks(klass)
126
+ end
127
+
128
+ # Defines all the callbacks for each observer of the model.
129
+ #
130
+ # @example Define all the callbacks.
131
+ # observer.define_callbacks(Document)
132
+ #
133
+ # @param [ Class ] klass The model to define them on.
134
+ #
135
+ # @since 2.0.0.rc.8
136
+ def define_callbacks(klass)
137
+ observer = self
138
+ observer_name = observer.class.name.underscore.gsub('/', '__')
139
+ Mongoid::Callbacks.observables.each do |callback|
140
+ next unless respond_to?(callback)
141
+ callback_meth = :"_notify_#{observer_name}_for_#{callback}"
142
+ unless klass.respond_to?(callback_meth)
143
+ klass.send(:define_method, callback_meth) do |&block|
144
+ if value = observer.update(callback, self, &block)
145
+ value
146
+ else
147
+ block.call if block
148
+ end
149
+ end
150
+ klass.send(callback, callback_meth)
151
+ end
152
+ end
153
+ self
154
+ end
155
+
156
+ # Are the observers disabled for the object?
157
+ #
158
+ # @api private
159
+ #
160
+ # @example If the observer disabled?
161
+ # Observer.disabled_for(band)
162
+ #
163
+ # @param [ Document ] object The model instance.
164
+ #
165
+ # @return [ true, false ] If the observer is disabled.
166
+ def disabled_for?(object)
167
+ klass = object.class
168
+ return false unless klass.respond_to?(:observers)
169
+ klass.observers.disabled_for?(self) || Mongoid.observers.disabled_for?(self)
170
+ end
171
+ end
172
+ end
@@ -1,5 +1,5 @@
1
1
  module Mongoid::Audit
2
- class Sweeper < Mongoid::Observer
2
+ class Sweeper < ActiveModel::Observer
3
3
  def controller
4
4
  Thread.current[:mongoid_history_sweeper_controller]
5
5
  end
@@ -8,6 +8,10 @@ module Mongoid::Audit
8
8
  Thread.current[:mongoid_history_sweeper_controller] = value
9
9
  end
10
10
 
11
+ def self.observed_classes
12
+ [ Mongoid::Audit.tracker_class ]
13
+ end
14
+
11
15
  # Hook to ActionController::Base#around_filter.
12
16
  # Runs before a controller action is run.
13
17
  # It should always return true so controller actions
@@ -5,6 +5,8 @@ module Mongoid::Audit
5
5
  included do
6
6
  include Mongoid::Document
7
7
  include Mongoid::Timestamps
8
+ include ActiveModel::Observing
9
+
8
10
  attr_writer :trackable
9
11
 
10
12
  field :association_chain, :type => Array, :default => []
@@ -18,15 +20,6 @@ module Mongoid::Audit
18
20
  Mongoid::Audit.tracker_class_name = self.name.tableize.singularize.to_sym
19
21
 
20
22
  index({'association_chain.name' => 1, 'association_chain.id' => 1})
21
-
22
- # install model observer and action controller filter
23
- Mongoid::Audit::Sweeper.send(:observe, Mongoid::Audit.tracker_class_name)
24
- if defined?(ActionController) and defined?(ActionController::Base)
25
- ActionController::Base.class_eval do
26
- before_filter { |controller| Mongoid::Audit::Sweeper.instance.before(controller) }
27
- after_filter { |controller| Mongoid::Audit::Sweeper.instance.after(controller) }
28
- end
29
- end
30
23
  end
31
24
 
32
25
  def undo!(modifier)
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Audit
3
- VERSION = "0.1.7"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
data/lib/mongoid-audit.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  require 'mongoid-audit/version'
2
2
  require 'easy_diff'
3
3
  require 'mongoid'
4
+ require 'rails-observers'
5
+ require 'rails/observers/active_model/active_model'
6
+
7
+
4
8
 
5
9
  module Mongoid
6
10
  module Audit
@@ -15,6 +19,30 @@ module Mongoid
15
19
  end
16
20
  end
17
21
 
22
+ unless Mongoid.const_defined?('Observer')
23
+ require "rails"
24
+ module Rails
25
+ module Mongoid
26
+ class Railtie < Rails::Railtie
27
+ initializer "instantiate observers" do
28
+ config.after_initialize do
29
+ ::Mongoid::Audit.tracker_class.add_observer ::Mongoid::Audit::Sweeper.instance
30
+
31
+ # install model observer and action controller filter
32
+ # Mongoid::Audit::Sweeper.send(:observe, Mongoid::Audit.tracker_class_name)
33
+ if defined?(ActionController) and defined?(ActionController::Base)
34
+ ActionController::Base.class_eval do
35
+ before_filter { |controller| ::Mongoid::Audit::Sweeper.instance.before(controller) }
36
+ after_filter { |controller| ::Mongoid::Audit::Sweeper.instance.after(controller) }
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+
18
46
  if Object.const_defined?("RailsAdmin")
19
47
  require "mongoid-audit/rails_admin"
20
48
  end
@@ -26,4 +54,3 @@ require 'mongoid-audit/sweeper'
26
54
  Mongoid::Audit.modifier_class_name = "User"
27
55
  Mongoid::Audit.trackable_class_options = {}
28
56
  Mongoid::Audit.current_user_method ||= :current_user
29
-
@@ -18,11 +18,12 @@ 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.0")
21
+ gem.add_runtime_dependency('mongoid', ">= 3.1.0")
22
+ gem.add_runtime_dependency('rails-observers', "~> 0.1.1")
22
23
 
23
24
  gem.add_development_dependency('rake')
24
25
  gem.add_development_dependency('rspec', "~> 2.13.0")
25
26
  gem.add_development_dependency('bundler', '>= 1.0.0')
26
27
  gem.add_development_dependency('database_cleaner', "~> 0.9.1")
27
- gem.add_development_dependency('activesupport', '~> 3.2.13')
28
+ gem.add_development_dependency('activesupport', '~> 4.0.0')
28
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid-audit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
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-04-25 00:00:00.000000000 Z
11
+ date: 2013-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: easy_diff
@@ -30,14 +30,28 @@ dependencies:
30
30
  requirements:
31
31
  - - '>='
32
32
  - !ruby/object:Gem::Version
33
- version: 3.0.0
33
+ version: 3.1.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - '>='
39
39
  - !ruby/object:Gem::Version
40
- version: 3.0.0
40
+ version: 3.1.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails-observers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 0.1.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.1.1
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -100,14 +114,14 @@ dependencies:
100
114
  requirements:
101
115
  - - ~>
102
116
  - !ruby/object:Gem::Version
103
- version: 3.2.13
117
+ version: 4.0.0
104
118
  type: :development
105
119
  prerelease: false
106
120
  version_requirements: !ruby/object:Gem::Requirement
107
121
  requirements:
108
122
  - - ~>
109
123
  - !ruby/object:Gem::Version
110
- version: 3.2.13
124
+ version: 4.0.0
111
125
  description: Mongoid Audit
112
126
  email:
113
127
  - glebtv@gmail.com
@@ -117,6 +131,8 @@ extra_rdoc_files: []
117
131
  files:
118
132
  - .gitignore
119
133
  - .rspec
134
+ - .ruby-gemset
135
+ - .ruby-version
120
136
  - .travis.yml
121
137
  - Gemfile
122
138
  - LICENSE.txt
@@ -124,6 +140,7 @@ files:
124
140
  - Rakefile
125
141
  - config/mongoid.yml
126
142
  - lib/mongoid-audit.rb
143
+ - lib/mongoid-audit/mongoid_observer.rb
127
144
  - lib/mongoid-audit/rails_admin.rb
128
145
  - lib/mongoid-audit/sweeper.rb
129
146
  - lib/mongoid-audit/trackable.rb