audited-deferrable 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Ben Hughes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # audited-deferrable
2
+
3
+ Extension to the audited gem to deferred writing of audits via Resque, but with modular architecture to support adding other providers such as sidekiq and delayed job (pull requests, please!)
4
+
5
+ NOTE: This currently only works with Resque v1, not Resque v2 which is a different API. I'd gladly accept pull requests supporting both.
6
+
7
+ ## Usage:
8
+
9
+ In Gemfile:
10
+
11
+ gem "audited-activerecord", "~> 3.0.0"
12
+ gem "resque", "~> 1.24"
13
+ gem "audited-deferrable", "~> 0.1.0"
14
+
15
+ In an initializer (config/initializers/audited.rb):
16
+
17
+ Audited.defer_with = :resque
18
+
19
+ After this, all of your audits will be written to the resque queue, and to completely them you'll need to run a resque worker:
20
+
21
+ $ QUEUE=audited rake environment resque:work
22
+
23
+ ## Configuration
24
+
25
+ By default, the resque queue is set to :audited, but can be changed:
26
+
27
+ Audited::Deferrable::Resque.queue = :custom_queue
28
+
29
+ ## Custom Handlers
30
+
31
+ If you want to write a handler for sidekiq or delayed job, please fork the project and implement as a subclass of Audited::Deferrable::Base, then send a pull request so all can use.
32
+
33
+ A completely custom handler can be implemented outside of the gem by subclassing Audited::Deferrable::Base, then setting that class to defer_with directly:
34
+
35
+ class Audited::Deferrable::CustomProvider < Audited::Deferrable::Base
36
+ ...
37
+ end
38
+
39
+ Audited.defer_with = Audited::Deferrable::CustomProvider
40
+
41
+
@@ -0,0 +1,52 @@
1
+ require "audited"
2
+
3
+ module Audited
4
+ module Deferrable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ Audited::Auditor::AuditedInstanceMethods.class_eval do
9
+ alias_method :write_audit_without_deferrable, :write_audit
10
+
11
+ def write_audit(attrs)
12
+ if Audited.deferrable
13
+ Audited.deferrable.new(self, Audited::Sweeper.instance.current_user, attrs).defer if auditing_enabled
14
+ else
15
+ write_audit_without_deferrable(attrs)
16
+ end
17
+ end
18
+ end
19
+ end
20
+
21
+ module ClassMethods
22
+ def defer_with
23
+ @defer_with
24
+ end
25
+
26
+ def defer_with=(value)
27
+ @defer_with = value
28
+ deferrable
29
+ @defer_with
30
+ end
31
+
32
+ def deferrable
33
+ if @defer_with.nil?
34
+ nil
35
+ elsif @defer_with.is_a?(Class)
36
+ @defer_with
37
+ else
38
+ begin
39
+ require "audited/deferrable/#{@defer_with}"
40
+ @defer_with = Audited::Deferrable.const_get(@defer_with.to_s.classify)
41
+ rescue LoadError
42
+ raise ArgumentError, "No handler exists for '#{@defer_with}'"
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ Audited.send :include, Audited::Deferrable
52
+
@@ -0,0 +1,39 @@
1
+ module Audited
2
+ module Deferrable
3
+ class Base
4
+
5
+ def initialize(object, user, attrs)
6
+ @object = object
7
+ @user = user
8
+ @attrs = attrs
9
+ end
10
+
11
+ def arguments
12
+ [
13
+ @object.class.name,
14
+ @object.id,
15
+ @user.try(:class).try(:name),
16
+ @user.try(:id),
17
+ @attrs
18
+ ]
19
+ end
20
+
21
+ def self.from_arguments(object_class_name, object_id, user_class_name, user_id, attrs)
22
+ object = object_class_name.constantize.find(object_id)
23
+ user = user_class_name.constantize.find(user_id) if user_class_name
24
+ new(object, user, attrs)
25
+ end
26
+
27
+ def defer
28
+ raise ArgumentError, "Implementation Required"
29
+ end
30
+
31
+ def work
32
+ Audited.audit_class.as_user(@user) do
33
+ @object.send(:write_audit_without_deferrable, @attrs)
34
+ end
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,30 @@
1
+ require "audited"
2
+ require "audited/deferrable/base"
3
+
4
+ module Audited
5
+ module Deferrable
6
+ class Resque < Base
7
+ @queue = :audited
8
+
9
+ def self.queue
10
+ @queue
11
+ end
12
+ def self.queue=(value)
13
+ @queue = value
14
+ end
15
+
16
+ def resque
17
+ @resque ||= Resque.new
18
+ end
19
+
20
+ def defer
21
+ ::Resque.enqueue(self.class, *arguments)
22
+ end
23
+
24
+ def self.perform(*args)
25
+ from_arguments(*args).work
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ module Audited
2
+ module Deferrable
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: audited-deferrable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ben Hughes
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-15 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: &70329449422300 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70329449422300
25
+ - !ruby/object:Gem::Dependency
26
+ name: audited
27
+ requirement: &70329449420780 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70329449420780
36
+ - !ruby/object:Gem::Dependency
37
+ name: rspec
38
+ requirement: &70329449418760 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '2.13'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *70329449418760
47
+ - !ruby/object:Gem::Dependency
48
+ name: appraisal
49
+ requirement: &70329449416480 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 0.5.1
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *70329449416480
58
+ - !ruby/object:Gem::Dependency
59
+ name: sqlite3-ruby
60
+ requirement: &70329449434560 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 1.3.1
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *70329449434560
69
+ description: Extension to the audited gem to deferred writing of audits via Resque,
70
+ Delayed Job, or Sidekiq
71
+ email: ben@railsgarden.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/audited/deferrable/base.rb
77
+ - lib/audited/deferrable/resque.rb
78
+ - lib/audited/deferrable/version.rb
79
+ - lib/audited/deferrable.rb
80
+ - LICENSE
81
+ - README.md
82
+ homepage: http://github.com/rubiety/audited-deferrable
83
+ licenses: []
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: 1.3.4
100
+ requirements: []
101
+ rubyforge_project: audited-deferrable
102
+ rubygems_version: 1.8.16
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: Extension to the audited gem to deferred writing of audits via Resque, Delayed
106
+ Job, or Sidekiq
107
+ test_files: []
108
+ has_rdoc: