cia 0.5.10 → 0.6.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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/Readme.md +104 -0
  3. data/lib/cia.rb +5 -5
  4. data/lib/cia/version.rb +1 -1
  5. metadata +7 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c0103b7461c217ca96f7c037fee25c83448ff0c6
4
- data.tar.gz: f625ccbc4959663c19c2e0807a5ac24dc9e29720
3
+ metadata.gz: 721b3ddd4c0d65f03c32681de80e18d7dc2709c9
4
+ data.tar.gz: 634c7289d85d4f7a8768e97abaec2a3acade6175
5
5
  SHA512:
6
- metadata.gz: 6d120a22bf343b9e16a41f76fdc42142d7ca0151c6251fb0f78f465457216a9a206da0c2f50424e6240902a95505906c28ddc7d2c124d6c2578a823e947bc306
7
- data.tar.gz: 856f0d74b7aea9793c507dabe68d9c03c78292e7cb0237bc601be75ebd8afe7160dd5dd5ee30d27db183a88ff4edcf4ca78f21bb80b98a9774441c0995708634
6
+ metadata.gz: 3757ab4bf222f509a11d10a084985eb7beffc34681a5076f26ad9471220bdf1f4283ccb6b999f2ba9a834ae09441b81f91bd1d7a59aacfd56be3ef9a72b4ca5e
7
+ data.tar.gz: 95b73f3e943191df350c9765716332638aa0cdd2975ec31465014f2bd1ba3a6007bcd7b2625b2620a9fb721d8be5650d6a3ecc5a3812b1e463b4c8df08f26f95
@@ -0,0 +1,104 @@
1
+ Central Internal Auditing
2
+ ============================
3
+
4
+ Audit model actions like update/create/destroy/<custom> + attribute changes.
5
+
6
+ - normalized and queryable through table layout
7
+ - actors and subjects are polymorphic
8
+ - works on ActiveRecord 3.2, 4.0 and 4.1
9
+
10
+ Table layout:
11
+
12
+ Event (actor/ip/time/updated subject + message)
13
+ -> has many attribute changes (changed password from foo to bar on subject)
14
+
15
+
16
+ Install
17
+ =======
18
+
19
+ ```Bash
20
+ gem install cia
21
+ ```
22
+
23
+ `rails g migration add_cia` + paste [Migration](https://raw.github.com/grosser/cia/master/MIGRATION.rb)
24
+
25
+
26
+ Usage
27
+ =====
28
+
29
+ ```Ruby
30
+ class User < ActiveRecord::Base
31
+ include CIA::Auditable
32
+ audited_attributes :email, :crypted_password
33
+ end
34
+
35
+ class ApplicationController < ActionController::Base
36
+ around_action :scope_auditing
37
+
38
+ def scope_auditing(&block)
39
+ CIA.audit actor: current_user, ip_address: request.remote_ip, &block
40
+ end
41
+ end
42
+
43
+ # quick access
44
+ User.last.cia_events
45
+ changes = User.last.cia_attribute_changes
46
+ last_passwords = changes.where(attribute_name: "crypted_password").map(&:new_value)
47
+
48
+ # exceptions (raised by default)
49
+ CIA.exception_handler = -> (e) { raise e unless Rails.env.production? }
50
+
51
+ # conditional auditing
52
+ class User < ActiveRecord::Base
53
+ audited_attributes :email, if: :interesting?
54
+
55
+ def interesting?
56
+ ...
57
+ end
58
+ end
59
+
60
+ # adding an actor e.g. for user creation
61
+ CIA.current_actor = @user
62
+
63
+ # custom changes
64
+ class User < ActiveRecord::Base
65
+ def cia_changes
66
+ changes.merge("this" => ["always", "changes"])
67
+ end
68
+ end
69
+
70
+ # using after_commit, useful if the CIA::Event is stored in a different database then the audited class
71
+ class User < ActiveRecord::Base
72
+ include CIA::Auditable
73
+ audited_attributes :email, :crypted_password, callback: :after_commit
74
+ end
75
+
76
+ # passing arbitrary attributes into the .audit method
77
+ CIA.non_recordable_attributes = [:my_pretty_audit_property]
78
+ CIA.audit(actor: current_user, my_pretty_audit_property: "12345") do
79
+ ...
80
+ end
81
+
82
+ # storing complex objects in old/new and reducing it's size if it's to big (serialized via json)
83
+ value = CIA::AttributeChange.serialize_for_storage(["some", "complex"*1000, "object"]){|too_big| too_big.delete_at(1); too_big }
84
+ CIA::AttributeChange.create!(old_value: value)
85
+
86
+ # add something to current transaction or start a new audit
87
+ CIA.audit bar: :baz, foo: :bang do
88
+ CIA.amend_audit foo: :bar do
89
+ puts CIA.current_transaction
90
+ end
91
+ end
92
+ -> {foo: :bar, bar: :baz}
93
+ ```
94
+
95
+
96
+ # TODO
97
+ - reuse AR3+ previous_changes in a nice way
98
+
99
+ Author
100
+ ======
101
+ [Michael Grosser](http://grosser.it)<br/>
102
+ michael@grosser.it<br/>
103
+ License: MIT<br/>
104
+ [![Build Status](https://travis-ci.org/grosser/cia.png)](https://travis-ci.org/grosser/cia)
data/lib/cia.rb CHANGED
@@ -55,9 +55,9 @@ module CIA
55
55
  return if not message and changes.empty? and action.to_s == "update"
56
56
 
57
57
  transaction_attributes = current_transaction.dup
58
- transaction_attributes.reject! { |k, v| non_recordable_attributes.include?(k) } if non_recordable_attributes
58
+ transaction_attributes.reject! { |k, _v| non_recordable_attributes.include?(k) } if non_recordable_attributes
59
59
 
60
- event = CIA::Event.new(transaction_attributes.merge(
60
+ event = CIA::Event.new(transaction_attributes.reverse_merge(
61
61
  action: action.to_s,
62
62
  source: source,
63
63
  message: message
@@ -65,11 +65,11 @@ module CIA
65
65
  event.add_attribute_changes(changes)
66
66
  event.save!
67
67
  event
68
- rescue Object => e
68
+ rescue StandardError
69
69
  if exception_handler
70
- exception_handler.call e
70
+ exception_handler.call $!
71
71
  else
72
- raise e
72
+ raise
73
73
  end
74
74
  end
75
75
  end
@@ -1,3 +1,3 @@
1
1
  module CIA
2
- VERSION = '0.5.10'
2
+ VERSION = '0.6.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cia
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.10
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Grosser
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-11 00:00:00.000000000 Z
11
+ date: 2016-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -56,16 +56,16 @@ dependencies:
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '2'
61
+ version: '3.4'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '2'
68
+ version: '3.4'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: wwtd
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -114,6 +114,7 @@ executables: []
114
114
  extensions: []
115
115
  extra_rdoc_files: []
116
116
  files:
117
+ - Readme.md
117
118
  - lib/cia.rb
118
119
  - lib/cia/attribute_change.rb
119
120
  - lib/cia/auditable.rb