audit_trail 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. data/.document +5 -0
  2. data/Gemfile +9 -0
  3. data/Gemfile.lock +99 -0
  4. data/LICENSE.txt +20 -0
  5. data/README +0 -0
  6. data/README.md +19 -0
  7. data/Rakefile +36 -0
  8. data/VERSION +1 -0
  9. data/app/models/change_event.rb +3 -0
  10. data/audit_trail.gemspec +110 -0
  11. data/lib/audit_trail.rb +11 -0
  12. data/lib/audit_trail/change_tracking.rb +64 -0
  13. data/lib/audit_trail/engine.rb +4 -0
  14. data/lib/audit_trail/model.rb +31 -0
  15. data/lib/generators/audit_trail/install_generator.rb +18 -0
  16. data/lib/generators/audit_trail/templates/migration.rb +16 -0
  17. data/test/audit_trail_test.rb +5 -0
  18. data/test/change_tracking_test.rb +242 -0
  19. data/test/test_app_3_1/Gemfile +8 -0
  20. data/test/test_app_3_1/Gemfile.lock +102 -0
  21. data/test/test_app_3_1/README +261 -0
  22. data/test/test_app_3_1/Rakefile +7 -0
  23. data/test/test_app_3_1/app/assets/images/rails.png +0 -0
  24. data/test/test_app_3_1/app/assets/javascripts/application.js +9 -0
  25. data/test/test_app_3_1/app/assets/stylesheets/application.css +7 -0
  26. data/test/test_app_3_1/app/controllers/application_controller.rb +3 -0
  27. data/test/test_app_3_1/app/helpers/application_helper.rb +2 -0
  28. data/test/test_app_3_1/app/views/layouts/application.html.erb +14 -0
  29. data/test/test_app_3_1/config.ru +4 -0
  30. data/test/test_app_3_1/config/application.rb +48 -0
  31. data/test/test_app_3_1/config/boot.rb +6 -0
  32. data/test/test_app_3_1/config/database.yml +42 -0
  33. data/test/test_app_3_1/config/environment.rb +5 -0
  34. data/test/test_app_3_1/config/environments/development.rb +30 -0
  35. data/test/test_app_3_1/config/environments/production.rb +60 -0
  36. data/test/test_app_3_1/config/environments/test.rb +42 -0
  37. data/test/test_app_3_1/config/initializers/backtrace_silencers.rb +7 -0
  38. data/test/test_app_3_1/config/initializers/inflections.rb +10 -0
  39. data/test/test_app_3_1/config/initializers/mime_types.rb +5 -0
  40. data/test/test_app_3_1/config/initializers/secret_token.rb +7 -0
  41. data/test/test_app_3_1/config/initializers/session_store.rb +8 -0
  42. data/test/test_app_3_1/config/initializers/wrap_parameters.rb +14 -0
  43. data/test/test_app_3_1/config/locales/en.yml +5 -0
  44. data/test/test_app_3_1/config/routes.rb +58 -0
  45. data/test/test_app_3_1/db/schema.rb +15 -0
  46. data/test/test_app_3_1/db/seeds.rb +7 -0
  47. data/test/test_app_3_1/log/development.log +3 -0
  48. data/test/test_app_3_1/log/test.log +17791 -0
  49. data/test/test_app_3_1/public/404.html +26 -0
  50. data/test/test_app_3_1/public/422.html +26 -0
  51. data/test/test_app_3_1/public/500.html +26 -0
  52. data/test/test_app_3_1/public/favicon.ico +0 -0
  53. data/test/test_app_3_1/public/index.html +241 -0
  54. data/test/test_app_3_1/public/robots.txt +5 -0
  55. data/test/test_app_3_1/script/rails +6 -0
  56. data/test/test_app_3_1/test/test_helper.rb +13 -0
  57. data/test/test_app_3_1/tmp/cache/4B5/180/experiments +0 -0
  58. data/test/test_helper.rb +66 -0
  59. metadata +198 -0
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+ gem 'rails', "~> 3.1"
3
+
4
+ group :development do
5
+ gem 'mysql2'
6
+ gem 'mocha'
7
+ gem "bundler", "~> 1.0.0"
8
+ gem "jeweler", "~> 1.8.4"
9
+ end
@@ -0,0 +1,99 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ actionmailer (3.2.6)
5
+ actionpack (= 3.2.6)
6
+ mail (~> 2.4.4)
7
+ actionpack (3.2.6)
8
+ activemodel (= 3.2.6)
9
+ activesupport (= 3.2.6)
10
+ builder (~> 3.0.0)
11
+ erubis (~> 2.7.0)
12
+ journey (~> 1.0.1)
13
+ rack (~> 1.4.0)
14
+ rack-cache (~> 1.2)
15
+ rack-test (~> 0.6.1)
16
+ sprockets (~> 2.1.3)
17
+ activemodel (3.2.6)
18
+ activesupport (= 3.2.6)
19
+ builder (~> 3.0.0)
20
+ activerecord (3.2.6)
21
+ activemodel (= 3.2.6)
22
+ activesupport (= 3.2.6)
23
+ arel (~> 3.0.2)
24
+ tzinfo (~> 0.3.29)
25
+ activeresource (3.2.6)
26
+ activemodel (= 3.2.6)
27
+ activesupport (= 3.2.6)
28
+ activesupport (3.2.6)
29
+ i18n (~> 0.6)
30
+ multi_json (~> 1.0)
31
+ arel (3.0.2)
32
+ builder (3.0.0)
33
+ erubis (2.7.0)
34
+ git (1.2.5)
35
+ hike (1.2.1)
36
+ i18n (0.6.0)
37
+ jeweler (1.8.4)
38
+ bundler (~> 1.0)
39
+ git (>= 1.2.5)
40
+ rake
41
+ rdoc
42
+ journey (1.0.4)
43
+ json (1.7.3)
44
+ mail (2.4.4)
45
+ i18n (>= 0.4.0)
46
+ mime-types (~> 1.16)
47
+ treetop (~> 1.4.8)
48
+ metaclass (0.0.1)
49
+ mime-types (1.19)
50
+ mocha (0.11.3)
51
+ metaclass (~> 0.0.1)
52
+ multi_json (1.3.6)
53
+ mysql2 (0.3.11)
54
+ polyglot (0.3.3)
55
+ rack (1.4.1)
56
+ rack-cache (1.2)
57
+ rack (>= 0.4)
58
+ rack-ssl (1.3.2)
59
+ rack
60
+ rack-test (0.6.1)
61
+ rack (>= 1.0)
62
+ rails (3.2.6)
63
+ actionmailer (= 3.2.6)
64
+ actionpack (= 3.2.6)
65
+ activerecord (= 3.2.6)
66
+ activeresource (= 3.2.6)
67
+ activesupport (= 3.2.6)
68
+ bundler (~> 1.0)
69
+ railties (= 3.2.6)
70
+ railties (3.2.6)
71
+ actionpack (= 3.2.6)
72
+ activesupport (= 3.2.6)
73
+ rack-ssl (~> 1.3.2)
74
+ rake (>= 0.8.7)
75
+ rdoc (~> 3.4)
76
+ thor (>= 0.14.6, < 2.0)
77
+ rake (0.9.2.2)
78
+ rdoc (3.12)
79
+ json (~> 1.4)
80
+ sprockets (2.1.3)
81
+ hike (~> 1.2)
82
+ rack (~> 1.0)
83
+ tilt (~> 1.1, != 1.3.0)
84
+ thor (0.15.4)
85
+ tilt (1.3.3)
86
+ treetop (1.4.10)
87
+ polyglot
88
+ polyglot (>= 0.3.1)
89
+ tzinfo (0.3.33)
90
+
91
+ PLATFORMS
92
+ ruby
93
+
94
+ DEPENDENCIES
95
+ bundler (~> 1.0.0)
96
+ jeweler (~> 1.8.4)
97
+ mocha
98
+ mysql2
99
+ rails (~> 3.1)
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Tushar Ranka
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 ADDED
File without changes
@@ -0,0 +1,19 @@
1
+ = audit_trail
2
+
3
+ audit_trail is an easy to use library to track changes on an attribute level to ActiveRecord objects. It keeps tracks of the changes in database type-sensitive manner in order to allow
4
+
5
+ == Contributing to audit_trail
6
+
7
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
+ * Fork the project.
10
+ * Start a feature/bugfix branch.
11
+ * Commit and push until you are happy with your contribution.
12
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
+ * 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.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2012 Tushar Ranka. See LICENSE.txt for
18
+ further details.
19
+
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+
3
+ require 'rubygems'
4
+ require 'bundler'
5
+ begin
6
+ Bundler.setup(:default, :development)
7
+ rescue Bundler::BundlerError => e
8
+ $stderr.puts e.message
9
+ $stderr.puts "Run `bundle install` to install missing gems"
10
+ exit e.status_code
11
+ end
12
+ require 'rake'
13
+
14
+ require 'jeweler'
15
+ Jeweler::Tasks.new do |gem|
16
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
17
+ gem.name = "audit_trail"
18
+ gem.homepage = "http://github.com/tusharr/audit_trail"
19
+ gem.license = "MIT"
20
+ gem.summary = %Q{track changes to active record objects on an attribute level}
21
+ gem.description = %Q{tracks_changes provides a way to easily keep track of changes to ActiveRecord models at an attribute level and maintains the typecasting in the database.}
22
+ gem.email = "tusharranka@gmail.com"
23
+ gem.authors = ["Tushar Ranka"]
24
+ # dependencies defined in Gemfile
25
+ end
26
+ Jeweler::RubygemsDotOrgTasks.new
27
+
28
+ require 'rake/testtask'
29
+ Rake::TestTask.new(:test) do |test|
30
+ test.libs << 'lib' << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+
35
+ task :default => :test
36
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,3 @@
1
+ class ChangeEvent < ActiveRecord::Base
2
+ include AuditTrail::Model
3
+ end
@@ -0,0 +1,110 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "audit_trail"
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Tushar Ranka"]
12
+ s.date = "2012-07-24"
13
+ s.description = "tracks_changes provides a way to easily keep track of changes to ActiveRecord models at an attribute level and maintains the typecasting in the database."
14
+ s.email = "tusharranka@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README",
18
+ "README.md"
19
+ ]
20
+ s.files = [
21
+ ".document",
22
+ "Gemfile",
23
+ "Gemfile.lock",
24
+ "LICENSE.txt",
25
+ "README",
26
+ "README.md",
27
+ "Rakefile",
28
+ "VERSION",
29
+ "app/models/change_event.rb",
30
+ "audit_trail.gemspec",
31
+ "lib/audit_trail.rb",
32
+ "lib/audit_trail/change_tracking.rb",
33
+ "lib/audit_trail/engine.rb",
34
+ "lib/audit_trail/model.rb",
35
+ "lib/generators/audit_trail/install_generator.rb",
36
+ "lib/generators/audit_trail/templates/migration.rb",
37
+ "test/audit_trail_test.rb",
38
+ "test/change_tracking_test.rb",
39
+ "test/test_app_3_1/Gemfile",
40
+ "test/test_app_3_1/Gemfile.lock",
41
+ "test/test_app_3_1/README",
42
+ "test/test_app_3_1/Rakefile",
43
+ "test/test_app_3_1/app/assets/images/rails.png",
44
+ "test/test_app_3_1/app/assets/javascripts/application.js",
45
+ "test/test_app_3_1/app/assets/stylesheets/application.css",
46
+ "test/test_app_3_1/app/controllers/application_controller.rb",
47
+ "test/test_app_3_1/app/helpers/application_helper.rb",
48
+ "test/test_app_3_1/app/views/layouts/application.html.erb",
49
+ "test/test_app_3_1/config.ru",
50
+ "test/test_app_3_1/config/application.rb",
51
+ "test/test_app_3_1/config/boot.rb",
52
+ "test/test_app_3_1/config/database.yml",
53
+ "test/test_app_3_1/config/environment.rb",
54
+ "test/test_app_3_1/config/environments/development.rb",
55
+ "test/test_app_3_1/config/environments/production.rb",
56
+ "test/test_app_3_1/config/environments/test.rb",
57
+ "test/test_app_3_1/config/initializers/backtrace_silencers.rb",
58
+ "test/test_app_3_1/config/initializers/inflections.rb",
59
+ "test/test_app_3_1/config/initializers/mime_types.rb",
60
+ "test/test_app_3_1/config/initializers/secret_token.rb",
61
+ "test/test_app_3_1/config/initializers/session_store.rb",
62
+ "test/test_app_3_1/config/initializers/wrap_parameters.rb",
63
+ "test/test_app_3_1/config/locales/en.yml",
64
+ "test/test_app_3_1/config/routes.rb",
65
+ "test/test_app_3_1/db/schema.rb",
66
+ "test/test_app_3_1/db/seeds.rb",
67
+ "test/test_app_3_1/log/development.log",
68
+ "test/test_app_3_1/log/test.log",
69
+ "test/test_app_3_1/public/404.html",
70
+ "test/test_app_3_1/public/422.html",
71
+ "test/test_app_3_1/public/500.html",
72
+ "test/test_app_3_1/public/favicon.ico",
73
+ "test/test_app_3_1/public/index.html",
74
+ "test/test_app_3_1/public/robots.txt",
75
+ "test/test_app_3_1/script/rails",
76
+ "test/test_app_3_1/test/test_helper.rb",
77
+ "test/test_app_3_1/tmp/cache/4B5/180/experiments",
78
+ "test/test_helper.rb"
79
+ ]
80
+ s.homepage = "http://github.com/tusharr/audit_trail"
81
+ s.licenses = ["MIT"]
82
+ s.require_paths = ["lib"]
83
+ s.rubygems_version = "1.8.12"
84
+ s.summary = "track changes to active record objects on an attribute level"
85
+
86
+ if s.respond_to? :specification_version then
87
+ s.specification_version = 3
88
+
89
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
90
+ s.add_runtime_dependency(%q<rails>, ["~> 3.1"])
91
+ s.add_development_dependency(%q<mysql2>, [">= 0"])
92
+ s.add_development_dependency(%q<mocha>, [">= 0"])
93
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
94
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
95
+ else
96
+ s.add_dependency(%q<rails>, ["~> 3.1"])
97
+ s.add_dependency(%q<mysql2>, [">= 0"])
98
+ s.add_dependency(%q<mocha>, [">= 0"])
99
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
100
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
101
+ end
102
+ else
103
+ s.add_dependency(%q<rails>, ["~> 3.1"])
104
+ s.add_dependency(%q<mysql2>, [">= 0"])
105
+ s.add_dependency(%q<mocha>, [">= 0"])
106
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
107
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
108
+ end
109
+ end
110
+
@@ -0,0 +1,11 @@
1
+ require 'rails'
2
+ require 'active_support/dependencies'
3
+ require 'audit_trail/engine'
4
+
5
+ module AuditTrail
6
+ extend ActiveSupport::Autoload
7
+ autoload :Model, 'audit_trail/model'
8
+ autoload :ChangeTracking, 'audit_trail/change_tracking'
9
+ end
10
+
11
+ ActiveRecord::Base.send :include, AuditTrail::ChangeTracking
@@ -0,0 +1,64 @@
1
+ module AuditTrail
2
+ module ChangeTracking
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ class_attribute :audit_trail_types
7
+ self.audit_trail_types = (self.audit_trail_types ? self.audit_trail_types.dup : {})
8
+ end
9
+
10
+ module ClassMethods
11
+ def audit_trail_for(*attrs)
12
+ options = attrs.extract_options!
13
+ callback_event = options.delete(:on) || :save
14
+ additional_info_method = options.delete(:additional_info)
15
+ if_method = options.delete(:if)
16
+
17
+ attrs.each do |attr|
18
+ callback_method_name = :"record_change_event_for_#{attr}"
19
+ tracked_column_type = self.columns.find { |column| column.name.to_s == attr.to_s }.type
20
+ self.audit_trail_types[attr.to_s] = tracked_column_type
21
+
22
+ if additional_info_method
23
+ define_method :"#{attr}_change_additional_info" do
24
+ if additional_info_method.is_a?(Symbol)
25
+ send(additional_info_method)
26
+ elsif additional_info_method.respond_to?(:call)
27
+ additional_info_method.call(self)
28
+ end
29
+ end
30
+ end
31
+
32
+ define_method callback_method_name do
33
+ return unless send(:"#{attr}_changed?")
34
+ change = send(:"#{attr}_change")
35
+
36
+ change_event_attrs = {
37
+ :changed_object => self,
38
+ :changed_attribute => attr,
39
+ :changed_by_id => ChangeEvent.changer.try(:id),
40
+ :changer_ip_address => ChangeEvent.changer_ip_address,
41
+
42
+ }
43
+
44
+ change_event_attrs[:additional_info] = send(:"#{attr}_change_additional_info") if additional_info_method
45
+ change_event_attrs["previous_#{tracked_column_type}_value"] = change.first
46
+ change_event_attrs["#{tracked_column_type}_value"] = change.last
47
+ change_event = ChangeEvent.new(change_event_attrs)
48
+ change_event.save!
49
+ end
50
+
51
+ send(:"after_#{callback_event}", callback_method_name, :if => if_method)
52
+
53
+ has_many :"#{attr}_change_events",
54
+ :dependent => :destroy,
55
+ :class_name => "ChangeEvent",
56
+ :as => :changed_object,
57
+ :order => "change_events.created_at DESC, change_events.id DESC",
58
+ :conditions => "change_events.changed_attribute = '#{attr}'"
59
+
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,4 @@
1
+ module AuditTrail
2
+ class Engine < Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,31 @@
1
+ module AuditTrail
2
+ module Model
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ belongs_to :changed_object, :polymorphic => true
7
+ # belongs_to :created_by, :class_name => 'User'
8
+ validates_presence_of :changed_attribute, :changed_object
9
+ end
10
+
11
+ module ClassMethods
12
+ def changer
13
+ end
14
+
15
+ def changer_ip_address
16
+ end
17
+ end
18
+
19
+ def value
20
+ send("#{change_type}_value")
21
+ end
22
+
23
+ def previous_value
24
+ send("previous_#{change_type}_value")
25
+ end
26
+
27
+ def change_type
28
+ @change_type ||= changed_object.class.audit_trail_types[changed_attribute.to_s]
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,18 @@
1
+ require 'rails/generators/active_record'
2
+
3
+ module AuditTrail
4
+ module Generators
5
+ class InstallGenerator < Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+ extend ActiveRecord::Generators::Migration
8
+
9
+ source_root File.expand_path("../templates", __FILE__)
10
+
11
+ desc "Creates the migration file required for audit_trail"
12
+
13
+ def copy_migrations
14
+ migration_template "migration.rb", "db/migrate/create_audit_trail"
15
+ end
16
+ end
17
+ end
18
+ end