record_auditor 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 409a04c41fa9d61a9867b089b37becc1923a5f69
4
+ data.tar.gz: 7daaf373ac892322bec67f25ab8fda1043408c19
5
+ SHA512:
6
+ metadata.gz: 74e3b5a45b33d45e163f35143d0c5af765a2210e727db6e4e3800e9514faa656a15a6702ee52609205b6d5b2601fe023288a431f4acb9ae46d46e2c1279b7bb5
7
+ data.tar.gz: 0902b23fb71fb688b585a2cb1199728ec58c6d7ce55e0b9626b5cb3954a55a232f6d733e7668a5f1ca39ee7d1e0afd63bd87b83b41be5b02055031bead3b84e9
@@ -0,0 +1,20 @@
1
+ Copyright 2015 ocowchun
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,22 @@
1
+ #RecordAuditor
2
+ > audit ActiveRecord change
3
+
4
+ heavily inspired by [audited](https://github.com/collectiveidea/audited)
5
+
6
+ ##How To Start
7
+ ###1. Add below to `config/initializer/record_auditor.rb`
8
+
9
+ ```rb
10
+ RecordAuditor.handle_audit=Proc.new{|options|puts options} #change to whatever you want to audit active record
11
+ RecordAuditor.current_user_method=:current_rollbar_user #your custom current_user method
12
+ ```
13
+
14
+ ###2. add `audit` to the model you want to audit
15
+ ```rb
16
+ class User < ::ActiveRecord::Base
17
+ audit
18
+ ...
19
+ end
20
+ ```
21
+
22
+ MIT
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'RecordAuditor'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,19 @@
1
+ require 'record_auditor/auditor'
2
+ require 'record_auditor/audit'
3
+
4
+
5
+ module RecordAuditor
6
+ class << self
7
+ attr_accessor :ignored_attributes,:handle_audit,:current_user_method, :audit_class
8
+ # , :current_user_method, :audit_class
9
+ def store
10
+ Thread.current[:audited_store] ||= {}
11
+ end
12
+ end
13
+
14
+ @ignored_attributes = %w(lock_version created_at updated_at created_on updated_on password)
15
+ @current_user_method = :current_user
16
+ end
17
+
18
+ RecordAuditor.audit_class = RecordAuditor::Audit
19
+ require 'record_auditor/sweeper'
@@ -0,0 +1,22 @@
1
+ module RecordAuditor
2
+ class Audit
3
+ # include ActiveModel::Observing
4
+ extend ActiveModel::Callbacks
5
+ include ActiveModel::Conversion
6
+ # extend ActiveModel::Translation
7
+ include ActiveModel::Model
8
+ define_model_callbacks :create
9
+
10
+ attr_accessor :user,:auditable
11
+
12
+ def create(attrs={})
13
+ run_callbacks(:create) do
14
+ # handle something here
15
+ if RecordAuditor.handle_audit.present?
16
+ RecordAuditor.handle_audit.call(attrs.merge(user:user))
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,99 @@
1
+ module RecordAuditor
2
+ module Auditor
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ def get_id
10
+ 1
11
+ end
12
+
13
+ def audit(options = {})
14
+ include RecordAuditor::Auditor::LocalInstanceMethods
15
+ class_attribute :non_audited_columns, instance_writer: false
16
+ class_attribute :auditable_name, instance_writer: false
17
+
18
+
19
+ if options[:only]
20
+ except = column_names - Array(options[:only]).flatten.map(&:to_s)
21
+ else
22
+ except = default_ignored_attributes + RecordAuditor.ignored_attributes
23
+ except |= Array(options[:except]).collect(&:to_s) if options[:except]
24
+ end
25
+
26
+ self.non_audited_columns = except
27
+ self.auditable_name = options[:auditable_name]
28
+
29
+
30
+ after_create :audit_create
31
+ after_update :audit_update
32
+ after_destroy :audit_destroy
33
+
34
+ after_commit :handle_audit
35
+
36
+ end
37
+
38
+ def default_ignored_attributes
39
+ [primary_key, inheritance_column]
40
+ end
41
+
42
+ end
43
+
44
+
45
+ module LocalInstanceMethods
46
+ attr_accessor :action,:audited_changes_attrs
47
+
48
+ def audited_attributes
49
+ attributes.except(*non_audited_columns)
50
+ end
51
+
52
+ private
53
+
54
+ def audited_changes
55
+ changed_attributes.except(*non_audited_columns).inject({}) do |changes, (attr, old_value)|
56
+ changes[attr] = [old_value, self[attr]]
57
+ changes
58
+ end
59
+ end
60
+
61
+ def handle_audit()
62
+ if audited_changes_attrs.present?
63
+ a=RecordAuditor::Audit.new
64
+ attrs={action:action,audited_changes:audited_changes_attrs}
65
+ attrs[:auditable_type]=self.class.to_s
66
+ attrs[:auditable_id]=self.id
67
+
68
+ if auditable_name.present?&&self.respond_to?(*auditable_name)
69
+ attrs[:auditable_name]=self.send(*auditable_name)
70
+ end
71
+
72
+ a.create(attrs)
73
+ end
74
+ end
75
+
76
+ def audit_create
77
+ self.action='create'
78
+ self.audited_changes_attrs=audited_attributes
79
+ end
80
+
81
+ def audit_update
82
+ if (changes = audited_changes).empty?
83
+ self.action=nil
84
+ self.audited_changes_attrs=nil
85
+ else
86
+ self.action='update'
87
+ self.audited_changes_attrs=changes
88
+ end
89
+ end
90
+
91
+ def audit_destroy
92
+ self.action='destroy'
93
+ self.audited_changes_attrs='destroy'
94
+ end
95
+ end
96
+ end
97
+ end
98
+
99
+ ActiveRecord::Base.send :include, RecordAuditor::Auditor
@@ -0,0 +1,56 @@
1
+ require "rails/observers/activerecord/active_record"
2
+ require "rails/observers/action_controller/caching"
3
+
4
+ module RecordAuditor
5
+ class Sweeper < ActionController::Caching::Sweeper
6
+ observe RecordAuditor.audit_class
7
+
8
+ attr_accessor :controller
9
+
10
+ def around(controller)
11
+ begin
12
+ self.controller = controller
13
+ yield
14
+ ensure
15
+ self.controller = nil
16
+ end
17
+ end
18
+
19
+ def before_create(audit)
20
+ audit.user ||= current_user
21
+ # audit.remote_address = controller.try(:request).try(:remote_ip)
22
+ end
23
+
24
+ def current_user
25
+ controller.send(RecordAuditor.current_user_method) if controller.respond_to?(RecordAuditor.current_user_method, true)
26
+ end
27
+
28
+ def add_observer!(klass)
29
+ # super
30
+ define_callback(klass)
31
+ end
32
+
33
+ def define_callback(klass)
34
+ observer = self
35
+ callback_meth = :_notify_audited_sweeper
36
+ klass.send(:define_method, callback_meth) do
37
+ observer.update(:before_create, self)
38
+ end
39
+ klass.send(:before_create, callback_meth)
40
+ end
41
+
42
+ def controller
43
+ ::RecordAuditor.store[:current_controller]
44
+ end
45
+
46
+ def controller=(value)
47
+ ::RecordAuditor.store[:current_controller] = value
48
+ end
49
+ end
50
+ end
51
+
52
+ if defined?(ActionController) && defined?(ActionController::Base)
53
+ ActionController::Base.class_eval do
54
+ around_filter RecordAuditor::Sweeper.instance
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module RecordAuditor
2
+ VERSION = "0.0.5"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :audit_record do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: record_auditor
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
5
+ platform: ruby
6
+ authors:
7
+ - ocowchun
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-03-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 4.2.4
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '4.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 4.2.4
33
+ - !ruby/object:Gem::Dependency
34
+ name: rails-observers
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 0.1.2
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 0.1.2
47
+ - !ruby/object:Gem::Dependency
48
+ name: sqlite3
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '1.3'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec-rails
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: pry
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ description: audit ActiveRecord change
90
+ email:
91
+ - ocowchun@gmail.com
92
+ executables: []
93
+ extensions: []
94
+ extra_rdoc_files: []
95
+ files:
96
+ - MIT-LICENSE
97
+ - README.md
98
+ - Rakefile
99
+ - lib/record_auditor.rb
100
+ - lib/record_auditor/audit.rb
101
+ - lib/record_auditor/auditor.rb
102
+ - lib/record_auditor/sweeper.rb
103
+ - lib/record_auditor/version.rb
104
+ - lib/tasks/audit_record_tasks.rake
105
+ homepage: https://github.com/sudo-recruit/record_auditor
106
+ licenses:
107
+ - MIT
108
+ metadata: {}
109
+ post_install_message:
110
+ rdoc_options: []
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ required_rubygems_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ requirements: []
124
+ rubyforge_project:
125
+ rubygems_version: 2.4.8
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: record_auditor is a gem to audit ActiveRecord change
129
+ test_files: []