save_chain_inspector 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fb5b157973994040b28e5a9a9db7fae7fba7ce8ca5e5c13a7c5dbc9dc13165d8
4
+ data.tar.gz: e88c0bd09a45fce934dedb1af3580e6e0220690e09c865ea142a237a7896834f
5
+ SHA512:
6
+ metadata.gz: aa60bbc1f71bfb213f32992030b8d80d57aac2c8797ef57eb4e421de9d3b219403c7d9944344e620b972afcf3e6ad8af9453a817adef765b4f2f7f26e45477c0
7
+ data.tar.gz: eeeb8944be49e80994d87042daa5da60eee32b886e63311f43c95c6185d575268417db34c193db552c9930c36d9d2de0fe76f22c09bbf93be2ed117f2f4a846e
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,15 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.0
3
+ SuggestExtensions: false
4
+ NewCops: enable
5
+ Layout/LineLength:
6
+ Exclude:
7
+ - save_chain_inspector.gemspec
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - 'Rakefile'
11
+ - '**/*.rake'
12
+ - 'spec/**/*.rb'
13
+ - 'Gemfile'
14
+ - '*.gemspec'
15
+
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-07-28
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2024 Shinichi Maeshima
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,58 @@
1
+ # SaveChainInspector
2
+
3
+ When you execute save on a model in Active Record, the saves of related models with pre-set hooks are also executed. SaveChainInspector provides a way to know which models and which hooks have been executed specifically.
4
+
5
+ ## Installation
6
+
7
+ Install the gem and add to the application's Gemfile by executing:
8
+
9
+ $ bundle add save_chain_inspector --group=development,test
10
+
11
+ If bundler is not being used to manage dependencies, install the gem by executing:
12
+
13
+ $ gem install save_chain_inspector
14
+
15
+ ## Usage
16
+
17
+ `SaveChainInspector.start` takes a block. It outputs the execution order of the save chain to the standard output.
18
+
19
+ ```ruby
20
+ SaveChainInspector.start do
21
+ post = Post.new
22
+ post.comments.build
23
+ post.save
24
+ end
25
+ ```
26
+
27
+ ```
28
+ Post#save start
29
+ Post#autosave_associated_records_for_comments start
30
+ Comment#save start
31
+ Comment#before_save start
32
+ Comment#autosave_associated_records_for_post start
33
+ Comment#autosave_associated_records_for_post end
34
+ Comment#before_save end
35
+ Comment#after_create start
36
+ Comment#after_create end
37
+ Comment#save end
38
+ Post#autosave_associated_records_for_comments end
39
+ Post#save end
40
+ ```
41
+
42
+ ## Development
43
+
44
+ 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.
45
+
46
+ 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).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/willnet/save_chain_inspector. 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/willnet/save_chain_inspector/blob/main/CODE_OF_CONDUCT.md).
51
+
52
+ ## License
53
+
54
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
55
+
56
+ ## Code of Conduct
57
+
58
+ Everyone interacting in the SaveChainInspector project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/willnet/save_chain_inspector/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,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SaveChainInspector
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'save_chain_inspector/version'
4
+
5
+ class SaveChainInspector # rubocop:disable Metrics/ClassLength, Style/Documentation
6
+ class << self
7
+ attr_accessor :indent, :enable
8
+ end
9
+
10
+ self.indent = 0
11
+ self.enable = false
12
+ def self.start(&block)
13
+ self.indent = 0
14
+ self.enable = true
15
+ new.call(&block)
16
+ ensure
17
+ self.enable = false
18
+ end
19
+
20
+ def initialize
21
+ ActiveRecord::Base.descendants.each do |klass|
22
+ next if klass.abstract_class
23
+ next if @save_chain_inspector_initialized
24
+
25
+ @save_chain_inspector_initialized = true
26
+ add_hooks(klass)
27
+ end
28
+ end
29
+
30
+ attr_accessor :last_call_method, :last_call_class, :last_return_method, :last_return_class
31
+
32
+ def add_hooks(klass) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
33
+ klass.before_save(prepend: true) do |model|
34
+ next unless SaveChainInspector.enable
35
+
36
+ puts "#{' ' * (SaveChainInspector.indent * 2)}#{model.class}#before_save start"
37
+ SaveChainInspector.indent += 1
38
+ end
39
+ klass.before_save do |model|
40
+ next unless SaveChainInspector.enable
41
+
42
+ SaveChainInspector.indent -= 1
43
+ puts "#{' ' * (SaveChainInspector.indent * 2)}#{model.class}#before_save end"
44
+ end
45
+ klass.set_callback(:create, :after) do |model|
46
+ next unless SaveChainInspector.enable
47
+
48
+ puts "#{' ' * (SaveChainInspector.indent * 2)}#{model.class}#after_create start"
49
+ SaveChainInspector.indent += 1
50
+ end
51
+ klass.after_create do |model|
52
+ next unless SaveChainInspector.enable
53
+
54
+ SaveChainInspector.indent -= 1
55
+ puts "#{' ' * (SaveChainInspector.indent * 2)}#{model.class}#after_create end"
56
+ end
57
+ klass.set_callback(:update, :after) do |model|
58
+ next unless SaveChainInspector.enable
59
+
60
+ puts "#{' ' * (SaveChainInspector.indent * 2)}#{model.class}#after_update start"
61
+ SaveChainInspector.indent += 1
62
+ end
63
+ klass.after_update do |model|
64
+ next unless SaveChainInspector.enable
65
+
66
+ SaveChainInspector.indent -= 1
67
+ puts "#{' ' * (SaveChainInspector.indent * 2)}#{model.class}#after_update end"
68
+ end
69
+ end
70
+
71
+ def autosave_method?(trace_point)
72
+ trace_point.method_id.match?(/autosave_associated_records_for_/)
73
+ end
74
+
75
+ def save_method?(trace_point)
76
+ trace_point.method_id == :save || trace_point.method_id == :save!
77
+ end
78
+
79
+ def duplicate_save_method_call?(trace_point)
80
+ last_call_method == trace_point.method_id && last_call_class == trace_point.self.class
81
+ end
82
+
83
+ def duplicate_save_method_return?(trace_point)
84
+ last_return_method == trace_point.method_id && last_return_class == trace_point.self.class
85
+ end
86
+
87
+ def autosave_to_save?(trace_point)
88
+ (trace_point.method_id == :save || trace_point.method_id == :save!) &&
89
+ last_call_method&.match?(/autosave_associated_records_for_/)
90
+ end
91
+
92
+ def update_last_call(trace_point)
93
+ self.last_call_class = trace_point.self.class
94
+ self.last_call_method = trace_point.method_id
95
+ end
96
+
97
+ def update_last_return(trace_point)
98
+ self.last_return_class = trace_point.self.class
99
+ self.last_return_method = trace_point.method_id
100
+ end
101
+
102
+ def call(&block)
103
+ trace.enable
104
+ block.yield
105
+ ensure
106
+ trace.disable
107
+ end
108
+
109
+ def trace # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
110
+ @trace ||= TracePoint.new(:call, :return) do |trace_point|
111
+ if trace_point.event == :call
112
+ if autosave_method?(trace_point) || (save_method?(trace_point) && !duplicate_save_method_call?(trace_point))
113
+ update_last_call(trace_point)
114
+ puts "#{' ' * (self.class.indent * 2)}#{trace_point.self.class.name}##{trace_point.method_id} start"
115
+ self.class.indent += 1
116
+ end
117
+ else # :return
118
+ if save_method?(trace_point) && !duplicate_save_method_return?(trace_point)
119
+ self.class.indent -= 1
120
+ update_last_return(trace_point)
121
+ puts "#{' ' * (self.class.indent * 2)}#{trace_point.self.class.name}##{trace_point.method_id} end"
122
+ end
123
+
124
+ if autosave_method?(trace_point)
125
+ self.class.indent -= 1
126
+ puts "#{' ' * (self.class.indent * 2)}#{trace_point.self.class.name}##{trace_point.method_id} end"
127
+ end
128
+ end
129
+ end
130
+ end
131
+ end
@@ -0,0 +1,4 @@
1
+ module SaveChainInspector
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: save_chain_inspector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shinichi Maeshima
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-07-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: When you execute save on a model in Active Record, the saves of related
28
+ models with pre-set hooks are also executed. SaveChainInspector provides a way to
29
+ know which models and which hooks have been executed specifically.
30
+ email:
31
+ - netwillnet@gmail.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - ".rspec"
37
+ - ".rubocop.yml"
38
+ - CHANGELOG.md
39
+ - CODE_OF_CONDUCT.md
40
+ - LICENSE.txt
41
+ - README.md
42
+ - Rakefile
43
+ - lib/save_chain_inspector.rb
44
+ - lib/save_chain_inspector/version.rb
45
+ - sig/save_chain_inspector.rbs
46
+ homepage: https://github.com/willnet/save_chain_inspector
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ homepage_uri: https://github.com/willnet/save_chain_inspector
51
+ source_code_uri: https://github.com/willnet/save_chain_inspector
52
+ changelog_uri: https://github.com/willnet/save_chain_inspector/releases
53
+ rubygems_mfa_required: 'true'
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 3.0.0
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ requirements: []
69
+ rubygems_version: 3.4.20
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: A tool to investigate the order in which the saves of related models are
73
+ executed.
74
+ test_files: []