active_audit 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3639ddec358dc194b95215ac12caa995fc05760f
4
+ data.tar.gz: 28917214926c09dab3beeb569c63ffaf46f4debe
5
+ SHA512:
6
+ metadata.gz: 33ef0015420b4123923c6b0ce6c241f1fbafe353b4945ff49be485ac38ea00fe305c2c50620bf2196d968621e9a8560c26cca9ee71643c02891ca8568f1be365
7
+ data.tar.gz: eaa2b2385daf2418ac94a3eecc039e95c366e820e5653bc7b818345f96b8901db662124eb4b32cd3389cbf7b9adcb4db4101ae0d793153329df17a39c2fdbb93
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --backtrace
2
+ --color
3
+ --format progress
4
+ --order defined
data/.travis.yml ADDED
@@ -0,0 +1,13 @@
1
+ before_install:
2
+ - sudo apt-get install libxml2-dev
3
+ cache: bundler
4
+ language: ruby
5
+ notifications:
6
+ email:
7
+ recipients:
8
+ - j.gomez@drexed.com
9
+ on_failure: change
10
+ on_success: never
11
+ rvm:
12
+ - ruby-head
13
+ script: 'bundle exec rake'
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_audit.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Juan Gomez
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all 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,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,82 @@
1
+ # ActiveAudit
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/active_audit.svg)](http://badge.fury.io/rb/active_audit)
4
+ [![Build Status](https://travis-ci.org/drexed/active_audit.svg?branch=master)](https://travis-ci.org/drexed/active_audit)
5
+ [![Coverage Status](https://coveralls.io/repos/drexed/active_audit/badge.png)](https://coveralls.io/r/drexed/active_audit)
6
+
7
+ ActiveAudit is a library for auditing record changes.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'active_audit'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install active_audit
24
+
25
+ ## Table of Contents
26
+
27
+ * [Configurations](#configurations)
28
+ * [Usage](#usage)
29
+
30
+ ## Configurations
31
+
32
+ **Options:**
33
+ * auditor
34
+ * source
35
+
36
+ `rails generate active_audit:install` will generate the following `active_audit.rb` file:
37
+
38
+ ```ruby
39
+ # config/initalizers/active_audit.rb
40
+
41
+ ActiveAudit.configure do |config|
42
+ # option = default
43
+
44
+ config.auditor = :current_user
45
+ config.source = :app
46
+ end
47
+
48
+ # NOTE:
49
+ # Will also generate the following files:
50
+ # app/models/audit.rb
51
+ # db/migrate/*_create_audits.rb
52
+ ```
53
+
54
+ ## Usage
55
+ ```ruby
56
+ user = User.first.update(name: "bill")
57
+ Audit.record!(user, :update, source: "api", remote_ip: "123.12.123.123")
58
+
59
+ # OR:
60
+
61
+ class Comment < ActiveRecord::Base
62
+
63
+ include ActiveAudit::Auditable
64
+
65
+ after_create :trigger_audit_create!
66
+ after_update :trigger_audit_update!
67
+ after_destroy :trigger_audit_destroy!
68
+
69
+ end
70
+
71
+ comment = Comment.create!(body: "Foobar comment")
72
+
73
+ Audit.first #=> #<Audit id: 1, auditable_id: 1, auditable_type: "Comment", user_id: 1, source: 0, version: 0, action: "create", revisions: {"body"=>[nil, "Foobar"], "user_id"=>[nil, 1], "created_at"=>[nil, 2015-09-30 19:48:48 UTC], "updated_at"=>[nil, 2015-09-30 19:48:48 UTC], "id"=>[nil, 1]}, comment: "Foobar comment", remote_ip: nil, remote_uuid: nil, created_at: "2015-09-30 19:48:48", updated_at: "2015-09-30 19:48:48">
74
+ ```
75
+
76
+ ## Contributing
77
+
78
+ 1. Fork it ( https://github.com/[my-github-username]/active_audit/fork )
79
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
80
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
81
+ 4. Push to the branch (`git push origin my-new-feature`)
82
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task default: :spec
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_audit/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_audit"
8
+ spec.version = ActiveAudit::VERSION
9
+ spec.authors = ["Juan Gomez"]
10
+ spec.email = ["j.gomez@drexed.com"]
11
+
12
+ spec.summary = %q{Gem for auditing record changes.}
13
+ spec.description = %q{Library for auditing record changes.}
14
+ spec.homepage = "https://github.com/drexed/active_audit"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "rails"
23
+
24
+ spec.add_development_dependency "bundler"
25
+ spec.add_development_dependency "coveralls"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "sqlite3"
29
+ spec.add_development_dependency "database_cleaner"
30
+ spec.add_development_dependency "generator_spec"
31
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_audit"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/rake ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # This file was generated by Bundler.
4
+ #
5
+ # The application 'rake' is installed as part of a gem, and
6
+ # this file is here to facilitate running it.
7
+ #
8
+
9
+ require 'pathname'
10
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path("../../Gemfile",
11
+ Pathname.new(__FILE__).realpath)
12
+
13
+ require 'rubygems'
14
+ require 'bundler/setup'
15
+
16
+ load Gem.bin_path('rake', 'rake')
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ en:
2
+ active_audit:
3
+ archival:
4
+ archived: "Auditd"
5
+ unarchived: "Unarchived"
@@ -0,0 +1,45 @@
1
+ module ActiveAudit
2
+ module Auditable
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ has_many :audits, as: :auditable
7
+
8
+ attr_accessor :audit_comment, :audit_remote_ip, :audit_remote_uuid, :audit_source
9
+
10
+ before_validation :set_audit_source!, if: -> (obj) { obj.audit_source.nil? }
11
+ before_destroy :set_audit_source!, if: -> (obj) { obj.audit_source.nil? }
12
+ end
13
+
14
+ private
15
+
16
+ def audit_params
17
+ {
18
+ auditable: self,
19
+ user: ActiveAudit.configuration.auditor,
20
+ source: audit_source,
21
+ revisions: changes,
22
+ comment: audit_comment,
23
+ remote_ip: audit_remote_ip,
24
+ remote_uuid: audit_remote_uuid
25
+ }
26
+ end
27
+
28
+ def set_audit_source!
29
+ self.audit_source = ActiveAudit.configuration.source
30
+ end
31
+
32
+ def trigger_audit_create!
33
+ Audit.create!(audit_params.merge({ action: :create }))
34
+ end
35
+
36
+ def trigger_audit_update!
37
+ Audit.create!(audit_params.merge({ action: :update }))
38
+ end
39
+
40
+ def trigger_audit_destroy!
41
+ Audit.create!(audit_params.merge({ action: :destroy }))
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,10 @@
1
+ class ActiveAudit::Configuration
2
+
3
+ attr_accessor :auditor, :source
4
+
5
+ def initialize
6
+ @auditor = :current_user
7
+ @source = :app
8
+ end
9
+
10
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveAudit
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,49 @@
1
+ require "active_audit/version"
2
+ require "active_audit/configuration"
3
+
4
+ module ActiveAudit
5
+
6
+ class << self
7
+ attr_accessor :configuration
8
+ end
9
+
10
+ def self.configuration
11
+ @configuration ||= Configuration.new
12
+ end
13
+
14
+ def self.configuration=(config)
15
+ @configuration = config
16
+ end
17
+
18
+ def self.configure
19
+ yield(configuration)
20
+ end
21
+
22
+ end
23
+
24
+ require "active_audit/auditable"
25
+ require "generators/active_audit/install_generator"
26
+
27
+ if defined?(Rails)
28
+ require 'rails'
29
+
30
+ module ActiveAudit
31
+ class Railtie < ::Rails::Railtie
32
+
33
+ initializer 'active_audit' do |app|
34
+ ActiveAudit::Railtie.instance_eval do
35
+ [app.config.i18n.available_locales].flatten.each do |locale|
36
+ (I18n.load_path << path(locale)) if File.file?(path(locale))
37
+ end
38
+ end
39
+ end
40
+
41
+ protected
42
+
43
+ def self.path(locale)
44
+ File.expand_path("../../config/locales/#{locale}.yml", __FILE__)
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+ require "rails/generators"
2
+
3
+ module ActiveAudit
4
+ class InstallGenerator < Rails::Generators::Base
5
+ source_root File.expand_path('../templates', __FILE__)
6
+
7
+ def create_migration_file
8
+ timestamp = [Time.now.utc.strftime("%Y%m%d%H%M%S"), "_"].join
9
+ copy_file("install_migration.rb", "db/migrate/#{timestamp if ActiveRecord::Base.timestamped_migrations }create_audits.rb")
10
+ end
11
+
12
+ def copy_initializer_file
13
+ copy_file("install_initializer.rb", "config/initializers/active_audit.rb")
14
+ end
15
+
16
+ def copy_model_file
17
+ copy_file("install_model.rb", "app/models/audit.rb")
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,4 @@
1
+ ActiveAudit.configure do |config|
2
+ config.auditor = :current_user
3
+ config.source = :app
4
+ end
@@ -0,0 +1,16 @@
1
+ class CreateAudits < ActiveRecord::Migration
2
+ def change
3
+ create_table :audits do |t|
4
+ t.references :auditable, null: false, index: true, polymorphic: true
5
+ t.references :user, index: true
6
+ t.integer :source, null: false, default: 0
7
+ t.integer :version, null: false, default: 0
8
+ t.string :action, null: false
9
+ t.text :revisions
10
+ t.text :comment
11
+ t.string :remote_ip
12
+ t.string :remote_uuid
13
+ t.timestamps null: false
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ class Audit < ActiveRecord::Base
2
+
3
+ SOURCES = { app: 0, api: 1 }
4
+
5
+ belongs_to :auditable, polymorphic: true
6
+ belongs_to :user
7
+
8
+ enum source: SOURCES
9
+
10
+ serialize :revisions
11
+
12
+ validates :action, presence: true
13
+ validates :auditable_id, numericality: { integer: true }
14
+ validates :auditable_type, presence: true
15
+ validates :source, inclusion: SOURCES.dup.keys.map { |key| key.to_s }
16
+ validates :user_id, numericality: { integer: true },
17
+ allow_blank: true,
18
+ allow_nil: true
19
+ validates :version, numericality: { integer: true }
20
+
21
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_audit
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Juan Gomez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-30 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: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coveralls
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: generator_spec
113
+ requirement: !ruby/object:Gem::Requirement
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
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Library for auditing record changes.
126
+ email:
127
+ - j.gomez@drexed.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".coveralls.yml"
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
136
+ - CODE_OF_CONDUCT.md
137
+ - Gemfile
138
+ - LICENSE.txt
139
+ - README.md
140
+ - Rakefile
141
+ - active_audit.gemspec
142
+ - bin/console
143
+ - bin/rake
144
+ - bin/setup
145
+ - config/locales/en.yml
146
+ - lib/active_audit.rb
147
+ - lib/active_audit/auditable.rb
148
+ - lib/active_audit/configuration.rb
149
+ - lib/active_audit/version.rb
150
+ - lib/generators/active_audit/install_generator.rb
151
+ - lib/generators/active_audit/templates/install_initializer.rb
152
+ - lib/generators/active_audit/templates/install_migration.rb
153
+ - lib/generators/active_audit/templates/install_model.rb
154
+ homepage: https://github.com/drexed/active_audit
155
+ licenses:
156
+ - MIT
157
+ metadata: {}
158
+ post_install_message:
159
+ rdoc_options: []
160
+ require_paths:
161
+ - lib
162
+ required_ruby_version: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ required_rubygems_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ requirements: []
173
+ rubyforge_project:
174
+ rubygems_version: 2.4.8
175
+ signing_key:
176
+ specification_version: 4
177
+ summary: Gem for auditing record changes.
178
+ test_files: []