sequel-auditer 0.2.0 → 1.0.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
  SHA256:
3
- metadata.gz: 6860ac0c5865b177eac491a277880e87fe783ed4537be357de32a0d1f477f604
4
- data.tar.gz: 2dd9b761c2210f6b9eb135f6556aadc570bfa3f1f02671a1affb0a6c9d186da9
3
+ metadata.gz: 1f050023468273d8dc79bc50f14b0569240f1cd7902f42fefbdfdb6ec0b3aee1
4
+ data.tar.gz: f66d9e691b3cdc738d28686734113919c93de999c69b074d9e9e9b345cc95e73
5
5
  SHA512:
6
- metadata.gz: c5e5172bcb8f2f75192065c72559095308373c04cd92369e2c1881db19bce86fec84554d4dac9f250f81c3c87790e3a777f636f8be36f23266d3c4049c81a746
7
- data.tar.gz: 50f98657d457799fae402bf92ecfd7beba7b1b109448d993b412dd0f7095886d7c7e345a10f381c1562e8e520cf09191941f370aa45dc9df5e34db0db1aa26f8
6
+ metadata.gz: f2754f8c56a4599a387e5bc473ebe8fecff7a3766fcc7d4ad679a706ff5d80fc73464c2728d16e482c11483a2e98e5dec131890b126bf285d2c638d271809011
7
+ data.tar.gz: 11e3db29cb47239b2aa3d736870f3b77f885948cdeac76bf93c445e33442d2fa2f2ddcc80730db8487c0b2e87820d407aafbbf3f5910d1e67c3e697f89c6eefc
data/README.md CHANGED
@@ -75,6 +75,23 @@ bundle exec rake db:migrate
75
75
 
76
76
  This gem will try to get user from warden based authentications. When available, auditer will be fetched from warden, otherwise global function will be fired.
77
77
 
78
+ ## Resource Owner and Modifier
79
+
80
+ SequelAuditer allows you to store 2 types of user data.
81
+
82
+ ### Modifier
83
+
84
+ Modifier is who changed the data. This can be user itself, staff member or admins.
85
+
86
+ ### Resource Owner
87
+
88
+ Resource owner respresents modified data's owner. This is usually an user. So, you can browse audit logs based on resource owner id and see who is the modifier. This data came from target model. You need to specify user method in model like this:
89
+
90
+ ```ruby
91
+ plugin :auditer, additional_info: :additional_info, user_method: :global_user, owner_field: :user
92
+ ```
93
+
94
+ Now, `:owner_field` is a field in `Folder` model. `:global_user` is a global function/method that came from somewhere else in our app. Something like `current_user`.
78
95
 
79
96
  ### IMPORTANT SIDENOTE!
80
97
 
@@ -229,6 +246,8 @@ cat.update(name: 'Ruby Sequel')
229
246
  :changed => "{\"name\":[\"Sequel\",\"Ruby Sequel\"],\"updated_at\":\"<timestamp>\"}",
230
247
  :version => 2,
231
248
  :modifier_id => 88,
249
+ :modifier_type => "Staff",
250
+ :resource_owner_id => 70,
232
251
  :modifier_type => "User",
233
252
  :additional_info => "",
234
253
  :created_at => <timestamp>
@@ -254,6 +273,8 @@ cat.delete
254
273
  :changed => "{\"id\":1,\"name\":\"Ruby Sequel\",\"created_at\":\"<timestamp>\",\"updated_at\":\"<timestamp>\"}",
255
274
  :version => 3,
256
275
  :modifier_id => 88,
276
+ :modifier_type => "Staff",
277
+ :resource_owner_id => 70,
257
278
  :modifier_type => "User",
258
279
  :additional_info => "",
259
280
  :created_at => <timestamp>
@@ -295,6 +316,22 @@ So if you want to customize the modifier per model you can do that here.
295
316
 
296
317
  <br>
297
318
 
319
+ #### `Sequel::auditer.auditer_resource_owner_field`
320
+
321
+ Sets the name of the model name (association) that provides the resource owner object.
322
+ Default is: `nil`.
323
+
324
+ You can easily change the name of this model by calling:
325
+
326
+ ```ruby
327
+ Sequel::auditer.auditer_resource_owner_field = :user
328
+ ```
329
+
330
+ **Note!** the name of the function must be given as a symbol.
331
+ **Note!!** it will first try to hit the method on the model (i.e. Post) itself first. It won't hit global function.
332
+
333
+ <br>
334
+
298
335
  #### `Sequel::auditer.audited_additional_info_method`
299
336
 
300
337
  Sets the name of the global method that provides the additional info object (Hash).
@@ -438,7 +475,7 @@ Posts.audited_versions(:created_at < Date.today - 2)
438
475
 
439
476
 
440
477
 
441
- 2) Track all changes made by a user / modifier_group.
478
+ 1) Track all changes made by a user / modifier_group.
442
479
 
443
480
  ```ruby
444
481
  joe = User[88]
@@ -1,20 +1,17 @@
1
- require "sequel/auditer/railtie"
2
- require "sequel/auditer/version"
3
-
1
+ require 'sequel/auditer/railtie'
2
+ require 'sequel/auditer/version'
4
3
 
5
4
  module Sequel
6
-
7
- #
8
5
  module Auditer
9
-
10
- CREATE = 'create'
11
- UPDATE = 'update'
12
- DESTROY = 'destroy'
6
+ CREATE = 'create'.freeze
7
+ UPDATE = 'update'.freeze
8
+ DESTROY = 'destroy'.freeze
13
9
 
14
10
  # set the name of the global method that provides the current user. Default: :current_user
15
11
  @auditer_current_user_method = :current_user
16
- # set any additional info such as :ip, :user_agent, ...
12
+ # set any additional info such as :ip, :user_agent, ...
17
13
  @auditer_additional_info_method = :additional_info
14
+ @auditer_resource_owner_field = :owner_field
18
15
  # enable swapping of the Audit model
19
16
  @auditer_model_name = :AuditLog
20
17
  # toggle for enabling / disabling auditing
@@ -28,10 +25,12 @@ module Sequel
28
25
  ]
29
26
 
30
27
  class << self
31
- attr_accessor :auditer_current_user_method, :auditer_additional_info_method,
32
- :auditer_model_name, :auditer_enabled,
33
- :auditer_default_ignored_columns
28
+ attr_accessor :auditer_current_user_method,
29
+ :auditer_additional_info_method,
30
+ :auditer_model_name,
31
+ :auditer_enabled,
32
+ :auditer_default_ignored_columns,
33
+ :auditer_resource_owner_field
34
34
  end
35
-
36
35
  end
37
36
  end
@@ -1,5 +1,5 @@
1
1
  module Sequel
2
2
  module Auditer
3
- VERSION = "0.2.0"
3
+ VERSION = "1.0.0"
4
4
  end
5
5
  end
@@ -7,8 +7,9 @@ class AuditLog < Sequel::Model
7
7
  plugin :polymorphic
8
8
 
9
9
  # TODO: see if we should add these
10
- many_to_one :associated, polymorphic: true
11
- many_to_one :modifier, polymorphic: true
10
+ many_to_one :associated, polymorphic: true
11
+ many_to_one :modifier, polymorphic: true
12
+ many_to_one :resource_owner, polymorphic: true
12
13
 
13
14
  def before_validation
14
15
  # grab the current user
@@ -19,7 +20,12 @@ class AuditLog < Sequel::Model
19
20
  # grab any additional info if any
20
21
  if i = audit_additional_info
21
22
  self.additional_info = i
22
- end
23
+ end
24
+
25
+ # grab resource owner
26
+ # if o = audit_owner
27
+ # self.resource_owner = o
28
+ # end
23
29
 
24
30
  super
25
31
  end
@@ -45,10 +51,16 @@ class AuditLog < Sequel::Model
45
51
 
46
52
  def audit_additional_info
47
53
  m = Kernel.const_get(associated_type)
48
- m.send(m.auditer_additional_info_method) || send(m. auditer_additional_info_method)
54
+ m.send(m.auditer_additional_info_method) || send(m.auditer_additional_info_method)
49
55
  rescue StandardError
50
56
  nil
51
57
  end
58
+
59
+ def audit_owner
60
+ m = Kernel.const_get(associated_type)
61
+ o = m.send(m.auditer_resource_owner_field) || send(m.auditer_resource_owner_field)
62
+ # abort o.inspect
63
+ end
52
64
  end
53
65
 
54
66
  module Sequel
@@ -97,6 +109,7 @@ module Sequel
97
109
  # specifically for the audited model on a per model basis
98
110
  set_user_method(opts)
99
111
  set_additional_info_method(opts)
112
+ set_owner_method(opts)
100
113
 
101
114
  set_reference_method(opts)
102
115
 
@@ -132,7 +145,7 @@ module Sequel
132
145
  end
133
146
 
134
147
  module ClassMethods
135
- attr_accessor :auditer_default_ignored_columns, :auditer_current_user_method, :auditer_additional_info_method
148
+ attr_accessor :auditer_default_ignored_columns, :auditer_current_user_method, :auditer_additional_info_method, :auditer_resource_owner_field
136
149
  # The holder of ignored columns
137
150
  attr_reader :auditer_ignored_columns
138
151
  # The holder of columns that should be audited
@@ -144,6 +157,7 @@ module Sequel
144
157
  :@auditer_default_ignored_columns => nil,
145
158
  :@auditer_current_user_method => nil,
146
159
  :@auditer_additional_info_method => nil,
160
+ :@auditer_resource_owner_field => nil,
147
161
  :@auditer_included_columns => nil,
148
162
  :@auditer_ignored_columns => nil,
149
163
  :@auditer_reference_method => nil)
@@ -197,6 +211,10 @@ module Sequel
197
211
  const_get(audit_model_name)
198
212
  end
199
213
 
214
+ def resource_owner
215
+ const_get(audit_model_name)
216
+ end
217
+
200
218
  def audit_model_name
201
219
  ::Sequel::Auditer.auditer_model_name
202
220
  end
@@ -212,6 +230,10 @@ module Sequel
212
230
  def set_user_method(opts)
213
231
  @auditer_current_user_method = opts[:user_method] || ::Sequel::Auditer.auditer_current_user_method
214
232
  end
233
+
234
+ def set_owner_method(opts)
235
+ @auditer_resource_owner_field = opts[:owner_field] || ::Sequel::Auditer.auditer_resource_owner_field
236
+ end
215
237
 
216
238
  def set_additional_info_method(opts)
217
239
  if opts[:additional_info]
@@ -274,10 +296,18 @@ module Sequel
274
296
 
275
297
  def add_audited(event)
276
298
  changed = auditer_values(event)
299
+
300
+ begin
301
+ res_owner = self.send(*model.auditer_resource_owner_field)
302
+ rescue
303
+ res_owner = nil
304
+ end
305
+
277
306
  unless changed.blank?
278
307
  add_version(
279
308
  event: event,
280
- changed: changed
309
+ changed: changed,
310
+ resource_owner: res_owner
281
311
  )
282
312
  end
283
313
  end
@@ -4,18 +4,20 @@ Sequel.migration do
4
4
  change do
5
5
  create_table(:audit_logs) do
6
6
  primary_key :id
7
- String :associated_type
8
- Integer :associated_id
9
- String :event
10
- String :changed, text: true
11
- Integer :version
12
- Integer :modifier_id
13
- String :modifier_type
14
- String :additional_info, text: true
15
- DateTime :created_at
7
+ String :associated_type
8
+ Integer :associated_id
9
+ String :event
10
+ String :changed, text: true
11
+ Integer :version
12
+ Integer :modifier_id
13
+ Integer :resource_owner_id
14
+ String :modifier_type
15
+ String :resource_owner_type
16
+ String :additional_info, text: true
17
+ DateTime :created_at
16
18
 
17
- index [:associated_type, :associated_id]
18
- index [:modifier_type, :modifier_id]
19
+ index %i[associated_type associated_id]
20
+ index %i[modifier_type modifier_id]
19
21
  end
20
22
  end
21
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sequel-auditer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kematzy