henko 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. data/.gitignore +8 -0
  2. data/.rspec +2 -0
  3. data/Gemfile +17 -0
  4. data/Gemfile.lock +163 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +3 -0
  7. data/Rakefile +27 -0
  8. data/app/assets/images/change_logger/.gitkeep +0 -0
  9. data/app/assets/javascripts/change_logger/application.js +15 -0
  10. data/app/assets/stylesheets/change_logger/application.css +13 -0
  11. data/app/controllers/henko/application_controller.rb +4 -0
  12. data/app/helpers/henko/application_helper.rb +4 -0
  13. data/app/models/henko/audit_log.rb +17 -0
  14. data/app/views/layouts/change_logger/application.html.erb +14 -0
  15. data/change_logger.gemspec +35 -0
  16. data/config/routes.rb +2 -0
  17. data/lib/henko.rb +11 -0
  18. data/lib/henko/active_record_extensions.rb +21 -0
  19. data/lib/henko/engine.rb +12 -0
  20. data/lib/henko/log_manager.rb +36 -0
  21. data/lib/henko/railtie.rb +9 -0
  22. data/lib/henko/version.rb +3 -0
  23. data/lib/tasks/change_logger_tasks.rake +5 -0
  24. data/script/rails +8 -0
  25. data/spec/dummy/README.rdoc +261 -0
  26. data/spec/dummy/Rakefile +7 -0
  27. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  28. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  29. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  30. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  31. data/spec/dummy/app/mailers/.gitkeep +0 -0
  32. data/spec/dummy/app/models/.gitkeep +0 -0
  33. data/spec/dummy/app/models/user.rb +3 -0
  34. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  35. data/spec/dummy/config.ru +4 -0
  36. data/spec/dummy/config/application.rb +66 -0
  37. data/spec/dummy/config/boot.rb +10 -0
  38. data/spec/dummy/config/database.yml +25 -0
  39. data/spec/dummy/config/environment.rb +5 -0
  40. data/spec/dummy/config/environments/development.rb +37 -0
  41. data/spec/dummy/config/environments/production.rb +67 -0
  42. data/spec/dummy/config/environments/test.rb +37 -0
  43. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  44. data/spec/dummy/config/initializers/inflections.rb +15 -0
  45. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  46. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  47. data/spec/dummy/config/initializers/session_store.rb +8 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/spec/dummy/config/locales/en.yml +5 -0
  50. data/spec/dummy/config/mongoid.yml +20 -0
  51. data/spec/dummy/config/routes.rb +3 -0
  52. data/spec/dummy/db/migrate/20120812192635_create_users.rb +11 -0
  53. data/spec/dummy/db/schema.rb +24 -0
  54. data/spec/dummy/lib/assets/.gitkeep +0 -0
  55. data/spec/dummy/log/.gitkeep +0 -0
  56. data/spec/dummy/public/404.html +26 -0
  57. data/spec/dummy/public/422.html +26 -0
  58. data/spec/dummy/public/500.html +25 -0
  59. data/spec/dummy/public/favicon.ico +0 -0
  60. data/spec/dummy/script/rails +6 -0
  61. data/spec/fabricators/change_logger_audit_log_fabricator.rb +2 -0
  62. data/spec/lib/henko/active_record_extensions_spec.rb +25 -0
  63. data/spec/lib/henko/log_manager_spec.rb +49 -0
  64. data/spec/models/change_logger/audit_log_spec.rb +12 -0
  65. data/spec/spec_helper.rb +65 -0
  66. metadata +365 -0
@@ -0,0 +1,8 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/db/*.sqlite3
5
+ spec/dummy/log/*.log
6
+ spec/dummy/tmp/
7
+ spec/dummy/.sass-cache
8
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format doc
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Declare your gem's dependencies in henko.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # jquery-rails is used by the dummy application
9
+ gem "jquery-rails"
10
+
11
+ # Declare any dependencies that are still in development here instead of in
12
+ # your gemspec. These might include edge Rails or gems from your path or
13
+ # Git. Remember to move these dependencies to your gemspec before releasing
14
+ # your gem to rubygems.org.
15
+
16
+ # To use debugger
17
+ # gem 'debugger'
@@ -0,0 +1,163 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ henko (1.0.0)
5
+ bson_ext (~> 1.5)
6
+ mongoid (~> 2.4)
7
+ rails (~> 3.2.6)
8
+
9
+ GEM
10
+ remote: http://rubygems.org/
11
+ specs:
12
+ actionmailer (3.2.8)
13
+ actionpack (= 3.2.8)
14
+ mail (~> 2.4.4)
15
+ actionpack (3.2.8)
16
+ activemodel (= 3.2.8)
17
+ activesupport (= 3.2.8)
18
+ builder (~> 3.0.0)
19
+ erubis (~> 2.7.0)
20
+ journey (~> 1.0.4)
21
+ rack (~> 1.4.0)
22
+ rack-cache (~> 1.2)
23
+ rack-test (~> 0.6.1)
24
+ sprockets (~> 2.1.3)
25
+ activemodel (3.2.8)
26
+ activesupport (= 3.2.8)
27
+ builder (~> 3.0.0)
28
+ activerecord (3.2.8)
29
+ activemodel (= 3.2.8)
30
+ activesupport (= 3.2.8)
31
+ arel (~> 3.0.2)
32
+ tzinfo (~> 0.3.29)
33
+ activeresource (3.2.8)
34
+ activemodel (= 3.2.8)
35
+ activesupport (= 3.2.8)
36
+ activesupport (3.2.8)
37
+ i18n (~> 0.6)
38
+ multi_json (~> 1.0)
39
+ arel (3.0.2)
40
+ bson (1.6.4)
41
+ bson_ext (1.6.4)
42
+ bson (~> 1.6.4)
43
+ builder (3.0.0)
44
+ diff-lcs (1.1.3)
45
+ erubis (2.7.0)
46
+ fabrication (2.2.2)
47
+ faker (1.0.1)
48
+ i18n (~> 0.4)
49
+ fakeweb (1.3.0)
50
+ ffi (1.1.5)
51
+ guard (1.3.0)
52
+ listen (>= 0.4.2)
53
+ thor (>= 0.14.6)
54
+ guard-rspec (1.2.1)
55
+ guard (>= 1.1)
56
+ guard-spork (1.1.0)
57
+ guard (>= 1.1)
58
+ spork (>= 0.8.4)
59
+ hike (1.2.1)
60
+ i18n (0.6.0)
61
+ journey (1.0.4)
62
+ jquery-rails (2.0.2)
63
+ railties (>= 3.2.0, < 5.0)
64
+ thor (~> 0.14)
65
+ json (1.7.4)
66
+ listen (0.4.7)
67
+ rb-fchange (~> 0.0.5)
68
+ rb-fsevent (~> 0.9.1)
69
+ rb-inotify (~> 0.8.8)
70
+ mail (2.4.4)
71
+ i18n (>= 0.4.0)
72
+ mime-types (~> 1.16)
73
+ treetop (~> 1.4.8)
74
+ mime-types (1.19)
75
+ mongo (1.6.2)
76
+ bson (~> 1.6.2)
77
+ mongoid (2.4.12)
78
+ activemodel (~> 3.1)
79
+ mongo (<= 1.6.2)
80
+ tzinfo (~> 0.3.22)
81
+ mongoid-rspec (1.4.5)
82
+ mongoid (>= 2.4.6)
83
+ rake
84
+ rspec (>= 2.9)
85
+ multi_json (1.3.6)
86
+ polyglot (0.3.3)
87
+ rack (1.4.1)
88
+ rack-cache (1.2)
89
+ rack (>= 0.4)
90
+ rack-ssl (1.3.2)
91
+ rack
92
+ rack-test (0.6.1)
93
+ rack (>= 1.0)
94
+ rails (3.2.8)
95
+ actionmailer (= 3.2.8)
96
+ actionpack (= 3.2.8)
97
+ activerecord (= 3.2.8)
98
+ activeresource (= 3.2.8)
99
+ activesupport (= 3.2.8)
100
+ bundler (~> 1.0)
101
+ railties (= 3.2.8)
102
+ railties (3.2.8)
103
+ actionpack (= 3.2.8)
104
+ activesupport (= 3.2.8)
105
+ rack-ssl (~> 1.3.2)
106
+ rake (>= 0.8.7)
107
+ rdoc (~> 3.4)
108
+ thor (>= 0.14.6, < 2.0)
109
+ rake (0.9.2.2)
110
+ rb-fchange (0.0.5)
111
+ ffi
112
+ rb-fsevent (0.9.1)
113
+ rb-inotify (0.8.8)
114
+ ffi (>= 0.5.0)
115
+ rdoc (3.12)
116
+ json (~> 1.4)
117
+ rspec (2.11.0)
118
+ rspec-core (~> 2.11.0)
119
+ rspec-expectations (~> 2.11.0)
120
+ rspec-mocks (~> 2.11.0)
121
+ rspec-core (2.11.1)
122
+ rspec-expectations (2.11.2)
123
+ diff-lcs (~> 1.1.3)
124
+ rspec-mocks (2.11.2)
125
+ rspec-rails (2.11.0)
126
+ actionpack (>= 3.0)
127
+ activesupport (>= 3.0)
128
+ railties (>= 3.0)
129
+ rspec (~> 2.11.0)
130
+ simplecov (0.6.4)
131
+ multi_json (~> 1.0)
132
+ simplecov-html (~> 0.5.3)
133
+ simplecov-html (0.5.3)
134
+ spork (0.9.2)
135
+ sprockets (2.1.3)
136
+ hike (~> 1.2)
137
+ rack (~> 1.0)
138
+ tilt (~> 1.1, != 1.3.0)
139
+ sqlite3 (1.3.6)
140
+ thor (0.15.4)
141
+ tilt (1.3.3)
142
+ treetop (1.4.10)
143
+ polyglot
144
+ polyglot (>= 0.3.1)
145
+ tzinfo (0.3.33)
146
+ vcr (2.2.4)
147
+
148
+ PLATFORMS
149
+ ruby
150
+
151
+ DEPENDENCIES
152
+ fabrication
153
+ faker
154
+ fakeweb
155
+ guard-rspec
156
+ guard-spork
157
+ henko!
158
+ jquery-rails
159
+ mongoid-rspec
160
+ rspec-rails
161
+ simplecov
162
+ sqlite3
163
+ vcr
@@ -0,0 +1,20 @@
1
+ Copyright 2012 Avi Tzurel
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.
@@ -0,0 +1,3 @@
1
+ = Henko
2
+
3
+ This project rocks and uses MIT-LICENSE.
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Henko'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module Henko
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Henko
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,17 @@
1
+ module Henko
2
+ class AuditLog
3
+ include Mongoid::Document
4
+
5
+ field :action, type: String
6
+ field :action_url, type:String
7
+ field :user_id, type: String
8
+ field :class_name, type: String
9
+ field :user_ip, type: String
10
+
11
+ field :data, type: String
12
+
13
+ index :user_id, sparse: true
14
+ index :action
15
+ index :class_name
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Henko</title>
5
+ <%= stylesheet_link_tag "henko/application", :media => "all" %>
6
+ <%= javascript_include_tag "henko/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,35 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "henko/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "henko"
9
+ s.version = Henko::VERSION
10
+ s.authors = ["Avi Tzurel"]
11
+ s.email = ["avi@kensodev.com"]
12
+ s.homepage = "http://www.avi.io"
13
+ s.summary = "Log your AR model changes into MongoDB"
14
+ s.description = "Log your AR model changes into MongoDB"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "rails", "~> 3.2.6"
22
+ s.add_dependency "mongoid", "~> 2.4"
23
+ s.add_dependency "bson_ext", "~> 1.5"
24
+
25
+ s.add_development_dependency "sqlite3"
26
+ s.add_development_dependency "fabrication"
27
+ s.add_development_dependency "rspec-rails"
28
+ s.add_development_dependency "faker"
29
+ s.add_development_dependency 'simplecov'
30
+ s.add_development_dependency 'guard-spork'
31
+ s.add_development_dependency 'guard-rspec'
32
+ s.add_development_dependency 'mongoid-rspec'
33
+ s.add_development_dependency 'fakeweb'
34
+ s.add_development_dependency 'vcr'
35
+ end
@@ -0,0 +1,2 @@
1
+ Henko::Engine.routes.draw do
2
+ end
@@ -0,0 +1,11 @@
1
+ require "henko/engine"
2
+ require 'henko'
3
+
4
+ module Henko
5
+ end
6
+
7
+ require 'henko/active_record_extensions'
8
+ require 'henko/railtie'
9
+ require 'henko/log_manager'
10
+ require 'rails'
11
+ require 'mongoid'
@@ -0,0 +1,21 @@
1
+ require 'rails'
2
+
3
+ module Henko
4
+ require 'henko/railtie' if defined?(Rails)
5
+
6
+ module ActiveRecordExtensions
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ after_create do
11
+ Henko::LogManager.audit_model({ record: self, action: 'create' })
12
+ end
13
+ after_update do
14
+ Henko::LogManager.audit_model( {record: self, action: 'update' })
15
+ end
16
+ after_destroy do
17
+ Henko::LogManager.audit_model({ record: self, action: 'destroy' })
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,12 @@
1
+ module Henko
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Henko
4
+
5
+ config.generators do |g|
6
+ g.test_framework :rspec
7
+ g.integration_tool :rspec
8
+ g.orm :mongoid
9
+ g.fixture_replacement :fabrication
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,36 @@
1
+ module Henko
2
+ class LogManager
3
+ cattr_accessor :user_id, :user_ip, :action_url
4
+
5
+ def self.audit_model(options = {})
6
+ record = options[:record]
7
+ action = options[:action]
8
+
9
+ return unless record && options
10
+
11
+ changes = record.changes
12
+ current_user_id = Henko::LogManager.user_id
13
+
14
+ audit_log = Henko::AuditLog.new
15
+ audit_log.class_name = record.class.name
16
+ audit_log.user_id = current_user_id if current_user_id
17
+ audit_log.action = options[:action]
18
+ audit_log.action_url = Henko::LogManager.action_url if Henko::LogManager.action_url
19
+ audit_log.user_ip = Henko::LogManager.user_ip if Henko::LogManager.user_ip
20
+
21
+ audit_log.data = changes.to_json
22
+
23
+ if action == 'update'
24
+ changes.each_key do |key|
25
+ values= changes[key]
26
+ prev_value = values[0]
27
+ new_value = values[1]
28
+ audit_log.data += "\n\nupdate #{key} from #{prev_value.nil? ? 'NIL' : prev_value} to #{new_value.nil? ? 'NIL' : new_value}\n"
29
+ end
30
+ end
31
+ audit_log.save
32
+
33
+ return true
34
+ end
35
+ end
36
+ end