models_auditor 0.1.0

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.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +37 -0
  4. data/app/assets/javascripts/models_auditor/application.js +13 -0
  5. data/app/assets/stylesheets/models_auditor/application.css +64 -0
  6. data/app/controllers/models_auditor/audit_base_controller.rb +5 -0
  7. data/app/controllers/models_auditor/audit_controller.rb +121 -0
  8. data/app/helpers/models_auditor/application_helper.rb +4 -0
  9. data/app/models/models_auditor/audit_record.rb +17 -0
  10. data/app/models/models_auditor/audit_request.rb +12 -0
  11. data/app/views/layouts/models_auditor/application.html.erb +12 -0
  12. data/app/views/models_auditor/audit/_record.html.erb +39 -0
  13. data/app/views/models_auditor/audit/_request.html.erb +25 -0
  14. data/app/views/models_auditor/audit/index.html.erb +3 -0
  15. data/config/config_option_descriptions.yml +23 -0
  16. data/config/routes.rb +5 -0
  17. data/lib/generators/models_auditor/db_config/USAGE +7 -0
  18. data/lib/generators/models_auditor/db_config/db_config_generator.rb +23 -0
  19. data/lib/generators/models_auditor/install/USAGE +8 -0
  20. data/lib/generators/models_auditor/install/install_generator.rb +25 -0
  21. data/lib/generators/models_auditor/install/templates/initializer.rb.erb +13 -0
  22. data/lib/generators/models_auditor/migrations/USAGE +8 -0
  23. data/lib/generators/models_auditor/migrations/migrations_generator.rb +15 -0
  24. data/lib/generators/models_auditor/migrations/templates/create_audit_records.rb.erb +18 -0
  25. data/lib/generators/models_auditor/migrations/templates/create_audit_requests.rb.erb +12 -0
  26. data/lib/generators/models_auditor/migrations_helper.rb +24 -0
  27. data/lib/models_auditor.rb +37 -0
  28. data/lib/models_auditor/audit.rb +202 -0
  29. data/lib/models_auditor/config.rb +118 -0
  30. data/lib/models_auditor/controller.rb +67 -0
  31. data/lib/models_auditor/engine.rb +5 -0
  32. data/lib/models_auditor/version.rb +3 -0
  33. data/lib/tasks/models_auditor_tasks.rake +75 -0
  34. data/test/dummy/README.rdoc +28 -0
  35. data/test/dummy/Rakefile +6 -0
  36. data/test/dummy/app/assets/javascripts/application.js +13 -0
  37. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  38. data/test/dummy/app/helpers/application_helper.rb +2 -0
  39. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  40. data/test/dummy/bin/bundle +3 -0
  41. data/test/dummy/bin/rails +4 -0
  42. data/test/dummy/bin/rake +4 -0
  43. data/test/dummy/bin/setup +29 -0
  44. data/test/dummy/config.ru +4 -0
  45. data/test/dummy/config/application.rb +26 -0
  46. data/test/dummy/config/boot.rb +5 -0
  47. data/test/dummy/config/database.yml +25 -0
  48. data/test/dummy/config/environment.rb +5 -0
  49. data/test/dummy/config/environments/development.rb +41 -0
  50. data/test/dummy/config/environments/production.rb +79 -0
  51. data/test/dummy/config/environments/test.rb +42 -0
  52. data/test/dummy/config/initializers/assets.rb +11 -0
  53. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  54. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  55. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  56. data/test/dummy/config/initializers/inflections.rb +16 -0
  57. data/test/dummy/config/initializers/mime_types.rb +4 -0
  58. data/test/dummy/config/initializers/session_store.rb +3 -0
  59. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  60. data/test/dummy/config/locales/en.yml +23 -0
  61. data/test/dummy/config/routes.rb +4 -0
  62. data/test/dummy/config/secrets.yml +22 -0
  63. data/test/dummy/public/404.html +67 -0
  64. data/test/dummy/public/422.html +67 -0
  65. data/test/dummy/public/500.html +66 -0
  66. data/test/dummy/public/favicon.ico +0 -0
  67. data/test/integration/navigation_test.rb +8 -0
  68. data/test/models_auditor_test.rb +7 -0
  69. data/test/test_helper.rb +21 -0
  70. metadata +249 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 267bcb8f116da48b04983ca8ebd0f383a645ae63
4
+ data.tar.gz: 8925843248724eb62269f2d7f9f1dd558e4170d2
5
+ SHA512:
6
+ metadata.gz: 1c70a5409a656494595045a74ca5a6b517586a427ea59b3ad8629f1c45c9b10c4cfbd9219552faa70a9537a8af02526eda2dbae49e960fd87cfd75d9ea69a5f3
7
+ data.tar.gz: 7a3fd44fa43106b2281d4488104c1aa99090bf6c6cbea5ce6ba73d393410aa79646e873284d1116281166edd90890baba2f5124a8d445758b5f91de6d5e7b3d0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Alexander Gorbunov
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/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ModelsAuditor'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,13 @@
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 any plugin's vendor/assets/javascripts directory 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
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,64 @@
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 any plugin's vendor/assets/stylesheets directory 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 bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
16
+
17
+ .full-width {
18
+ width: 100%;
19
+ }
20
+
21
+ .models-auditor-flex {
22
+ display: flex;
23
+ }
24
+
25
+ .models-auditor-column {
26
+ flex-direction: column;
27
+ }
28
+
29
+ .models-auditor-list {
30
+ display: flex;
31
+ }
32
+
33
+ .models-auditor-list .models-auditor-item {
34
+ display: flex;
35
+ }
36
+
37
+ .models-auditor-form-group {
38
+ display: flex;
39
+ flex-wrap: wrap;
40
+ }
41
+
42
+ .models-auditor-label {
43
+ flex-basis: 10%;
44
+ font-weight: 700;
45
+ }
46
+
47
+ .models-auditor-val {
48
+ flex-basis: 90%;
49
+ padding-left: 25px;
50
+ box-sizing: border-box;
51
+ }
52
+
53
+ .models-auditor-parameter {
54
+ display: flex;
55
+
56
+ }
57
+
58
+ .models-auditor-parameter .models-auditor-form-group {
59
+ padding-left: 25px;
60
+ }
61
+
62
+ .models-auditor-audit-record {
63
+ border-bottom: 1px solid #ccc;
64
+ }
@@ -0,0 +1,5 @@
1
+ module ModelsAuditor
2
+ class AuditBaseController < ActionController::Base
3
+ protect_from_forgery with: :exception
4
+ end
5
+ end
@@ -0,0 +1,121 @@
1
+ module ModelsAuditor
2
+ class AuditController < ModelsAuditor.config.audit_controller_base.classify.constantize
3
+ layout 'models_auditor/application'
4
+
5
+ # GET /pages.json
6
+ def index
7
+ page = params.fetch(:page, 1).to_i
8
+ per_page = ModelsAuditor.config.records_per_page
9
+ paginate_info = {page: page, per_page: per_page}.tap do |info|
10
+ info.merge!(total_entries: (page * per_page + per_page * 10)) if ModelsAuditor.config.fake_total_count
11
+ end
12
+
13
+ @collection =
14
+ ModelsAuditor::AuditRequest.includes(:records).all
15
+ .order(created_at: :desc)
16
+ .paginate(paginate_info)
17
+
18
+ @collection = apply_filters(@collection, params[:filters])
19
+
20
+ respond_to do |f|
21
+ if ModelsAuditor.config.respond_to_json_enabled
22
+ f.json {
23
+ render json: {
24
+ ModelsAuditor.config.json_response_data_key => structure_requests_data(@collection),
25
+ ModelsAuditor.config.json_response_meta_key => {
26
+ per_page: @collection.per_page,
27
+ total: @collection.total_entries,
28
+ sort_by: @collection.order_info
29
+ }
30
+ }
31
+ }
32
+ end
33
+ if ModelsAuditor.config.respond_to_html_enabled
34
+ f.html
35
+ end
36
+ end
37
+ end
38
+
39
+ private
40
+
41
+ def apply_filters(collection, filters)
42
+ return collection if filters.blank? || collection.nil?
43
+ if filters[:since_at].present? && (time = (Time.parse(filters[:since_at]) rescue nil)).present?
44
+ collection =
45
+ collection
46
+ .where(ModelsAuditor::AuditRecord.arel_table[:created_at].gteq(time))
47
+ .references(ModelsAuditor::AuditRecord.table_name.to_sym)
48
+ end
49
+ if filters[:before_at].present? && (time = (Time.parse(filters[:before_at]) rescue nil)).present?
50
+ collection =
51
+ collection
52
+ .where(ModelsAuditor::AuditRecord.arel_table[:created_at].lteq(time))
53
+ .references(ModelsAuditor::AuditRecord.table_name.to_sym)
54
+ end
55
+ if filters[:action].present?
56
+ collection = collection.where(ModelsAuditor::AuditRecord.arel_table[:action].eq(ModelsAuditor::AuditRecord.actions[filters[:action]])).references(ModelsAuditor::AuditRecord.table_name.to_sym)
57
+
58
+ end
59
+ if filters[:user_id].present?
60
+ collection = collection.where(user_id: filters[:user_id])
61
+ end
62
+ if filters[:object].present?
63
+ cond = {}
64
+ if (object_id = filters[:object][:id].to_i) > 0
65
+ cond[:object_id] = object_id
66
+ end
67
+ if (object_type = filters[:object][:type]).present?
68
+ cond[:object_type] = object_type
69
+ end
70
+
71
+ filtered_requests = collection.where(ModelsAuditor::AuditRecord.table_name.to_sym => cond).pluck(:id)
72
+ collection = collection.where(id: filtered_requests)
73
+ end
74
+
75
+ collection
76
+ end
77
+
78
+ def get_relations(record, records)
79
+ rel_records = records.select { |r| !r.bridge.nil? && r.bridge.keys.include?(record.object_type) }
80
+ rel_records.map do |i|
81
+ target_class = i.bridge.except(record.object_type).keys.first
82
+ next unless target_class
83
+ i.attributes.slice('id', 'object_type', 'object_id').merge(target: {target_class => i.bridge[target_class].values.first})
84
+ end.compact
85
+ end
86
+
87
+ # @param [ActiveRecord::Relation|ModelsAuditor::AuditRequest|Array] data
88
+ def structure_requests_data(data)
89
+ requests =
90
+ case data
91
+ when ActiveRecord::Relation
92
+ data.to_a
93
+ when ModelsAuditor::AuditRequest
94
+ [data]
95
+ when Array
96
+ data
97
+ else
98
+ raise ArgumentError('Incorrect type of argument `requests`')
99
+ end
100
+
101
+ requests.map do |request|
102
+ records = request.records
103
+ {}.tap do |result|
104
+ changed_models_collection =
105
+ records
106
+ .select { |record| record.bridge.nil? }
107
+ .map do |record|
108
+ {
109
+ data: record.attributes.slice('id', 'object_type', 'object_id'),
110
+ relationships: get_relations(record, records)
111
+ }
112
+ end
113
+
114
+ result[:request] = request.as_json
115
+ result[:changes_struct] = changed_models_collection.group_by { |i| i[:data]['object_type'] }
116
+ result[:all_changes] = records.map(&:as_json)
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,4 @@
1
+ module ModelsAuditor
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,17 @@
1
+ module ModelsAuditor
2
+ class AuditRecord < ActiveRecord::Base
3
+ ACTION_CREATE = 0
4
+ ACTION_UPDATE = 1
5
+ ACTION_DESTROY = 2
6
+
7
+ establish_connection [ModelsAuditor.config.connection_namespace, Rails.env].map(&:presence).compact.join('_').to_sym
8
+ self.table_name = ModelsAuditor.config.audit_records_table_name
9
+
10
+ belongs_to :request, class_name: ModelsAuditor::AuditRequest.name, foreign_key: :request_id
11
+ belongs_to :auditable, polymorphic: true, foreign_key: :object_id, foreign_type: :object_type
12
+
13
+ enum action: {action_create: ACTION_CREATE, action_update: ACTION_UPDATE, action_destroy: ACTION_DESTROY}
14
+
15
+ validates :object_type, :object_id, presence: true
16
+ end
17
+ end
@@ -0,0 +1,12 @@
1
+ module ModelsAuditor
2
+ class AuditRequest < ActiveRecord::Base
3
+ establish_connection [ModelsAuditor.config.connection_namespace, Rails.env].map(&:presence).compact.join('_').to_sym
4
+ self.table_name = ModelsAuditor.config.audit_requests_table_name
5
+
6
+ has_many :records, class_name: ModelsAuditor::AuditRecord.name, foreign_key: :request_id, inverse_of: :request
7
+
8
+ # def as_json(options = nil)
9
+ # super({include: :records }.merge(options || {}))
10
+ # end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>ModelsAuditor</title>
5
+ <%= stylesheet_link_tag 'models_auditor/application', media: 'all' %>
6
+ <%= csrf_meta_tags %>
7
+ </head>
8
+ <body>
9
+ <%= yield %>
10
+ <%= javascript_include_tag 'models_auditor/application' %>
11
+ </body>
12
+ </html>
@@ -0,0 +1,39 @@
1
+ <div class="models-auditor-item full-width models-auditor-column models-auditor-audit-record">
2
+ <div class="models-auditor-parameter full-width models-auditor-column">
3
+ <div class="models-auditor-label">Модель:</div>
4
+ <div class="models-auditor-form-group models-auditor-val"><%= entry.object_type %></div>
5
+ </div>
6
+ <div class="models-auditor-parameter full-width models-auditor-column">
7
+ <div class="models-auditor-label">ID:</div>
8
+ <div class="models-auditor-form-group models-auditor-val"><%= entry.object_id %></div>
9
+ </div>
10
+ <div class="models-auditor-parameter full-width models-auditor-column">
11
+ <div class="models-auditor-label">Request:</div>
12
+ <div class="models-auditor-form-group models-auditor-val"><a href="<%= models_auditor_request_path(id: entry.request_id) %>">#<%= entry.request_id %></a></div>
13
+ </div>
14
+ <div class="models-auditor-parameter full-width models-auditor-column">
15
+ <div class="models-auditor-label">Действие:</div>
16
+ <div class="models-auditor-form-group models-auditor-val">
17
+ <% case entry.read_attribute(:action) %>
18
+ <% when ModelsAuditor::AuditRecord::ACTION_CREATE %>
19
+ Создание
20
+ <% when ModelsAuditor::AuditRecord::ACTION_UPDATE %>
21
+ Внесение изменений
22
+ <% when ModelsAuditor::AuditRecord::ACTION_DESTROY %>
23
+ Удаление
24
+ <% end %>
25
+ </div>
26
+ </div>
27
+ <div class="models-auditor-parameter full-width models-auditor-column">
28
+ <div class="models-auditor-label">Изменения:</div>
29
+ <div class="models-auditor-form-group models-auditor-val models-auditor-column">
30
+ <% (entry.content || {}).each do |key, val| %>
31
+ <div class="models-auditor-form-group">
32
+ <div class="models-auditor-label"><%= key %></div>
33
+ <div class="models-auditor-val"><%= val %></div>
34
+ </div>
35
+ <% end %>
36
+ </div>
37
+ </div>
38
+
39
+ </div>
@@ -0,0 +1,25 @@
1
+ <div class="models-auditor-item full-width models-auditor-column">
2
+ <div class="models-auditor-parameter full-width models-auditor-column">
3
+ <div class="models-auditor-label">Параметры запроса:</div>
4
+ <% (entry.request_info || {}).each do |key, val| %>
5
+ <div class="models-auditor-form-group">
6
+ <div class="models-auditor-label"><%= key %></div>
7
+ <div class="models-auditor-val"><%= val %></div>
8
+ </div>
9
+ <% end %>
10
+ </div>
11
+ <div class="models-auditor-parameter full-width models-auditor-column">
12
+ <div class="models-auditor-label">ID Ответственного:</div>
13
+ <div class="models-auditor-form-group"><%= entry.user_id %></div>
14
+ </div>
15
+ <div class="models-auditor-parameter full-width models-auditor-column">
16
+ <div class="models-auditor-label">Модели:</div>
17
+ <div class="models-auditor-form-group">
18
+ <div class="models-auditor-list models-auditor-column full-width">
19
+ <% (entry.records || []).each do |record| %>
20
+ <%= render partial: 'record', object: record, as: :entry %>
21
+ <% end %>
22
+ </div>
23
+ </div>
24
+ </div>
25
+ </div>
@@ -0,0 +1,3 @@
1
+ <div class="models-auditor-list full-width models-auditor-column">
2
+ <%= render partial: 'request', collection: @collection, as: :entry %>
3
+ </div>
@@ -0,0 +1,23 @@
1
+ audit_enabled: Включение/Отключение аудита всех моделей
2
+ connection_namespace: Namespace для конфига базы данных
3
+ audit_migrations_dir: Папка для хранения миграций аудитора
4
+ audit_records_table_name: Название таблицы для хрянения логов
5
+ audit_requests_table_name: Название таблицы для хранения информации о запросах, приведших к изменению данных
6
+ audit_request_changes_only: |-
7
+ true: Логировать только изменения, производимые через Request
8
+ false: Логировать любые изменения
9
+ logger: |-
10
+ Может принимать объект класса Logger или false
11
+ Пример:
12
+ config.logger = false
13
+ или
14
+ config.logger = Logger.new(Rails.root.join('log', 'models_auditor.log'))
15
+ records_per_page: Количество залогированных записей об изменениях в моделях на страницу
16
+ fake_total_count: Не настоящее значение total_count записей в логах
17
+ audit_controller_base: |-
18
+ Базовый класс audit контроллера
19
+ Пример: config.audit_controller_base = '::ApplicationController'
20
+ respond_to_json_enabled: Доступ к логам через json api
21
+ respond_to_html_enabled: Доступ к логам через html
22
+ json_response_data_key: Ключ с залогированными данными в json ответе
23
+ json_response_meta_key: Ключ с meta информацией в json ответе
data/config/routes.rb ADDED
@@ -0,0 +1,5 @@
1
+ ModelsAuditor::Engine.routes.draw do
2
+ get '(/index)(/page/:page)', to: 'audit#index', as: :models_auditor_requests, page: /\d+/, format: [:json, :html]
3
+
4
+ root to: 'audit#index'
5
+ end
@@ -0,0 +1,7 @@
1
+ Description:
2
+ Append audit database config
3
+
4
+ Example:
5
+ rails generate models_auditor:db_config
6
+
7
+ This will append audit database config to database.yml