activeaudit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2013 Massimo Maino
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.
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # ActiveAudit
2
+
3
+ Audit support for Active Record
4
+
5
+ [![Build Status](https://travis-ci.org/maintux/activeaudit.png)](https://travis-ci.org/maintux/activeaudit)
6
+
7
+ ## Install
8
+
9
+ Include `ActiveAudit` in your Gemfile
10
+ ```ruby
11
+ gem 'activeaudit', :git=>'https://github.com/maintux/activeaudit.git'
12
+ ```
13
+ and update your bundle
14
+ ```
15
+ bundle install
16
+ ```
17
+
18
+ Then run the task to generate migration
19
+ ```
20
+ rake active_audit_rails_engine:install:migrations
21
+ ```
22
+ and execute migration
23
+ ```
24
+ rake db:migrate
25
+ ```
26
+
27
+ ## Usage
28
+
29
+ In your model you have to include the mixin
30
+ ```ruby
31
+ include ActiveAudit::Logger
32
+ ```
33
+ This mixin allow you to use an helper called `loggable_event` that define what you want to log.
34
+
35
+ The basic syntax is:
36
+ ```ruby
37
+ loggable_event create: true
38
+ loggable_event destroy: true
39
+ loggable_event update: true
40
+ ```
41
+ With this, ActiveAudit will log events using the standard callbacks `after_create`, `after_update` and `before_destroy`
42
+
43
+ If you want customize the fields that determine the logging action, you can use the Array syntax like the following example:
44
+ ```ruby
45
+ loggable_event update: [:name,:surname]
46
+ ```
47
+ So with this syntax, ActiveAudit will log the update event only if `name` __OR__ `surname` fields are changed.
48
+
49
+ You can also specify the value that a field must have to trigger the logging:
50
+ ```ruby
51
+ loggable_event update: [{state: "confirmed"}]
52
+ ```
53
+ In this case ActiveAudit will log the update event only if `state` field is changed and the new value is "confirmed"
54
+
55
+ It's possible to use custom event name that is managed like an update event. For exaple you can create an event called `confirmation` like this:
56
+ ```ruby
57
+ loggable_event confirmation: [{state: "confirmed"}]
58
+ ```
59
+
60
+ ## Contributing to ActiveAudit
61
+
62
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
63
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
64
+ * Fork the project.
65
+ * Start a feature/bugfix branch.
66
+ * Commit and push until you are happy with your contribution.
67
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
68
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
69
+
70
+ ## Copyright
71
+
72
+ Copyright (c) 2013 Massimo Maino. See LICENSE.txt for further details.
73
+
@@ -0,0 +1,13 @@
1
+ class ActiveAudit::Audit < ActiveRecord::Base
2
+
3
+ attr_accessible :obj_id, :obj_type, :activity, :user_id
4
+ validates_presence_of :obj_id, :obj_type, :activity
5
+
6
+ if defined?(::Rails)
7
+ ::ActiveAudit::AuditConcern
8
+ if defined?(::ActiveAudit::AuditConcern)
9
+ include ::ActiveAudit::AuditConcern
10
+ end
11
+ end
12
+
13
+ end
@@ -0,0 +1,14 @@
1
+ class CreateActiveAudits < ActiveRecord::Migration
2
+ def change
3
+ create_table :audits do |t|
4
+ t.integer :obj_id
5
+ t.string :obj_type
6
+ t.integer :user_id
7
+ t.text :activity
8
+
9
+ t.timestamps
10
+ end
11
+ add_index :audits, :obj_id
12
+ add_index :audits, :user_id
13
+ end
14
+ end
@@ -0,0 +1,14 @@
1
+ require 'active_audit/logger'
2
+
3
+ module ActiveAudit
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ initializer 'activeservice.autoload', :before => :set_autoload_paths do |app|
7
+ app.config.autoload_paths += %W(#{app.config.root}/app/concerns/active_audit)
8
+ end
9
+ #ActiveRecord::Base.class_eval do
10
+ #include ActiveAudit::Logger
11
+ #end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,95 @@
1
+ require 'active_support/concern'
2
+
3
+ module ActiveAudit
4
+ module Logger
5
+
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+
10
+ after_create :log_activity_on_create
11
+ after_update :log_activity_on_update
12
+ before_destroy :log_activity_on_destroy
13
+
14
+ attr_accessible :audit_user_id
15
+ attr_accessor :audit_user_id
16
+
17
+ @@_loggable_events = {}
18
+
19
+ def self.loggable_event(event)
20
+ @@_loggable_events.merge! event
21
+ end
22
+
23
+ def self.loggable_events
24
+ @@_loggable_events
25
+ end
26
+
27
+ def log_activity_on_create
28
+ @@_loggable_events.each do |k,v|
29
+ next unless k.eql?(:create)
30
+ if v.eql?(true)
31
+ ActiveAudit::Audit.create obj_id: id, obj_type: self.class.to_s, user_id: audit_user_id, activity: k.to_s
32
+ elsif v.is_a?(Array)
33
+ log_event = false
34
+ v.each do |field|
35
+ if field.is_a?(Hash)
36
+ key, value = field.first
37
+ if self.send(key.to_s).eql?(value)
38
+ log_event = true
39
+ break
40
+ end
41
+ elsif field.is_a?(Symbol)
42
+ if self.send(field.to_s)
43
+ log_event = true
44
+ break
45
+ end
46
+ end
47
+ end
48
+ if log_event
49
+ ActiveAudit::Audit.create obj_id: id, obj_type: self.class.to_s, user_id: audit_user_id, activity: k.to_s
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ def log_activity_on_update
56
+ @@_loggable_events.each do |k,v|
57
+ next if [:create,:destroy].include?(k)
58
+ if k.eql?(:update) and v.eql?(true)
59
+ ActiveAudit::Audit.create obj_id: id, obj_type: self.class.to_s, user_id: audit_user_id, activity: k.to_s
60
+ else
61
+ if v.is_a?(Array)
62
+ log_event = false
63
+ v.each do |field|
64
+ if field.is_a?(Hash)
65
+ key, value = field.first
66
+ if self.send("#{key}_changed?") and self.send(key.to_s).eql?(value)
67
+ log_event = true
68
+ break
69
+ end
70
+ elsif field.is_a?(Symbol)
71
+ if self.send("#{field}_changed?")
72
+ log_event = true
73
+ break
74
+ end
75
+ end
76
+ end
77
+ if log_event
78
+ ActiveAudit::Audit.create obj_id: id, obj_type: self.class.to_s, user_id: audit_user_id, activity: k.to_s
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
84
+
85
+ def log_activity_on_destroy
86
+ @@_loggable_events.each do |k,v|
87
+ next unless k.eql?(:destroy)
88
+ ActiveAudit::Audit.create obj_id: id, obj_type: self.class.to_s, user_id: audit_user_id, activity: k.to_s
89
+ end
90
+ end
91
+
92
+ end
93
+
94
+ end
95
+ end
@@ -0,0 +1,9 @@
1
+ module ActiveAudit
2
+ def self.load!
3
+ if defined?(::Rails)
4
+ require 'active_audit/engine'
5
+ end
6
+ end
7
+ end
8
+
9
+ ActiveAudit.load!
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+ require 'models/person'
3
+ require './app/models/active_audit/audit'
4
+
5
+ describe "Callbacks" do
6
+
7
+ before :each do
8
+ ActiveAudit::Audit.destroy_all
9
+ @person = Person.create name: Faker::Name.first_name, surname: Faker::Name.last_name, age: 10, audit_user_id: 1
10
+ @person.should_not be_nil
11
+ end
12
+
13
+ it "should create a 'create log'" do
14
+ ActiveAudit::Audit.count.should eq(1)
15
+ latest_log = ActiveAudit::Audit.last
16
+ latest_log.obj_id.should eq(@person.id)
17
+ latest_log.obj_type.should eq("Person")
18
+ latest_log.activity.should eq("create")
19
+ latest_log.user_id.should eq(1)
20
+ end
21
+
22
+ it "should create a 'destroy log'" do
23
+ @person.destroy
24
+ ActiveAudit::Audit.count.should eq(2)
25
+ latest_log = ActiveAudit::Audit.last
26
+ latest_log.obj_id.should eq(@person.id)
27
+ latest_log.obj_type.should eq("Person")
28
+ latest_log.activity.should eq("destroy")
29
+ latest_log.user_id.should eq(1)
30
+ end
31
+
32
+ it "should create a 'update log' - One field" do
33
+ @person.name = "new_#{Faker::Name.first_name}"
34
+ @person.save
35
+ ActiveAudit::Audit.count.should eq(2)
36
+ latest_log = ActiveAudit::Audit.last
37
+ latest_log.obj_id.should eq(@person.id)
38
+ latest_log.obj_type.should eq("Person")
39
+ latest_log.activity.should eq("update")
40
+ latest_log.user_id.should eq(1)
41
+ end
42
+
43
+ it "should create a 'update log' - More fields" do
44
+ @person.name = "new_#{Faker::Name.first_name}"
45
+ @person.surname = "new_#{Faker::Name.last_name}"
46
+ @person.email = Faker::Internet.email
47
+ @person.save
48
+ ActiveAudit::Audit.count.should eq(2)
49
+ latest_log = ActiveAudit::Audit.last
50
+ latest_log.obj_id.should eq(@person.id)
51
+ latest_log.obj_type.should eq("Person")
52
+ latest_log.activity.should eq("update")
53
+ latest_log.user_id.should eq(1)
54
+ end
55
+
56
+ it "should not create a 'update log' - External field" do
57
+ @person.email = Faker::Internet.email
58
+ @person.save
59
+ ActiveAudit::Audit.count.should eq(1)
60
+ end
61
+
62
+ it "should not create a 'update log' - Field linked to other event" do
63
+ @person.age = 1
64
+ @person.save
65
+ ActiveAudit::Audit.count.should eq(1)
66
+ end
67
+
68
+ it "should create a 'child log'" do
69
+ @person.age = 14
70
+ @person.save
71
+ ActiveAudit::Audit.count.should eq(2)
72
+ latest_log = ActiveAudit::Audit.last
73
+ latest_log.obj_id.should eq(@person.id)
74
+ latest_log.obj_type.should eq("Person")
75
+ latest_log.activity.should eq("child")
76
+ latest_log.user_id.should eq(1)
77
+ end
78
+
79
+ it "should create a 'adult log'" do
80
+ @person.age = 18
81
+ @person.save
82
+ ActiveAudit::Audit.count.should eq(2)
83
+ latest_log = ActiveAudit::Audit.last
84
+ latest_log.obj_id.should eq(@person.id)
85
+ latest_log.obj_type.should eq("Person")
86
+ latest_log.activity.should eq("adult")
87
+ latest_log.user_id.should eq(1)
88
+ end
89
+
90
+ it "should create a 'adult log' and 'update log'" do
91
+ @person.age = 18
92
+ @person.name = "new_#{Faker::Name.first_name}"
93
+ @person.save
94
+ ActiveAudit::Audit.count.should eq(3)
95
+ latest_logs = ActiveAudit::Audit.last(2)
96
+ first_log = latest_logs.first
97
+ first_log.obj_id.should eq(@person.id)
98
+ first_log.obj_type.should eq("Person")
99
+ first_log.activity.should eq("update")
100
+ first_log.user_id.should eq(1)
101
+ second_log = latest_logs.second
102
+ second_log.obj_id.should eq(@person.id)
103
+ second_log.obj_type.should eq("Person")
104
+ second_log.activity.should eq("adult")
105
+ second_log.user_id.should eq(1)
106
+ end
107
+
108
+ end
data/spec/core_spec.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ require 'models/person'
3
+
4
+ describe "Core" do
5
+
6
+ it "should have loggable_event method" do
7
+ ret = Person.respond_to?(:loggable_event)
8
+ ret.should be_true
9
+ end
10
+
11
+ it "should have audit_user_id attribute" do
12
+ person = Person.new
13
+ ret = person.respond_to?(:audit_user_id)
14
+ ret.should be_true
15
+ end
16
+
17
+ it "should have right loggable_events" do
18
+ events = Person.loggable_events
19
+ events.should eq({create: true, destroy: true, update: [:name,:surname], adult: [{age: 18}], child: [{age: 14}]})
20
+ end
21
+
22
+ end
@@ -0,0 +1,12 @@
1
+ class Person < ActiveRecord::Base
2
+ include ActiveAudit::Logger
3
+
4
+ attr_accessible :name, :surname, :age
5
+
6
+ loggable_event create: true
7
+ loggable_event destroy: true
8
+ loggable_event update: [:name,:surname]
9
+ loggable_event adult: [{age: 18}]
10
+ loggable_event child: [{age: 14}]
11
+
12
+ end
@@ -0,0 +1,35 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require 'active_audit/logger'
9
+ require 'active_record'
10
+ require 'faker'
11
+ require './db/migrate/1_create_active_audits'
12
+
13
+ puts "Removing test database..."
14
+ if File.exist? "spec/db/active_audit_test.sqlite3"
15
+ File.unlink "spec/db/active_audit_test.sqlite3"
16
+ end
17
+ ActiveRecord::Base.establish_connection(
18
+ :adapter => "sqlite3",
19
+ :database => "spec/db/active_audit_test.sqlite3",
20
+ )
21
+ puts "Creating test tables..."
22
+ ActiveRecord::Base.connection.execute("CREATE TABLE `people`(`id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` TEXT, `surname` TEXT, `email` TEXT, `age` INT, `created_at` DATETIME, `updated_at` DATETIME);")
23
+ CreateActiveAudits.migrate :up
24
+
25
+ RSpec.configure do |config|
26
+ config.treat_symbols_as_metadata_keys_with_true_values = true
27
+ config.run_all_when_everything_filtered = true
28
+ config.filter_run :focus
29
+
30
+ # Run specs in random order to surface order dependencies. If you find an
31
+ # order dependency and want to debug it, you can fix the order by providing
32
+ # the seed, which is printed after each run.
33
+ # --seed 1234
34
+ config.order = 'random'
35
+ end
metadata ADDED
@@ -0,0 +1,173 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeaudit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Massimo Maino
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-26 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rdoc
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3.12'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3.12'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: jeweler
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 1.8.4
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 1.8.4
62
+ - !ruby/object:Gem::Dependency
63
+ name: rspec
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ - !ruby/object:Gem::Dependency
79
+ name: activerecord
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: sqlite3
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ - !ruby/object:Gem::Dependency
111
+ name: faker
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ! '>='
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ description: Audit support for Active Record
127
+ email: maintux@gmail.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files:
131
+ - LICENSE.txt
132
+ - README.md
133
+ files:
134
+ - app/models/active_audit/audit.rb
135
+ - db/migrate/1_create_active_audits.rb
136
+ - lib/active_audit/engine.rb
137
+ - lib/active_audit/logger.rb
138
+ - lib/activeaudit.rb
139
+ - spec/callbacks_spec.rb
140
+ - spec/core_spec.rb
141
+ - spec/models/person.rb
142
+ - spec/spec_helper.rb
143
+ - LICENSE.txt
144
+ - README.md
145
+ homepage: http://github.com/maintux/activeaudit
146
+ licenses:
147
+ - MIT
148
+ post_install_message:
149
+ rdoc_options: []
150
+ require_paths:
151
+ - lib
152
+ required_ruby_version: !ruby/object:Gem::Requirement
153
+ none: false
154
+ requirements:
155
+ - - ! '>='
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ segments:
159
+ - 0
160
+ hash: 4479424809207949742
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ! '>='
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ requirements: []
168
+ rubyforge_project:
169
+ rubygems_version: 1.8.24
170
+ signing_key:
171
+ specification_version: 3
172
+ summary: Audit support for Active Record
173
+ test_files: []