sequel-auditer 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 44286a93587c09bbef33c1977f2d721081054844118d3928f4b85505e075c5aa
4
- data.tar.gz: 189ea2f2dcaecc8e14126d8915fa9c574ecbfd402f9e726ec6e32f27f58e4db1
3
+ metadata.gz: 6860ac0c5865b177eac491a277880e87fe783ed4537be357de32a0d1f477f604
4
+ data.tar.gz: 2dd9b761c2210f6b9eb135f6556aadc570bfa3f1f02671a1affb0a6c9d186da9
5
5
  SHA512:
6
- metadata.gz: 194e4c5c7f5d6f1cf7d41f583c4a1048d938ed299beff7cf919e01597ad723adb699ecfd92feb89118d79e23712c0a6c8566264d436e5fc1a765e240a684e678
7
- data.tar.gz: aa0386f43e8f235931e73fadfac67525c67b9b49b525d3d23f6aff9f8090227129ad3d8074846f37eb2457c1101f700540a1a24d33c76b354b672347e2bf1253
6
+ metadata.gz: c5e5172bcb8f2f75192065c72559095308373c04cd92369e2c1881db19bce86fec84554d4dac9f250f81c3c87790e3a777f636f8be36f23266d3c4049c81a746
7
+ data.tar.gz: 50f98657d457799fae402bf92ecfd7beba7b1b109448d993b412dd0f7095886d7c7e345a10f381c1562e8e520cf09191941f370aa45dc9df5e34db0db1aa26f8
data/.gitignore CHANGED
@@ -10,3 +10,4 @@
10
10
  spec/sequel-audited-test.db
11
11
  .env.test
12
12
  rubocop
13
+ *.gem
data/README.md CHANGED
@@ -7,14 +7,6 @@ and when the change was made.
7
7
  This plugin provides model auditing (a.k.a: record versioning) for DB scenarios when DB triggers
8
8
  are not possible. (ie: on a web app on Heroku).
9
9
 
10
-
11
-
12
- ## Disclaimer
13
-
14
- This is still **work-in-progress**, and therefore **NOT production ready**, so **use with care**
15
- and test thoroughly before depending upon this gem for mission-critical stuff!
16
- You have been warned! No warranties and guarantees expressed or implied!
17
-
18
10
  <br>
19
11
 
20
12
  ----
@@ -1,5 +1,5 @@
1
1
  module Sequel
2
2
  module Auditer
3
- VERSION = "0.1.0"
3
+ VERSION = "0.2.0"
4
4
  end
5
5
  end
@@ -2,7 +2,7 @@ require_relative '../auditer'
2
2
 
3
3
  class AuditLog < Sequel::Model
4
4
  # handle versioning of audited records
5
- plugin :list, field: :version, scope: [:associated_type, :associated_id]
5
+ plugin :list, field: :version, scope: %i[associated_type associated_id]
6
6
  plugin :timestamps
7
7
  plugin :polymorphic
8
8
 
@@ -15,12 +15,12 @@ class AuditLog < Sequel::Model
15
15
  if u = audit_user
16
16
  self.modifier = u
17
17
  end
18
-
19
- # grab any additional info if any
20
- if i = audit_additional_info
18
+
19
+ # grab any additional info if any
20
+ if i = audit_additional_info
21
21
  self.additional_info = i
22
- end
23
-
22
+ end
23
+
24
24
  super
25
25
  end
26
26
 
@@ -32,30 +32,27 @@ class AuditLog < Sequel::Model
32
32
  # # NOTE! this allows overriding the default value on a per audited model
33
33
  def audit_user
34
34
  user = ::Sequel::Auditer::Railtie.user
35
- return user if !user.nil?
36
-
37
- begin
38
- m = Kernel.const_get(associated_type)
39
- m.send(m.auditer_current_user_method) || send(m.auditer_current_user_method)
40
- rescue
41
- nil
42
- end
35
+
36
+ m = Kernel.const_get(associated_type)
37
+ u = m.send(m.auditer_current_user_method) || send(m.auditer_current_user_method)
38
+ return u unless u.nil?
39
+ return user if u.nil? && !user.nil?
40
+
41
+ nil
42
+ rescue StandardError
43
+ nil
43
44
  end
44
-
45
+
45
46
  def audit_additional_info
46
- begin
47
- m = Kernel.const_get(associated_type)
48
- m.send(m.auditer_additional_info_method) || send(m. auditer_additional_info_method)
49
- rescue
50
- nil
51
- end
47
+ m = Kernel.const_get(associated_type)
48
+ m.send(m.auditer_additional_info_method) || send(m. auditer_additional_info_method)
49
+ rescue StandardError
50
+ nil
52
51
  end
53
-
54
52
  end
55
53
 
56
54
  module Sequel
57
55
  module Plugins
58
-
59
56
  # Given a Post model with these fields:
60
57
  # [:id, :category_id, :title, :body, :author_id, :created_at, :updated_at]
61
58
  #
@@ -86,7 +83,6 @@ module Sequel
86
83
  #
87
84
  #
88
85
  module Auditer
89
-
90
86
  # called when
91
87
  def self.configure(model, opts = {})
92
88
  model.instance_eval do
@@ -107,12 +103,7 @@ module Sequel
107
103
  only = opts.fetch(:only, [])
108
104
  except = opts.fetch(:except, [])
109
105
 
110
- unless only.empty?
111
- # we should only track the provided column
112
- included_columns = [only].flatten
113
- # subtract the 'only' columns from all columns to get excluded_columns
114
- excluded_columns = columns - included_columns
115
- else # except:
106
+ if only.empty? # except:
116
107
  # all columns minus any excepted columns and default ignored columns
117
108
  included_columns = [
118
109
  [columns - [except].flatten].flatten - @auditer_default_ignored_columns
@@ -121,6 +112,11 @@ module Sequel
121
112
  # except_columns = except.empty? ? [] : [except].flatten
122
113
  excluded_columns = [columns - included_columns].flatten.uniq
123
114
  # excluded_columns = [columns - [except_columns, included_columns].flatten].flatten.uniq
115
+ else
116
+ # we should only track the provided column
117
+ included_columns = [only].flatten
118
+ # subtract the 'only' columns from all columns to get excluded_columns
119
+ excluded_columns = columns - included_columns
124
120
  end
125
121
 
126
122
  @auditer_included_columns = included_columns
@@ -132,15 +128,10 @@ module Sequel
132
128
  class: audit_model_name,
133
129
  as: 'associated'
134
130
  )
135
-
136
131
  end
137
-
138
-
139
132
  end
140
133
 
141
- #
142
134
  module ClassMethods
143
-
144
135
  attr_accessor :auditer_default_ignored_columns, :auditer_current_user_method, :auditer_additional_info_method
145
136
  # The holder of ignored columns
146
137
  attr_reader :auditer_ignored_columns
@@ -149,15 +140,13 @@ module Sequel
149
140
 
150
141
  attr_accessor :auditer_reference_method
151
142
 
152
-
153
143
  Plugins.inherited_instance_variables(self,
154
144
  :@auditer_default_ignored_columns => nil,
155
145
  :@auditer_current_user_method => nil,
156
146
  :@auditer_additional_info_method => nil,
157
147
  :@auditer_included_columns => nil,
158
148
  :@auditer_ignored_columns => nil,
159
- :@auditer_reference_method => nil
160
- )
149
+ :@auditer_reference_method => nil)
161
150
 
162
151
  def non_audited_columns
163
152
  columns - auditer_columns
@@ -202,10 +191,8 @@ module Sequel
202
191
  audit_model.where(opts.merge(associated_type: name.to_s)).order(:version).all
203
192
  end
204
193
 
205
-
206
194
  private
207
195
 
208
-
209
196
  def audit_model
210
197
  const_get(audit_model_name)
211
198
  end
@@ -223,11 +210,7 @@ module Sequel
223
210
  end
224
211
 
225
212
  def set_user_method(opts)
226
- if opts[:user_method]
227
- @auditer_current_user_method = opts[:user_method]
228
- else
229
- @auditer_current_user_method = ::Sequel::Auditer.auditer_current_user_method
230
- end
213
+ @auditer_current_user_method = opts[:user_method] || ::Sequel::Auditer.auditer_current_user_method
231
214
  end
232
215
 
233
216
  def set_additional_info_method(opts)
@@ -243,13 +226,9 @@ module Sequel
243
226
  @auditer_reference_method = opts[:reference_method]
244
227
  end
245
228
  end
246
-
247
229
  end
248
230
 
249
-
250
- #
251
231
  module InstanceMethods
252
-
253
232
  # Returns who put the post into its current state.
254
233
  #
255
234
  # post.blame # => 'joeblogs'
@@ -262,7 +241,7 @@ module Sequel
262
241
  v = versions.last unless versions.empty?
263
242
  v ? v.modifier : 'not audited'
264
243
  end
265
- alias_method :last_audited_by, :blame
244
+ alias last_audited_by blame
266
245
 
267
246
  # Returns who put the post into its current state.
268
247
  #
@@ -276,19 +255,19 @@ module Sequel
276
255
  v = versions.last unless versions.empty?
277
256
  v ? v.created_at : 'not audited'
278
257
  end
279
- alias_method :last_audited_on, :last_audited_at
258
+ alias last_audited_on last_audited_at
280
259
 
281
260
  private
282
261
 
283
262
  # extract audited values only
284
263
  def auditer_values(event)
285
264
  vals = case event
286
- when Sequel::Auditer::CREATE
287
- self.values
288
- when Sequel::Auditer::UPDATE
289
- (column_changes.empty? ? previous_changes : column_changes)
290
- when Sequel::Auditer::DESTROY
291
- self.values
265
+ when Sequel::Auditer::CREATE
266
+ values
267
+ when Sequel::Auditer::UPDATE
268
+ (column_changes.empty? ? previous_changes : column_changes)
269
+ when Sequel::Auditer::DESTROY
270
+ values
292
271
  end
293
272
  vals.except(*model.auditer_default_ignored_columns)
294
273
  end
@@ -304,7 +283,7 @@ module Sequel
304
283
  end
305
284
 
306
285
  ### CALLBACKS ###
307
-
286
+
308
287
  def after_create
309
288
  super
310
289
  add_audited(Sequel::Auditer::CREATE)
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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kematzy
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: exe
13
13
  cert_chain: []
14
- date: 2018-09-30 00:00:00.000000000 Z
14
+ date: 2018-12-25 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: sequel