snapcher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (82) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +13 -0
  4. data/.ruby-version +1 -0
  5. data/CHANGELOG.md +5 -0
  6. data/CODE_OF_CONDUCT.md +84 -0
  7. data/Gemfile +12 -0
  8. data/Gemfile.lock +63 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +39 -0
  11. data/Rakefile +12 -0
  12. data/lib/generators/snapcher/install/install_generator.rb +36 -0
  13. data/lib/generators/snapcher/templates/install.rb +20 -0
  14. data/lib/snapcher/junker.rb +127 -0
  15. data/lib/snapcher/railtie.rb +16 -0
  16. data/lib/snapcher/scanning.rb +11 -0
  17. data/lib/snapcher/sweeper.rb +42 -0
  18. data/lib/snapcher/version.rb +5 -0
  19. data/lib/snapcher.rb +30 -0
  20. data/sample-app/.dockerignore +37 -0
  21. data/sample-app/.gitattributes +9 -0
  22. data/sample-app/.gitignore +35 -0
  23. data/sample-app/.ruby-version +1 -0
  24. data/sample-app/Dockerfile +62 -0
  25. data/sample-app/Gemfile +23 -0
  26. data/sample-app/Gemfile.lock +233 -0
  27. data/sample-app/README.md +24 -0
  28. data/sample-app/Rakefile +6 -0
  29. data/sample-app/app/assets/config/manifest.js +2 -0
  30. data/sample-app/app/assets/images/.keep +0 -0
  31. data/sample-app/app/assets/stylesheets/application.css +15 -0
  32. data/sample-app/app/controllers/application_controller.rb +2 -0
  33. data/sample-app/app/controllers/concerns/.keep +0 -0
  34. data/sample-app/app/helpers/application_helper.rb +2 -0
  35. data/sample-app/app/jobs/application_job.rb +7 -0
  36. data/sample-app/app/models/application_record.rb +3 -0
  37. data/sample-app/app/models/concerns/.keep +0 -0
  38. data/sample-app/app/models/user.rb +5 -0
  39. data/sample-app/app/views/layouts/application.html.erb +15 -0
  40. data/sample-app/bin/bundle +109 -0
  41. data/sample-app/bin/docker-entrypoint +8 -0
  42. data/sample-app/bin/rails +4 -0
  43. data/sample-app/bin/rake +4 -0
  44. data/sample-app/bin/setup +33 -0
  45. data/sample-app/config/application.rb +42 -0
  46. data/sample-app/config/boot.rb +4 -0
  47. data/sample-app/config/credentials.yml.enc +1 -0
  48. data/sample-app/config/database.yml +25 -0
  49. data/sample-app/config/environment.rb +5 -0
  50. data/sample-app/config/environments/development.rb +65 -0
  51. data/sample-app/config/environments/production.rb +83 -0
  52. data/sample-app/config/environments/test.rb +54 -0
  53. data/sample-app/config/initializers/assets.rb +12 -0
  54. data/sample-app/config/initializers/content_security_policy.rb +25 -0
  55. data/sample-app/config/initializers/filter_parameter_logging.rb +8 -0
  56. data/sample-app/config/initializers/inflections.rb +16 -0
  57. data/sample-app/config/initializers/permissions_policy.rb +13 -0
  58. data/sample-app/config/locales/en.yml +31 -0
  59. data/sample-app/config/puma.rb +35 -0
  60. data/sample-app/config/routes.rb +10 -0
  61. data/sample-app/config.ru +6 -0
  62. data/sample-app/db/migrate/20231019150803_create_users.rb +15 -0
  63. data/sample-app/db/migrate/20231019151334_install_snapcher.rb +20 -0
  64. data/sample-app/db/schema.rb +37 -0
  65. data/sample-app/db/seeds.rb +9 -0
  66. data/sample-app/lib/assets/.keep +0 -0
  67. data/sample-app/lib/tasks/.keep +0 -0
  68. data/sample-app/log/.keep +0 -0
  69. data/sample-app/public/404.html +67 -0
  70. data/sample-app/public/422.html +67 -0
  71. data/sample-app/public/500.html +66 -0
  72. data/sample-app/public/apple-touch-icon-precomposed.png +0 -0
  73. data/sample-app/public/apple-touch-icon.png +0 -0
  74. data/sample-app/public/favicon.ico +0 -0
  75. data/sample-app/public/robots.txt +1 -0
  76. data/sample-app/storage/.keep +0 -0
  77. data/sample-app/tmp/.keep +0 -0
  78. data/sample-app/tmp/pids/.keep +0 -0
  79. data/sample-app/tmp/storage/.keep +0 -0
  80. data/sample-app/vendor/.keep +0 -0
  81. data/sig/snapcher.rbs +4 -0
  82. metadata +125 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d135b4fe2eb8e197ce54999308444dff97dcce48be81085900c3ab0f31630a01
4
+ data.tar.gz: 39561cc6337d078b96c55bf8eb01bb3682f9691e32d9c899f14d5efd7fc19a7d
5
+ SHA512:
6
+ metadata.gz: 862907d00cd9ced9f372a6ea76816ee05b43ed2d5689edc5b6886232697c4e5c9dea0f55671547a6c703f356cebf2aeccc795da3fb49ebabedd243c8989ef30a
7
+ data.tar.gz: 191d10f000330f07fda6c60fe54747f413149063182edd59808e289eb60e3073e14368dbdd1070d6a8fd57b6624d29af984ead148aebdfd5e11ab75c747fa793
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.2
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-10-11
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders 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, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at uchiryo7@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in snapcher.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.21"
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ snapcher (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ base64 (0.1.1)
11
+ diff-lcs (1.5.0)
12
+ json (2.6.3)
13
+ language_server-protocol (3.17.0.3)
14
+ parallel (1.23.0)
15
+ parser (3.2.2.4)
16
+ ast (~> 2.4.1)
17
+ racc
18
+ racc (1.7.1)
19
+ rainbow (3.1.1)
20
+ rake (13.0.6)
21
+ regexp_parser (2.8.2)
22
+ rexml (3.2.6)
23
+ rspec (3.12.0)
24
+ rspec-core (~> 3.12.0)
25
+ rspec-expectations (~> 3.12.0)
26
+ rspec-mocks (~> 3.12.0)
27
+ rspec-core (3.12.2)
28
+ rspec-support (~> 3.12.0)
29
+ rspec-expectations (3.12.3)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.12.0)
32
+ rspec-mocks (3.12.6)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.12.0)
35
+ rspec-support (3.12.1)
36
+ rubocop (1.57.1)
37
+ base64 (~> 0.1.1)
38
+ json (~> 2.3)
39
+ language_server-protocol (>= 3.17.0)
40
+ parallel (~> 1.10)
41
+ parser (>= 3.2.2.4)
42
+ rainbow (>= 2.2.2, < 4.0)
43
+ regexp_parser (>= 1.8, < 3.0)
44
+ rexml (>= 3.2.5, < 4.0)
45
+ rubocop-ast (>= 1.28.1, < 2.0)
46
+ ruby-progressbar (~> 1.7)
47
+ unicode-display_width (>= 2.4.0, < 3.0)
48
+ rubocop-ast (1.29.0)
49
+ parser (>= 3.2.1.0)
50
+ ruby-progressbar (1.13.0)
51
+ unicode-display_width (2.5.0)
52
+
53
+ PLATFORMS
54
+ arm64-darwin-22
55
+
56
+ DEPENDENCIES
57
+ rake (~> 13.0)
58
+ rspec (~> 3.0)
59
+ rubocop (~> 1.21)
60
+ snapcher!
61
+
62
+ BUNDLED WITH
63
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 ryosk7
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,39 @@
1
+ # Snapcher
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/snapcher`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/snapcher. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/snapcher/blob/main/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Snapcher project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/snapcher/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,36 @@
1
+ require "rails/generators"
2
+ require "rails/generators/migration"
3
+ require "active_record"
4
+ require "rails/generators/active_record"
5
+
6
+ module Snapcher
7
+ class InstallGenerator < ::Rails::Generators::Base
8
+ include Rails::Generators::Migration
9
+
10
+ source_root File.expand_path("../../templates", __FILE__)
11
+
12
+ def copy_migration
13
+ migration_template "install.rb", "db/migrate/install_snapcher.rb"
14
+ end
15
+
16
+ # Implement the required interface for Rails::Generators::Migration.
17
+ def self.next_migration_number(dirname) # :nodoc:
18
+ next_migration_number = current_migration_number(dirname) + 1
19
+ if timestamped_migrations?
20
+ [Time.now.utc.strftime("%Y%m%d%H%M%S"), "%.14d" % next_migration_number].max
21
+ else
22
+ "%.3d" % next_migration_number
23
+ end
24
+ end
25
+
26
+ def self.timestamped_migrations?
27
+ (Rails.version >= "7.0") ?
28
+ ::ActiveRecord.timestamped_migrations :
29
+ ::ActiveRecord::Base.timestamped_migrations
30
+ end
31
+
32
+ def migration_parent
33
+ "ActiveRecord::Migration[#{ActiveRecord::Migration.current_version}]"
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class <%= migration_class_name %> < <%= migration_parent %>
4
+ def self.up
5
+ create_table :scannings, :force => true do |t|
6
+ t.column :scannable_id, :integer
7
+ t.column :scannable_type, :string
8
+ t.column :table_name, :string
9
+ t.column :column_name, :string
10
+ t.column :before_params, :string
11
+ t.column :after_params, :string
12
+ t.column :action, :string
13
+ t.column :created_at, :datetime
14
+ end
15
+ end
16
+
17
+ def self.down
18
+ drop_table :scannings
19
+ end
20
+ end
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Snapcher
4
+ module Junker
5
+ extend ActiveSupport::Concern
6
+
7
+ CALLBACKS = [:scanning_create, :scanning_update, :scanning_destroy]
8
+
9
+ module ClassMethods
10
+ def scanning(options = {})
11
+ extend Snapcher::Junker::SnapcherClassMethods
12
+ include Snapcher::Junker::ScanningInstanceMethods
13
+
14
+ class_attribute :snapcher_options, instance_writer: false
15
+
16
+ self.snapcher_options = options
17
+
18
+ has_many :scannings, -> { order(version: :asc) }, as: :scannable, class_name: "Snapcher::Scanning", inverse_of: :scannable
19
+
20
+ after_create :scanning_create
21
+ before_update :scanning_update
22
+ before_destroy :scanning_destroy
23
+
24
+ define_callbacks :scanning
25
+ end
26
+ end
27
+
28
+ module ScanningInstanceMethods
29
+ def scanning_create
30
+ run_scanning(action: "create", column_name: self.snapcher_options[:column_name], after_params: snapcher_attributes[self.snapcher_options[:column_name]], table_name: self.class.table_name)
31
+ end
32
+
33
+ def scanning_update
34
+ if (changes = snapcher_changes).present?
35
+ run_scanning(action: "update", column_name: self.snapcher_options[:column_name], table_name: self.class.table_name, before_params: snapcher_changes[:before_params], after_params: snapcher_changes[:after_params])
36
+ end
37
+ end
38
+
39
+ def scanning_destroy
40
+ unless new_record?
41
+ run_scanning(action: "destroy", column_name: self.snapcher_options[:column_name], table_name: self.class.table_name)
42
+ end
43
+ end
44
+
45
+ # List of attributes that are snapcher.
46
+ def snapcher_attributes
47
+ snapcher_attributes = filter_encrypted_attrs(attributes)
48
+ normalize_enum_changes(snapcher_attributes)
49
+ end
50
+
51
+ def scanning_change_values
52
+ all_changes = if respond_to?(:changes_to_save)
53
+ changes_to_save
54
+ else
55
+ changes
56
+ end
57
+
58
+ filtered_changes = filter_encrypted_attrs(all_changes)
59
+ filtered_changes = normalize_enum_changes(filtered_changes)
60
+ filtered_changes.to_hash
61
+ end
62
+
63
+ def snapcher_changes
64
+ filtered_changes = scanning_change_values
65
+
66
+ monitoring_column_name = self.snapcher_options[:column_name]
67
+
68
+ return if filtered_changes[monitoring_column_name.to_s].nil?
69
+
70
+ before_params = filtered_changes[monitoring_column_name.to_s][0]
71
+ after_params = filtered_changes[monitoring_column_name.to_s][1]
72
+ {before_params: before_params, after_params: after_params}
73
+ end
74
+
75
+ def run_scanning(attrs)
76
+ run_callbacks(:scanning) {
77
+ scanning = scannings.create(attrs)
78
+ scanning
79
+ }
80
+ end
81
+
82
+ def filter_encrypted_attrs(filtered_changes)
83
+ filter_attr_values(
84
+ snapcher_changes: filtered_changes,
85
+ attrs: respond_to?(:encrypted_attributes) ? Array(encrypted_attributes).map(&:to_s) : []
86
+ )
87
+ end
88
+
89
+ def filter_attr_values(snapcher_changes: {}, attrs: [], placeholder: "[FILTERED]")
90
+ attrs.each do |attr|
91
+ next unless snapcher_changes.key?(attr)
92
+
93
+ changes = snapcher_changes[attr]
94
+ values = changes.is_a?(Array) ? changes.map { placeholder } : placeholder
95
+
96
+ snapcher_changes[attr] = values
97
+ end
98
+
99
+ snapcher_changes
100
+ end
101
+
102
+ def normalize_enum_changes(changes)
103
+ self.class.defined_enums.each do |name, values|
104
+ next unless changes.key?(name)
105
+
106
+ changes[name] = \
107
+ if changes[name].is_a?(Array)
108
+ changes[name].map { |v| values[v] }
109
+ else
110
+ values[changes[name]]
111
+ end
112
+ end
113
+ changes
114
+ end
115
+
116
+ CALLBACKS.each do |attr_name|
117
+ alias_method "#{attr_name}_callback".to_sym, attr_name
118
+ end
119
+ end
120
+
121
+ module SnapcherClassMethods
122
+ def snapcher_columns
123
+ @snapcher_columns ||= column_names
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Snapcher
4
+ class Railtie < Rails::Railtie
5
+ initializer "snapcher.sweeper" do
6
+ ActiveSupport.on_load(:action_controller) do
7
+ if defined?(ActionController::Base)
8
+ ActionController::Base.around_action Snapcher::Sweeper.new
9
+ end
10
+ if defined?(ActionController::API)
11
+ ActionController::API.around_action Snapcher::Sweeper.new
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Snapcher
4
+ class Scanning < ::ActiveRecord::Base
5
+ belongs_to :scannable, polymorphic: true
6
+
7
+ cattr_accessor :snapcher_class_names
8
+
9
+ scope :scannable_finder, ->(scannable_id, scannable_type) { where(scannable_id: scannable_id, scannable_type: scannable_type) }
10
+ end
11
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Snapcher
4
+ class Sweeper
5
+ STORED_DATA = {
6
+ current_remote_address: :remote_ip,
7
+ current_request_uuid: :request_uuid,
8
+ current_user: :current_user
9
+ }
10
+
11
+ delegate :store, to: ::Snapcher
12
+
13
+ def around(controller)
14
+ self.controller = controller
15
+ STORED_DATA.each { |k, m| store[k] = send(m) }
16
+ yield
17
+ ensure
18
+ self.controller = nil
19
+ STORED_DATA.keys.each { |k| store.delete(k) }
20
+ end
21
+
22
+ def current_user
23
+ lambda { controller.send(Snapcher.current_user_method) if controller.respond_to?(Snapcher.current_user_method, true) }
24
+ end
25
+
26
+ def remote_ip
27
+ controller.try(:request).try(:remote_ip)
28
+ end
29
+
30
+ def request_uuid
31
+ controller.try(:request).try(:uuid)
32
+ end
33
+
34
+ def controller
35
+ store[:current_controller]
36
+ end
37
+
38
+ def controller=(value)
39
+ store[:current_controller] = value
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Snapcher
4
+ VERSION = "0.1.0"
5
+ end
data/lib/snapcher.rb ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record"
4
+ require_relative "snapcher/version"
5
+
6
+ module Snapcher
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+
10
+ class << self
11
+ attr_accessor :column_name
12
+ attr_writer :scanning_class
13
+
14
+ def scanning_class
15
+ # The scanning_class is set as String in the initializer. It can not be constantized during initialization and must
16
+ # be constantized at runtime.
17
+ @scanning_class = @scanning_class.safe_constantize if @scanning_class.is_a?(String)
18
+ @scanning_class ||= Snapcher::Scanning
19
+ end
20
+ end
21
+ end
22
+
23
+ require "snapcher/junker"
24
+
25
+ ActiveSupport.on_load :active_record do
26
+ require "snapcher/scanning"
27
+ include Snapcher::Junker
28
+ end
29
+
30
+ require "snapcher/railtie"
@@ -0,0 +1,37 @@
1
+ # See https://docs.docker.com/engine/reference/builder/#dockerignore-file for more about ignoring files.
2
+
3
+ # Ignore git directory.
4
+ /.git/
5
+
6
+ # Ignore bundler config.
7
+ /.bundle
8
+
9
+ # Ignore all environment files (except templates).
10
+ /.env*
11
+ !/.env*.erb
12
+
13
+ # Ignore all default key files.
14
+ /config/master.key
15
+ /config/credentials/*.key
16
+
17
+ # Ignore all logfiles and tempfiles.
18
+ /log/*
19
+ /tmp/*
20
+ !/log/.keep
21
+ !/tmp/.keep
22
+
23
+ # Ignore pidfiles, but keep the directory.
24
+ /tmp/pids/*
25
+ !/tmp/pids/.keep
26
+
27
+ # Ignore storage (uploaded files in development and any SQLite databases).
28
+ /storage/*
29
+ !/storage/.keep
30
+ /tmp/storage/*
31
+ !/tmp/storage/.keep
32
+
33
+ # Ignore assets.
34
+ /node_modules/
35
+ /app/assets/builds/*
36
+ !/app/assets/builds/.keep
37
+ /public/assets
@@ -0,0 +1,9 @@
1
+ # See https://git-scm.com/docs/gitattributes for more about git attribute files.
2
+
3
+ # Mark the database schema as having been generated.
4
+ db/schema.rb linguist-generated
5
+
6
+ # Mark any vendored files as having been vendored.
7
+ vendor/* linguist-vendored
8
+ config/credentials/*.yml.enc diff=rails_credentials
9
+ config/credentials.yml.enc diff=rails_credentials
@@ -0,0 +1,35 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore all environment files (except templates).
11
+ /.env*
12
+ !/.env*.erb
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*
16
+ /tmp/*
17
+ !/log/.keep
18
+ !/tmp/.keep
19
+
20
+ # Ignore pidfiles, but keep the directory.
21
+ /tmp/pids/*
22
+ !/tmp/pids/
23
+ !/tmp/pids/.keep
24
+
25
+ # Ignore storage (uploaded files in development and any SQLite databases).
26
+ /storage/*
27
+ !/storage/.keep
28
+ /tmp/storage/*
29
+ !/tmp/storage/
30
+ !/tmp/storage/.keep
31
+
32
+ /public/assets
33
+
34
+ # Ignore master key for decrypting credentials and more.
35
+ /config/master.key
@@ -0,0 +1 @@
1
+ 3.2.2