auditing 1.2.0 → 1.2.2

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.
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ test.sqlite3
data/.rvmrc CHANGED
@@ -1,5 +1,5 @@
1
- if [[ -n "$rvm_environments_path" && -s "$rvm_environments_path/ruby-1.8.7-p302@auditing" ]] ; then
2
- \. "$rvm_environments_path/ruby-1.8.7-p302@auditing"
1
+ if [[ -n "$rvm_environments_path" && -s "$rvm_environments_path/ruby-1.9.2-p136@auditing" ]] ; then
2
+ \. "$rvm_environments_path/ruby-1.9.2-p136@auditing"
3
3
  else
4
- rvm --create use "ruby-1.8.7-p302@auditing"
4
+ rvm --create use "ruby-1.9.2-p136@auditing"
5
5
  fi
data/Gemfile CHANGED
@@ -1,7 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
- group :development, :test do
4
- gem 'activerecord'
5
- gem 'sqlite3-ruby'
6
- gem 'rspec'
7
- end
3
+ # Specify your gem's dependencies in auditing.gemspec
4
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,20 +1,12 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ auditing (1.2.2)
5
+
1
6
  GEM
2
7
  remote: http://rubygems.org/
3
8
  specs:
4
- activemodel (3.0.3)
5
- activesupport (= 3.0.3)
6
- builder (~> 2.1.2)
7
- i18n (~> 0.4)
8
- activerecord (3.0.3)
9
- activemodel (= 3.0.3)
10
- activesupport (= 3.0.3)
11
- arel (~> 2.0.2)
12
- tzinfo (~> 0.3.23)
13
- activesupport (3.0.3)
14
- arel (2.0.7)
15
- builder (2.1.2)
16
9
  diff-lcs (1.1.2)
17
- i18n (0.5.0)
18
10
  rspec (2.4.0)
19
11
  rspec-core (~> 2.4.0)
20
12
  rspec-expectations (~> 2.4.0)
@@ -23,15 +15,10 @@ GEM
23
15
  rspec-expectations (2.4.0)
24
16
  diff-lcs (~> 1.1.2)
25
17
  rspec-mocks (2.4.0)
26
- sqlite3 (1.3.3)
27
- sqlite3-ruby (1.3.3)
28
- sqlite3 (>= 1.3.3)
29
- tzinfo (0.3.24)
30
18
 
31
19
  PLATFORMS
32
20
  ruby
33
21
 
34
22
  DEPENDENCIES
35
- activerecord
23
+ auditing!
36
24
  rspec
37
- sqlite3-ruby
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Brad Cantin
1
+ Copyright (c) 2011 Brad Cantin
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.rdoc CHANGED
@@ -96,6 +96,19 @@ the audits table, you can add it in another migration
96
96
 
97
97
  add_column :audits, :user_id, :integer
98
98
 
99
+ You will need to add the relationship to the Audit class as well
100
+
101
+ class Audit < ActiveRecord::Base
102
+ belongs_to :user
103
+ end
104
+
105
+ If you want to see all the audits for a particular user, you can
106
+ add the has_many relationship to the User model as well
107
+
108
+ class User < ActiveRecord::Base
109
+ has_many :audits
110
+ end
111
+
99
112
  Your user class should respond to a class method called current_user
100
113
  and current_user= (some authentication systems do this, others do not).
101
114
 
@@ -119,7 +132,7 @@ get the current user properly
119
132
  class ApplicationController < ActionController::Base
120
133
 
121
134
  before_filter :set_current_user
122
- after_filter :unser_current_user
135
+ after_filter :unset_current_user
123
136
 
124
137
  private
125
138
 
data/Rakefile CHANGED
@@ -1,45 +1,2 @@
1
- require 'rubygems'
2
- require 'rake'
3
-
4
- begin
5
- require 'jeweler'
6
- Jeweler::Tasks.new do |gem|
7
- gem.name = "auditing"
8
- gem.summary = %Q{A gem to keep track of audit hisory of a record}
9
- gem.description = %Q{acts_as_versioned is good. This allows an attribute level rollback instead}
10
- gem.email = "brad.cantin@gmail.com"
11
- gem.homepage = "http://github.com/bcantin/auditing"
12
- gem.authors = ["Brad Cantin"]
13
- gem.add_development_dependency "rspec", ">= 2.0"
14
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
- end
16
- Jeweler::GemcutterTasks.new
17
- rescue LoadError
18
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
- end
20
-
21
- # require 'spec/rake/spectask'
22
- # Spec::Rake::SpecTask.new(:spec) do |spec|
23
- # spec.libs << 'lib' << 'spec'
24
- # spec.spec_files = FileList['spec/**/*_spec.rb']
25
- # end
26
- #
27
- # Spec::Rake::SpecTask.new(:rcov) do |spec|
28
- # spec.libs << 'lib' << 'spec'
29
- # spec.pattern = 'spec/**/*_spec.rb'
30
- # spec.rcov = true
31
- # end
32
- #
33
- # task :spec => :check_dependencies
34
-
35
- task :default => :spec
36
-
37
- require 'rake/rdoctask'
38
- Rake::RDocTask.new do |rdoc|
39
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
-
41
- rdoc.rdoc_dir = 'rdoc'
42
- rdoc.title = "auditing #{version}"
43
- rdoc.rdoc_files.include('README*')
44
- rdoc.rdoc_files.include('lib/**/*.rb')
45
- end
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/auditing.gemspec CHANGED
@@ -1,77 +1,23 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "auditing/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = %q{auditing}
8
- s.version = "1.2.0"
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Brad Cantin"]
12
- s.date = %q{2011-02-01}
6
+ s.name = %q{auditing}
7
+ s.version = Auditing::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Brad Cantin"]
10
+ s.email = ['brad.cantin@gmail.com']
11
+ s.homepage = 'https://github.com/bcantin/auditing'
13
12
  s.description = %q{acts_as_versioned is good. This allows an attribute level rollback instead}
14
- s.email = %q{brad.cantin@gmail.com}
15
- s.extra_rdoc_files = [
16
- "LICENSE",
17
- "README.rdoc"
18
- ]
19
- s.files = [
20
- ".bundle/config",
21
- ".document",
22
- ".rspec",
23
- ".rvmrc",
24
- "Gemfile",
25
- "Gemfile.lock",
26
- "LICENSE",
27
- "README.rdoc",
28
- "Rakefile",
29
- "VERSION",
30
- "auditing.gemspec",
31
- "lib/auditing.rb",
32
- "lib/auditing/audit_relationship.rb",
33
- "lib/auditing/auditor.rb",
34
- "lib/auditing/base.rb",
35
- "spec/auditing/audit_relationship_spec.rb",
36
- "spec/auditing/auditor_spec.rb",
37
- "spec/auditing/base_spec.rb",
38
- "spec/auditing/with_users_spec.rb",
39
- "spec/schema.rb",
40
- "spec/spec_helper.rb"
41
- ]
42
- s.homepage = %q{http://github.com/bcantin/auditing}
43
- s.require_paths = ["lib"]
44
- s.rubygems_version = %q{1.3.7}
45
- s.summary = %q{A gem to keep track of audit hisory of a record}
46
- s.test_files = [
47
- "spec/auditing/audit_relationship_spec.rb",
48
- "spec/auditing/auditor_spec.rb",
49
- "spec/auditing/base_spec.rb",
50
- "spec/auditing/with_users_spec.rb",
51
- "spec/schema.rb",
52
- "spec/spec_helper.rb"
53
- ]
54
-
55
- if s.respond_to? :specification_version then
56
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
57
- s.specification_version = 3
13
+ s.summary = %q{A gem to keep track of audit hisory of a record}
14
+
15
+ s.add_development_dependency "rspec"
16
+
17
+ s.rubyforge_project = "auditing"
58
18
 
59
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
60
- s.add_development_dependency(%q<activerecord>, [">= 0"])
61
- s.add_development_dependency(%q<sqlite3-ruby>, [">= 0"])
62
- s.add_development_dependency(%q<rspec>, [">= 0"])
63
- s.add_development_dependency(%q<rspec>, [">= 2.0"])
64
- else
65
- s.add_dependency(%q<activerecord>, [">= 0"])
66
- s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
67
- s.add_dependency(%q<rspec>, [">= 0"])
68
- s.add_dependency(%q<rspec>, [">= 2.0"])
69
- end
70
- else
71
- s.add_dependency(%q<activerecord>, [">= 0"])
72
- s.add_dependency(%q<sqlite3-ruby>, [">= 0"])
73
- s.add_dependency(%q<rspec>, [">= 0"])
74
- s.add_dependency(%q<rspec>, [">= 2.0"])
75
- end
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ["lib"]
76
23
  end
77
-
@@ -0,0 +1,3 @@
1
+ module Auditing
2
+ VERSION = "1.2.2"
3
+ end
data/lib/auditing.rb CHANGED
@@ -1,3 +1,6 @@
1
+ module Auditing
2
+ end
3
+
1
4
  require 'auditing/base'
2
5
  require 'auditing/auditor'
3
6
  require 'auditing/audit_relationship'
@@ -0,0 +1,16 @@
1
+ Description:
2
+ Create an audit model, migration, controller, helper, and an index view
3
+
4
+ Example:
5
+ rails generate auditing:install
6
+
7
+ This will create:
8
+ app/controllers/audits_controller.rb
9
+ app/helpers/audits_helper.rb
10
+ app/models/audit.rb
11
+ app/views/audits/index.html.erb
12
+ db/migrate/#{time_stamp}_create_audits.rb
13
+
14
+ This will modify config/routes.rb with
15
+ match ':resource/:id/audits' => 'audits#index', :as => 'audits'
16
+ match 'audit/rollback/:id' => 'audits#rollback', :as => 'audit_rollback'
@@ -0,0 +1,48 @@
1
+ require 'rails/generators/migration'
2
+
3
+ module Auditing
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+
7
+ include Rails::Generators::Migration
8
+
9
+ source_root File.expand_path("../templates", __FILE__)
10
+
11
+ def create_controller_files
12
+ template 'audits_controller.rb', 'app/controllers/audits_controller.rb'
13
+ end
14
+
15
+ def create_helper_files
16
+ template 'audits_helper.rb', 'app/helpers/audits_helper.rb'
17
+ end
18
+
19
+ def create_model_files
20
+ template 'audit.rb', 'app/models/audit.rb'
21
+ end
22
+
23
+ def create_view_files
24
+ template 'index.html.erb', 'app/views/audits/index.html.erb'
25
+ end
26
+
27
+ def create_routes
28
+ route "match ':resource/:id/audits' => 'audits#index', :as => 'audits'"
29
+ route "match 'audit/rollback/:id' => 'audits#rollback', :as => 'audit_rollback'"
30
+ end
31
+
32
+ def create_migration
33
+ migration_template 'migration.rb', "db/migrate/create_audits.rb"
34
+ end
35
+
36
+ private
37
+
38
+ def self.next_migration_number(dirname) #:nodoc:
39
+ if ActiveRecord::Base.timestamped_migrations
40
+ Time.now.utc.strftime("%Y%m%d%H%M%S")
41
+ else
42
+ "%.3d" % (current_migration_number(dirname) + 1)
43
+ end
44
+ end
45
+
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,7 @@
1
+ class Audit < ActiveRecord::Base
2
+
3
+ include Auditing::Auditor
4
+ belongs_to :auditable, :polymorphic => true
5
+ belongs_to :association, :polymorphic => true
6
+
7
+ end
@@ -0,0 +1,17 @@
1
+ class AuditsController < ApplicationController
2
+
3
+ def index
4
+ resource = params[:resource].classify.constantize
5
+ @resource = resource.find(params[:id])
6
+ @audits = @resource.audits
7
+ end
8
+
9
+ def rollback
10
+ audit = Audit.find(params[:id])
11
+ audit.rollback
12
+
13
+ resource = audit.auditable
14
+ redirect_to audits_path(resource.class.table_name, resource)
15
+ end
16
+
17
+ end
@@ -0,0 +1,75 @@
1
+ module AuditsHelper
2
+
3
+ def description(audit)
4
+ str = case audit.action
5
+ when 'created'
6
+ "Created Record #{audit.auditable_type}"
7
+ when 'updated'
8
+ if audit.association.blank?
9
+ if audit.field_name.match(/_id$/)
10
+ "Old: #{get_belongs_to(audit, audit.old_value)} <br/>" +
11
+ "New: #{get_belongs_to(audit, audit.new_value)}"
12
+ else
13
+ "Old: #{audit.old_value} <br/>" +
14
+ "New: #{audit.new_value}"
15
+ end
16
+ else
17
+ if audit.old_value
18
+ "Old: #{get_has_many(audit, audit.old_value)}<br/>"+
19
+ "New: #{get_has_many(audit, audit.new_value)}"
20
+ else
21
+ "New: #{get_has_many(audit, audit.new_value)}"
22
+ end
23
+ end
24
+ when 'added'
25
+ if audit.old_value
26
+ "Old: #{get_has_many(audit, audit.old_value)}<br/>"+
27
+ "New: #{get_has_many(audit, audit.new_value)}"
28
+ else
29
+ "New: #{get_has_many(audit, audit.new_value)}"
30
+ end
31
+ when 'removed'
32
+ "Removed #{audit.association_type}"
33
+ end
34
+
35
+ str.html_safe
36
+ end
37
+
38
+ def get_has_many(audit, value)
39
+ return nil unless value
40
+ if audit.field_name.match(/_id$/)
41
+ get_belongs_to(audit, value)
42
+ else
43
+ value
44
+ end
45
+ end
46
+
47
+ ##
48
+ # For a belongs_to association, we only store the ID of what is being
49
+ # refered to (:industry_id => 5). We cannot guess what would be relevant
50
+ # to actually show for the value. We recommend implementing a #to_label
51
+ # method and/or overriding this method to display what is relevant to
52
+ # your application.
53
+ #
54
+ # EG:
55
+ # class Industry < ActiveRecord::Base
56
+ # has_many :companies
57
+ #
58
+ # def to_label
59
+ # name
60
+ # end
61
+ # end
62
+
63
+ def get_belongs_to(audit, value)
64
+ return nil unless value
65
+ if audit.association
66
+ klass = audit.association.class.reflect_on_association(audit.field_name.gsub(/_id$/,'').to_sym).class_name.constantize
67
+ else
68
+ klass = audit.auditable.class.reflect_on_association(audit.field_name.gsub(/_id$/,'').to_sym).class_name.constantize
69
+ end
70
+ value = klass.where(:id => value).first
71
+ return 'Unknown' unless value
72
+ return value.to_label
73
+ end
74
+
75
+ end
@@ -0,0 +1,32 @@
1
+ <h2>Audit Log</h2>
2
+
3
+ <%%= link_to "back to #{@resource.class}", @resource %>
4
+
5
+ <table>
6
+ <thead>
7
+ <tr>
8
+ <th>Field Name</th>
9
+ <th>Values</th>
10
+ <th>Created</th>
11
+ <th>Rollback</th>
12
+ </tr>
13
+ </thead>
14
+ <tbody>
15
+ <%% @audits.each do |audit| %>
16
+ <tr>
17
+ <td>
18
+ <%%= audit.field_name.humanize if audit.field_name %>
19
+ </td>
20
+ <td>
21
+ <%%= description(audit) %>
22
+ </td>
23
+ <td>
24
+ <%%= time_ago_in_words(audit.created_at) %> ago
25
+ </td>
26
+ <td>
27
+ <%%= link_to 'rollback', audit_rollback_path(audit) if audit.reversable? %>
28
+ </td>
29
+ </tr>
30
+ <%% end %>
31
+ </tbody>
32
+ </table>
@@ -0,0 +1,4 @@
1
+ Rails.application.configure do
2
+ # Uncomment this to turn on verbose mode
3
+ config.my_gem.verbose = true
4
+ end
@@ -0,0 +1,24 @@
1
+ class CreateAudits < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :audits do |t|
4
+ t.string :action
5
+ t.string :auditable_type
6
+ t.integer :auditable_id
7
+ t.string :association_type
8
+ t.integer :association_id
9
+ t.string :field_name
10
+ t.string :old_value
11
+ t.string :new_value
12
+ # t.integer :user_id
13
+ t.boolean :undoable, :default => true
14
+ t.timestamps
15
+ end
16
+
17
+ add_index :audits, [:auditable_type, :auditable_id]
18
+ add_index :audits, [:association_type, :association_id]
19
+ end
20
+
21
+ def self.down
22
+ drop_table :audits
23
+ end
24
+ end
metadata CHANGED
@@ -1,13 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auditing
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
5
- prerelease: false
6
- segments:
7
- - 1
8
- - 2
9
- - 0
10
- version: 1.2.0
4
+ prerelease:
5
+ version: 1.2.2
11
6
  platform: ruby
12
7
  authors:
13
8
  - Brad Cantin
@@ -15,78 +10,33 @@ autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
12
 
18
- date: 2011-02-01 00:00:00 -05:00
13
+ date: 2011-02-05 00:00:00 -05:00
19
14
  default_executable:
20
15
  dependencies:
21
16
  - !ruby/object:Gem::Dependency
22
- type: :development
23
- prerelease: false
24
- name: activerecord
25
- version_requirements: &id001 !ruby/object:Gem::Requirement
26
- none: false
27
- requirements:
28
- - - ">="
29
- - !ruby/object:Gem::Version
30
- hash: 3
31
- segments:
32
- - 0
33
- version: "0"
34
- requirement: *id001
35
- - !ruby/object:Gem::Dependency
36
- type: :development
37
- prerelease: false
38
- name: sqlite3-ruby
39
- version_requirements: &id002 !ruby/object:Gem::Requirement
40
- none: false
41
- requirements:
42
- - - ">="
43
- - !ruby/object:Gem::Version
44
- hash: 3
45
- segments:
46
- - 0
47
- version: "0"
48
- requirement: *id002
49
- - !ruby/object:Gem::Dependency
50
- type: :development
51
- prerelease: false
52
17
  name: rspec
53
- version_requirements: &id003 !ruby/object:Gem::Requirement
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
54
20
  none: false
55
21
  requirements:
56
22
  - - ">="
57
23
  - !ruby/object:Gem::Version
58
- hash: 3
59
- segments:
60
- - 0
61
24
  version: "0"
62
- requirement: *id003
63
- - !ruby/object:Gem::Dependency
64
25
  type: :development
65
- prerelease: false
66
- name: rspec
67
- version_requirements: &id004 !ruby/object:Gem::Requirement
68
- none: false
69
- requirements:
70
- - - ">="
71
- - !ruby/object:Gem::Version
72
- hash: 3
73
- segments:
74
- - 2
75
- - 0
76
- version: "2.0"
77
- requirement: *id004
26
+ version_requirements: *id001
78
27
  description: acts_as_versioned is good. This allows an attribute level rollback instead
79
- email: brad.cantin@gmail.com
28
+ email:
29
+ - brad.cantin@gmail.com
80
30
  executables: []
81
31
 
82
32
  extensions: []
83
33
 
84
- extra_rdoc_files:
85
- - LICENSE
86
- - README.rdoc
34
+ extra_rdoc_files: []
35
+
87
36
  files:
88
37
  - .bundle/config
89
38
  - .document
39
+ - .gitignore
90
40
  - .rspec
91
41
  - .rvmrc
92
42
  - Gemfile
@@ -94,12 +44,20 @@ files:
94
44
  - LICENSE
95
45
  - README.rdoc
96
46
  - Rakefile
97
- - VERSION
98
47
  - auditing.gemspec
99
48
  - lib/auditing.rb
100
49
  - lib/auditing/audit_relationship.rb
101
50
  - lib/auditing/auditor.rb
102
51
  - lib/auditing/base.rb
52
+ - lib/auditing/version.rb
53
+ - lib/generators/auditing/USAGE
54
+ - lib/generators/auditing/install_generator.rb
55
+ - lib/generators/auditing/templates/audit.rb
56
+ - lib/generators/auditing/templates/audits_controller.rb
57
+ - lib/generators/auditing/templates/audits_helper.rb
58
+ - lib/generators/auditing/templates/index.html.erb
59
+ - lib/generators/auditing/templates/initializer.rb
60
+ - lib/generators/auditing/templates/migration.rb
103
61
  - spec/auditing/audit_relationship_spec.rb
104
62
  - spec/auditing/auditor_spec.rb
105
63
  - spec/auditing/base_spec.rb
@@ -107,7 +65,7 @@ files:
107
65
  - spec/schema.rb
108
66
  - spec/spec_helper.rb
109
67
  has_rdoc: true
110
- homepage: http://github.com/bcantin/auditing
68
+ homepage: https://github.com/bcantin/auditing
111
69
  licenses: []
112
70
 
113
71
  post_install_message:
@@ -120,23 +78,17 @@ required_ruby_version: !ruby/object:Gem::Requirement
120
78
  requirements:
121
79
  - - ">="
122
80
  - !ruby/object:Gem::Version
123
- hash: 3
124
- segments:
125
- - 0
126
81
  version: "0"
127
82
  required_rubygems_version: !ruby/object:Gem::Requirement
128
83
  none: false
129
84
  requirements:
130
85
  - - ">="
131
86
  - !ruby/object:Gem::Version
132
- hash: 3
133
- segments:
134
- - 0
135
87
  version: "0"
136
88
  requirements: []
137
89
 
138
- rubyforge_project:
139
- rubygems_version: 1.3.7
90
+ rubyforge_project: auditing
91
+ rubygems_version: 1.5.0
140
92
  signing_key:
141
93
  specification_version: 3
142
94
  summary: A gem to keep track of audit hisory of a record
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.2.0