notifiably_audited 0.0.2 → 0.0.3

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
- MjE4ZTc3NjA5YWUxYzdlM2YwODc3N2UwMTM0NDk1NGQwNzFjNzk0OQ==
4
+ Nzc2YzdiYWVkNWZjZWQ2NjUyMTk5ZGNkMzFmNzg0YmM0NTVjMDZiYQ==
5
5
  data.tar.gz: !binary |-
6
- NGVlN2YzNjMxMWI1MGMxZWQwYzgzMmQzOTI5MGJlNzk0NmIxZGY5MA==
6
+ ODE1NzhlN2IxMjc5YTBiOWU0Yjk0YjhiYTEzYjkwOGZlNTNkODUwMg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- YTVlM2QwMzliMDViNDQxNmM2NDIxOTQ1NDYxMzFhMzQyYWNjYTQ2OWU3NGE0
10
- YzhkZmYwYmY3MTZmYWM0MTFmNDVkN2M3N2ZlNzIyNDE4MmUxZmYyYTY3MmUx
11
- OTIwMjUxZjY4MGUwYjgzNzc2MDQzNzAxNTUxOTcxMDY3M2UyZDY=
9
+ ZTRiMTA4MTQ0NzBjYWFjNTY0NWQyYzRmOTQ4NDU1NDNjNGU5MTBmOGZlYmY2
10
+ YTZhNTU4NzVmYWVjNjg0MTllOTQ1YWFkYzliYzBmODlmYjdiOWQ4NDllYjEz
11
+ NmJlZjc1MzY2Yzg5ZGUxNDJiMzhhN2U4ZTc2YzhmNDcyNjE3MTY=
12
12
  data.tar.gz: !binary |-
13
- MmYxOTczOWU2ZGRkM2IxYzFlNWIzMjhhZTRiZTIyNzJlOWY0MDA4NDU0NmVl
14
- NzQ4NWU2NWQxYjcxNWEyYThlNWRlYjc5ZTFhMGU3ZjVmY2UyNDg1MmVmY2Qx
15
- OTE2ZDI2YjkxNzBjMjcxOGQ4MzNlZjNjZWY1ZWNmNDE2YWNjOGM=
13
+ YTZjNzNmMWU0MWM2YzQyNzBjZDNlOWIxY2I0OGJmNjI3ZDA0MDZiYjVmZWIw
14
+ NmUwNzJkYzY2M2YzYmI3NGNmOTRlMDRlNzA4Yzc1Yjk1M2NlZDhlODU5Yzhk
15
+ ZjAzNTYxZTZlZjQxNDI1NDA4ZDcwOTU5YzE5ZWIxNWVmZDhlMDU=
data/audited.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |gem|
4
4
  gem.name = 'notifiably_audited'
5
- gem.version = '0.0.2'
5
+ gem.version = '0.0.3'
6
6
 
7
7
  gem.authors = ['Brandon Keepers', 'Kenneth Kalmer', 'Daniel Morrison', 'Brian Ryckbost', 'Steve Richert', 'Ryan Glover']
8
8
  gem.email = 'info@collectiveidea.com'
@@ -18,6 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.add_development_dependency 'rails', '~> 3.0'
19
19
  gem.add_development_dependency 'rspec-rails', '~> 2.0'
20
20
  gem.add_development_dependency 'sqlite3', '~> 1.0'
21
+ gem.add_development_dependency 'private_pub'
21
22
 
22
23
  gem.files = `git ls-files`.split($\).reject{|f| f =~ /(lib\/audited\-|adapters|generators)/ }
23
24
  gem.test_files = gem.files.grep(/^spec\//)
data/lib/audited/audit.rb CHANGED
@@ -12,11 +12,14 @@ module Audited
12
12
  belongs_to :associated, :polymorphic => true
13
13
 
14
14
  before_create :set_version_number, :set_audit_user
15
+
16
+ after_save :notify
15
17
 
16
18
  cattr_accessor :audited_class_names
17
19
  self.audited_class_names = Set.new
18
20
 
19
- attr_accessible :action, :audited_changes, :comment, :associated
21
+ attr_accessible :action, :audited_changes, :comment,
22
+ :associated, :receiver_id, :checked, :meta,:title
20
23
  end
21
24
 
22
25
  # Returns the list of classes that are being audited
@@ -96,7 +99,17 @@ module Audited
96
99
 
97
100
  def set_audit_user
98
101
  self.user = Thread.current[:audited_user] if Thread.current[:audited_user]
102
+ p self.user
99
103
  nil # prevent stopping callback chains
100
104
  end
105
+ #====== beck added for notifiable ====
106
+ def notify
107
+ PrivatePub.publish_to("/notifiably_audited/" + self.receiver_id.to_s,
108
+ title: self.title,
109
+ comment: self.comment,
110
+ id: self.id)
111
+ end
112
+ #========================================
113
+
101
114
  end
102
115
  end
@@ -43,13 +43,21 @@ module Audited
43
43
  # attr_accessible :name
44
44
  # end
45
45
  #
46
- def audited(options = {})
46
+ def notifiably_audited(options = {})
47
47
  # don't allow multiple calls
48
48
  return if self.included_modules.include?(Audited::Auditor::AuditedInstanceMethods)
49
49
 
50
50
  class_attribute :non_audited_columns, :instance_writer => false
51
51
  class_attribute :auditing_enabled, :instance_writer => false
52
52
  class_attribute :audit_associated_with, :instance_writer => false
53
+
54
+ #====== beck added for notifiable ====
55
+ class_attribute :audit_alert_to, :instance_writer => false
56
+ class_attribute :audit_alert_for, :instance_writer => false
57
+ class_attribute :audit_title, :instance_writer => false
58
+ class_attribute :audit_create_comment, :instance_writer => false
59
+ class_attribute :audit_update_comment, :instance_writer => false
60
+ #=====================================
53
61
 
54
62
  if options[:only]
55
63
  except = self.column_names - options[:only].flatten.map(&:to_s)
@@ -59,6 +67,14 @@ module Audited
59
67
  end
60
68
  self.non_audited_columns = except
61
69
  self.audit_associated_with = options[:associated_with]
70
+
71
+ #====== beck added for notifiable ====
72
+ self.audit_alert_to = options[:alert_to] || :assigned_to
73
+ self.audit_alert_for = options[:alert_for] || nil
74
+ self.audit_title = options[:title] || :name
75
+ self.audit_create_comment = options[:create_comment] || "New <<here>> has been created"
76
+ self.audit_update_comment = options[:update_comment] || "Values of <<here>> has been updated"
77
+ #=====================================
62
78
 
63
79
  if options[:comment_required]
64
80
  validates_presence_of :audit_comment, :if => :auditing_enabled
@@ -88,12 +104,90 @@ module Audited
88
104
  include Audited::Auditor::AuditedInstanceMethods
89
105
 
90
106
  self.auditing_enabled = true
107
+
91
108
  end
92
109
 
93
110
  def has_associated_audits
94
111
  has_many :associated_audits, :as => :associated, :class_name => Audited.audit_class.name
95
112
  end
96
113
  end
114
+
115
+ #======== Audit Instance Methods - Means the methods on the target object =====================
116
+
117
+ #====== beck modified for notifiable ====
118
+ # @non_monitor is 1 initially, if somewhere an audit is created this is set to 0, so no future
119
+ # audit will be created.
120
+
121
+ # if polymorphic, then
122
+ # [:polymorphic,"title",
123
+ # col of self to be displayed as comment,[:commentable_type,:commentable_id]]
124
+ #----------------------------------------------
125
+ # opts format for notifiably_audited method/gem
126
+ #----------------------------------------------
127
+ # notifiably_audited alert_for: [[[:assigned_to],
128
+ # "Re-assigned",
129
+ # "This product has been reassigned",
130
+ # [:user,:email]],
131
+ # [[:color,:score],
132
+ # "Color/Score Changed",
133
+ # "Color/Score value is changed"],
134
+ # [[:product_status_id],
135
+ # "Status Changed",
136
+ # "Status of this product is changed",
137
+ # [:product_status,:name]]],
138
+ # associated_with: :product_status,
139
+ # title: :name,
140
+ # create_comment: "New <<here>> has been created",
141
+ # update_comment: "Custom: Values of <<here>> has been updated"
142
+ #
143
+ # &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
144
+ #
145
+ # notifiably_audited alert_for: [[:polymorphic,
146
+ # nil,
147
+ # :content,
148
+ # [:commentable_type,:commentable_id]]]
149
+ #
150
+ # -----------
151
+ # alert_to :
152
+ # -----------
153
+ # The column of the target_object that has the user_id to send
154
+ # the notification to.(receiver_id of the Audit record)
155
+ # -----------
156
+ # alert_for :
157
+ # -----------
158
+ # Takes array of arrays as an argument. Every array in the main array corresponds to
159
+ # one or group of columns update and how to notify.
160
+ # Following is the index wise explanation of the arrays.
161
+ # 1. Column name or Column names
162
+ # 2. Title of the notification(title column of the Audit record)
163
+ # 3. Description of the notification(audit_comment column of the Audit record)
164
+ # 4. If the column name is a foreign key of an belongs_to association, then the model_name of the
165
+ # associated model and the column of the model to be displayed in title is specified
166
+ # -----------
167
+ # title :
168
+ # -----------
169
+ # Takes 1 column name or a method name of the target object as an argument, in order to prompt the
170
+ # title of the target object wherever needed in the notification
171
+ # ---------------
172
+ # create_comment :
173
+ # ---------------
174
+ # Default audit_comment for create action
175
+ # ---------------
176
+ # update_comment :
177
+ # ---------------
178
+ # Default audit_comment for update action
179
+ #
180
+ # ***************
181
+ # :polymorphic
182
+ # ***************
183
+ # If this is passed as the first argument of the alert_for option, then it means, the current_model
184
+ # is polymorphic and the target_object is a different model to which the current model is
185
+ # polymorphic to.
186
+ #
187
+ # In the above case the 4th argument of the alert_for option should be the type and id of the
188
+ # polymorphic model.(Ex: [:commentable_type,commentable_id])
189
+ #----------------------------------------------
190
+ #=====================================
97
191
 
98
192
  module AuditedInstanceMethods
99
193
  # Temporarily turns off auditing while saving.
@@ -192,23 +286,108 @@ module Audited
192
286
  end
193
287
  audits.to_version(version)
194
288
  end
195
-
289
+ #====== beck modified for notifiable ====
290
+
196
291
  def audit_create
197
- write_audit(:action => 'create', :audited_changes => audited_attributes,
198
- :comment => audit_comment)
292
+ set_audit_values("create")# also sets @non_monitor as 1
293
+ if !audit_alert_for.nil?
294
+ audit_alert_for.each do |f|
295
+ if f[0] == :polymorphic
296
+ polymorphic_audit(f)# also sets @non_monitor as 0
297
+ end
298
+ end
299
+ end
300
+
301
+ if @non_monitor == 1
302
+ write_audit(@audit_values)
303
+ end
199
304
  end
200
-
305
+
201
306
  def audit_update
202
307
  unless (changes = audited_changes).empty? && audit_comment.blank?
203
- write_audit(:action => 'update', :audited_changes => changes,
204
- :comment => audit_comment)
308
+ set_audit_values("update")# also sets @non_monitor as 1
309
+
310
+ if !audit_alert_for.nil?
311
+ audit_alert_for.each do |f|
312
+ if f[0] == :polymorphic
313
+ polymorphic_audit(f)# also sets @non_monitor as 0
314
+ else
315
+ changed_eq_opts(f)# also sets @non_monitor as 0
316
+ end
317
+ end
318
+ end
319
+
320
+ if @non_monitor == 1
321
+ write_audit(@audit_values)
322
+ end
323
+
205
324
  end
206
325
  end
207
-
326
+
208
327
  def audit_destroy
209
328
  write_audit(:action => 'destroy', :audited_changes => audited_attributes,
210
- :comment => audit_comment)
329
+ :comment => audit_comment, :receiver_id => self.send(audit_alert_to), :checked => false)
330
+ end
331
+ #=== Following methods helps audit_create and update ======
332
+ # set_audit_values
333
+ # polymorphic_audit
334
+ # changed_eq_opts
335
+ #==========================================================
336
+
337
+ def set_audit_values(type)
338
+ @non_monitor = 1
339
+ # based on type the audit attributes are set
340
+ if (type == "create")
341
+ v_audited_changes = audited_attributes
342
+ elsif (type == "update")
343
+ v_audited_changes = changes
344
+ end
345
+ # the <<here>> part of the comment is replaced with class name
346
+ v_comment = send("audit_" + type +"_comment").gsub(/<<here>>/,self.class.name)
347
+ # fields of the audit record set initially as hash, can be over ridden
348
+ @audit_values = {action: type,
349
+ audited_changes: v_audited_changes,
350
+ comment: v_comment,
351
+ title: (self.send(audit_title) rescue self.class.name),
352
+ checked: false,
353
+ receiver_id: (self.send(audit_alert_to) rescue nil)}
354
+ end
355
+
356
+ def polymorphic_audit(opts)
357
+ # polymorphic - so the actual object is set, not the polymorphed object(product not comment)
358
+ target_object = self.send(opts[3][0]).constantize.find(self.send(opts[3][1]))
359
+ # overriding the audit values based on polymorphic opts
360
+ @audit_values[:title] = (opts[1] || @audit_values[:title]).to_s + " - #{target_object.class.name}[#{target_object.send(audit_title)}]"
361
+ @audit_values[:comment] = self.send(opts[2])[0..20].to_s + "..."
362
+ @audit_values[:receiver_id] = target_object.send(audit_alert_to)
363
+ # actual recording of audit
364
+ write_audit(@audit_values)
365
+ # setting to 0, so dont record anymore audits
366
+ @non_monitor = 0
367
+ end
368
+
369
+ def changed_eq_opts(opts)
370
+ # the cols that are going to be modified as identified by audited gem
371
+ changed = changes.keys.map &:to_sym
372
+ # the cols to be listened to, as passed in the opts
373
+ opts_changes = opts[0]
374
+ # if the sub array(to be listened) is included in the main array(audited identified cols)
375
+ if ((opts_changes - changed).size == 0)
376
+ # overriding audit values
377
+ @audit_values[:title] = opts[1]
378
+ @audit_values[:comment] = opts[2]
379
+ # if 3rd argument is present in the opts, then overriding title
380
+ if opts[3].present?
381
+ append = opts[3][0].to_s.camelize.constantize.find(self.send(opts[0][0])).send(opts[3][1])
382
+ @audit_values[:title] = @audit_values[:title] + "[#{append}]"
383
+ end
384
+ # actual recording of audit
385
+ write_audit(@audit_values)
386
+ # setting to 0, so dont record anymore audits
387
+ @non_monitor = 0
388
+ end
211
389
  end
390
+ #========================================
212
391
 
213
392
  def write_audit(attrs)
214
393
  attrs[:associated] = self.send(audit_associated_with) unless audit_associated_with.nil?
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: notifiably_audited
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandon Keepers
@@ -113,6 +113,20 @@ dependencies:
113
113
  - - ~>
114
114
  - !ruby/object:Gem::Version
115
115
  version: '1.0'
116
+ - !ruby/object:Gem::Dependency
117
+ name: private_pub
118
+ requirement: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ! '>='
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ type: :development
124
+ prerelease: false
125
+ version_requirements: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
116
130
  description: Log all changes to your models
117
131
  email: info@collectiveidea.com
118
132
  executables: []