order_cop 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: 83506d77c88c7d0fb5f3b5b0b779bdb33262adb7645951127fd2feb1668db52f
4
+ data.tar.gz: ee2e8013a24a8078e87455a2aa88e98aae921137a870d9c3d29bdb1dbc067a76
5
+ SHA512:
6
+ metadata.gz: 40dc1ee7a51ded6c68b4c44e68d0ce96c5a97f53ce95de5f68fb8651aab44c357f72564de56b623b853bba01867a8faf9dc7221c830a70ed5f961a1e61865a28
7
+ data.tar.gz: 11eb778fdc25c1bbc1e164ec27a1c2738a2d3541d49b7c53a0a5e5ca8a13f06e07a298332a859080787eb0351281b2bb999b756532dde3aa4ac330bccf4a48b1
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in order_cop.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "standard", "~> 1.3"
13
+
14
+ gem "rails", "~> 7.0.0"
15
+ gem "sqlite3"
data/Gemfile.lock ADDED
@@ -0,0 +1,221 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ order_cop (0.1.0)
5
+ binding_of_caller (~> 1.0)
6
+ rails (~> 7.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ actioncable (7.0.7)
12
+ actionpack (= 7.0.7)
13
+ activesupport (= 7.0.7)
14
+ nio4r (~> 2.0)
15
+ websocket-driver (>= 0.6.1)
16
+ actionmailbox (7.0.7)
17
+ actionpack (= 7.0.7)
18
+ activejob (= 7.0.7)
19
+ activerecord (= 7.0.7)
20
+ activestorage (= 7.0.7)
21
+ activesupport (= 7.0.7)
22
+ mail (>= 2.7.1)
23
+ net-imap
24
+ net-pop
25
+ net-smtp
26
+ actionmailer (7.0.7)
27
+ actionpack (= 7.0.7)
28
+ actionview (= 7.0.7)
29
+ activejob (= 7.0.7)
30
+ activesupport (= 7.0.7)
31
+ mail (~> 2.5, >= 2.5.4)
32
+ net-imap
33
+ net-pop
34
+ net-smtp
35
+ rails-dom-testing (~> 2.0)
36
+ actionpack (7.0.7)
37
+ actionview (= 7.0.7)
38
+ activesupport (= 7.0.7)
39
+ rack (~> 2.0, >= 2.2.4)
40
+ rack-test (>= 0.6.3)
41
+ rails-dom-testing (~> 2.0)
42
+ rails-html-sanitizer (~> 1.0, >= 1.2.0)
43
+ actiontext (7.0.7)
44
+ actionpack (= 7.0.7)
45
+ activerecord (= 7.0.7)
46
+ activestorage (= 7.0.7)
47
+ activesupport (= 7.0.7)
48
+ globalid (>= 0.6.0)
49
+ nokogiri (>= 1.8.5)
50
+ actionview (7.0.7)
51
+ activesupport (= 7.0.7)
52
+ builder (~> 3.1)
53
+ erubi (~> 1.4)
54
+ rails-dom-testing (~> 2.0)
55
+ rails-html-sanitizer (~> 1.1, >= 1.2.0)
56
+ activejob (7.0.7)
57
+ activesupport (= 7.0.7)
58
+ globalid (>= 0.3.6)
59
+ activemodel (7.0.7)
60
+ activesupport (= 7.0.7)
61
+ activerecord (7.0.7)
62
+ activemodel (= 7.0.7)
63
+ activesupport (= 7.0.7)
64
+ activestorage (7.0.7)
65
+ actionpack (= 7.0.7)
66
+ activejob (= 7.0.7)
67
+ activerecord (= 7.0.7)
68
+ activesupport (= 7.0.7)
69
+ marcel (~> 1.0)
70
+ mini_mime (>= 1.1.0)
71
+ activesupport (7.0.7)
72
+ concurrent-ruby (~> 1.0, >= 1.0.2)
73
+ i18n (>= 1.6, < 2)
74
+ minitest (>= 5.1)
75
+ tzinfo (~> 2.0)
76
+ ast (2.4.2)
77
+ binding_of_caller (1.0.0)
78
+ debug_inspector (>= 0.0.1)
79
+ builder (3.2.4)
80
+ concurrent-ruby (1.2.2)
81
+ crass (1.0.6)
82
+ date (3.3.3)
83
+ debug_inspector (1.1.0)
84
+ diff-lcs (1.5.0)
85
+ erubi (1.12.0)
86
+ globalid (1.1.0)
87
+ activesupport (>= 5.0)
88
+ i18n (1.14.1)
89
+ concurrent-ruby (~> 1.0)
90
+ json (2.6.3)
91
+ language_server-protocol (3.17.0.3)
92
+ lint_roller (1.1.0)
93
+ loofah (2.21.3)
94
+ crass (~> 1.0.2)
95
+ nokogiri (>= 1.12.0)
96
+ mail (2.8.1)
97
+ mini_mime (>= 0.1.1)
98
+ net-imap
99
+ net-pop
100
+ net-smtp
101
+ marcel (1.0.2)
102
+ method_source (1.0.0)
103
+ mini_mime (1.1.5)
104
+ minitest (5.19.0)
105
+ net-imap (0.3.7)
106
+ date
107
+ net-protocol
108
+ net-pop (0.1.2)
109
+ net-protocol
110
+ net-protocol (0.2.1)
111
+ timeout
112
+ net-smtp (0.3.3)
113
+ net-protocol
114
+ nio4r (2.5.9)
115
+ nokogiri (1.15.4-x86_64-linux)
116
+ racc (~> 1.4)
117
+ parallel (1.23.0)
118
+ parser (3.2.2.3)
119
+ ast (~> 2.4.1)
120
+ racc
121
+ racc (1.7.1)
122
+ rack (2.2.8)
123
+ rack-test (2.1.0)
124
+ rack (>= 1.3)
125
+ rails (7.0.7)
126
+ actioncable (= 7.0.7)
127
+ actionmailbox (= 7.0.7)
128
+ actionmailer (= 7.0.7)
129
+ actionpack (= 7.0.7)
130
+ actiontext (= 7.0.7)
131
+ actionview (= 7.0.7)
132
+ activejob (= 7.0.7)
133
+ activemodel (= 7.0.7)
134
+ activerecord (= 7.0.7)
135
+ activestorage (= 7.0.7)
136
+ activesupport (= 7.0.7)
137
+ bundler (>= 1.15.0)
138
+ railties (= 7.0.7)
139
+ rails-dom-testing (2.2.0)
140
+ activesupport (>= 5.0.0)
141
+ minitest
142
+ nokogiri (>= 1.6)
143
+ rails-html-sanitizer (1.6.0)
144
+ loofah (~> 2.21)
145
+ nokogiri (~> 1.14)
146
+ railties (7.0.7)
147
+ actionpack (= 7.0.7)
148
+ activesupport (= 7.0.7)
149
+ method_source
150
+ rake (>= 12.2)
151
+ thor (~> 1.0)
152
+ zeitwerk (~> 2.5)
153
+ rainbow (3.1.1)
154
+ rake (13.0.6)
155
+ regexp_parser (2.8.1)
156
+ rexml (3.2.6)
157
+ rspec (3.12.0)
158
+ rspec-core (~> 3.12.0)
159
+ rspec-expectations (~> 3.12.0)
160
+ rspec-mocks (~> 3.12.0)
161
+ rspec-core (3.12.2)
162
+ rspec-support (~> 3.12.0)
163
+ rspec-expectations (3.12.3)
164
+ diff-lcs (>= 1.2.0, < 2.0)
165
+ rspec-support (~> 3.12.0)
166
+ rspec-mocks (3.12.6)
167
+ diff-lcs (>= 1.2.0, < 2.0)
168
+ rspec-support (~> 3.12.0)
169
+ rspec-support (3.12.1)
170
+ rubocop (1.52.1)
171
+ json (~> 2.3)
172
+ parallel (~> 1.10)
173
+ parser (>= 3.2.2.3)
174
+ rainbow (>= 2.2.2, < 4.0)
175
+ regexp_parser (>= 1.8, < 3.0)
176
+ rexml (>= 3.2.5, < 4.0)
177
+ rubocop-ast (>= 1.28.0, < 2.0)
178
+ ruby-progressbar (~> 1.7)
179
+ unicode-display_width (>= 2.4.0, < 3.0)
180
+ rubocop-ast (1.29.0)
181
+ parser (>= 3.2.1.0)
182
+ rubocop-performance (1.18.0)
183
+ rubocop (>= 1.7.0, < 2.0)
184
+ rubocop-ast (>= 0.4.0)
185
+ ruby-progressbar (1.13.0)
186
+ sqlite3 (1.6.3-x86_64-linux)
187
+ standard (1.30.1)
188
+ language_server-protocol (~> 3.17.0.2)
189
+ lint_roller (~> 1.0)
190
+ rubocop (~> 1.52.0)
191
+ standard-custom (~> 1.0.0)
192
+ standard-performance (~> 1.1.0)
193
+ standard-custom (1.0.2)
194
+ lint_roller (~> 1.0)
195
+ rubocop (~> 1.50)
196
+ standard-performance (1.1.2)
197
+ lint_roller (~> 1.1)
198
+ rubocop-performance (~> 1.18.0)
199
+ thor (1.2.2)
200
+ timeout (0.4.0)
201
+ tzinfo (2.0.6)
202
+ concurrent-ruby (~> 1.0)
203
+ unicode-display_width (2.4.2)
204
+ websocket-driver (0.7.6)
205
+ websocket-extensions (>= 0.1.0)
206
+ websocket-extensions (0.1.5)
207
+ zeitwerk (2.6.11)
208
+
209
+ PLATFORMS
210
+ x86_64-linux
211
+
212
+ DEPENDENCIES
213
+ order_cop!
214
+ rails (~> 7.0.0)
215
+ rake (~> 13.0)
216
+ rspec (~> 3.0)
217
+ sqlite3
218
+ standard (~> 1.3)
219
+
220
+ BUNDLED WITH
221
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Gauthier Monserand
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,35 @@
1
+ # OrderCop
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/order_cop`. 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]/order_cop.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
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 "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module OrderCop
4
+ VERSION = "0.1.0"
5
+ end
data/lib/order_cop.rb ADDED
@@ -0,0 +1,173 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "order_cop/version"
4
+ require "active_record"
5
+ require "active_support"
6
+ require "rails"
7
+ require "ostruct"
8
+ require "binding_of_caller"
9
+
10
+ require "zeitwerk"
11
+ loader = Zeitwerk::Loader.for_gem
12
+ loader.setup
13
+ module OrderCop
14
+ module OrderCopMixin
15
+ # patch all methods which iterate over the collection and return several records
16
+ # we don't patch `first` and `last`, take or others because they return a single record)
17
+ # we don't patch sort because it specifically doesn't need an order
18
+ # we don't patch select as it's a query method
19
+
20
+ def each(...)
21
+ detect_missing_order(:each) if order_values.empty?
22
+ super(...)
23
+ end
24
+
25
+ def to_a
26
+ detect_missing_order(:to_a) if order_values.empty?
27
+ super
28
+ end
29
+
30
+ def map(...)
31
+ detect_missing_order(:map) if order_values.empty?
32
+ super(...)
33
+ end
34
+
35
+ def find_each(...)
36
+ detect_missing_order(:find_each) if order_values.empty?
37
+ super(...)
38
+ end
39
+
40
+ def find_in_batches(...)
41
+ detect_missing_order(:find_in_batches) if order_values.empty?
42
+ super(...)
43
+ end
44
+
45
+ def reject(...)
46
+ detect_missing_order(:reject) if order_values.empty?
47
+ super(...)
48
+ end
49
+
50
+ def reject!(&block)
51
+ detect_missing_order(:reject!) if order_values.empty?
52
+ super(&block)
53
+ end
54
+
55
+ private
56
+
57
+ def detect_missing_order(method)
58
+ return if OrderCop.disabled?
59
+ level = 1.upto(binding.frame_count).find do |i|
60
+ lbinding = binding.of_caller(i)
61
+ lmethod = lbinding.eval("__method__")
62
+ next if lmethod.nil?
63
+ if OrderCop::WHITELIST.include?(lmethod)
64
+ puts "whitelisted #{lmethod}"
65
+ return nil
66
+ end
67
+ location = lbinding.source_location[0]
68
+ location.include?(Rails.root.to_s) && !location.include?(Rails.root.join("config").to_s)
69
+ end
70
+
71
+ if level
72
+ location = binding.of_caller(level - 1).source_location
73
+ file_path = location.first.gsub(Rails.root.to_s, "")
74
+ line_number = location.last
75
+ msg = "Missing Order for :#{method} at #{file_path}:#{line_number}"
76
+ else
77
+ msg = "Missing Order for :#{method}"
78
+ end
79
+
80
+ if OrderCop.rails_logger?
81
+ Rails.logger.error order_cop_red("Missing order for #{method}")
82
+ caller.each do |line|
83
+ Rails.logger.error order_cop_red(" #{line}") if line.include?(Rails.root.to_s)
84
+ end
85
+ end
86
+ if OrderCop.raise?
87
+ raise OrderCop::Error.new(msg)
88
+ end
89
+ end
90
+
91
+ def order_cop_red(msg)
92
+ ActiveSupport::LogSubscriber.new.send(:color, msg, :red)
93
+ end
94
+ end
95
+
96
+ class Error < StandardError; end
97
+
98
+ WHITELIST = %i[sum any? none? inspect method_missing not load_target reindex attachment attachments attributes_table with_current_arbre_element].freeze
99
+
100
+ def self.disable(&block)
101
+ old_enabled = @enabled
102
+ @enabled = false
103
+ yield
104
+ ensure
105
+ @enabled = old_enabled
106
+ end
107
+
108
+ def self.disabled?
109
+ @enabled == false
110
+ end
111
+
112
+ def self.enabled?
113
+ @enabled != false
114
+ end
115
+
116
+ def self.patch_active_record(app)
117
+ ActiveRecord::Base.descendants.each do |model|
118
+ model.const_get(:ActiveRecord_Associations_CollectionProxy).class_eval do
119
+ prepend OrderCopMixin
120
+ end
121
+ model.const_get(:ActiveRecord_Relation).class_eval do
122
+ prepend OrderCopMixin
123
+ end
124
+ model.const_get(:ActiveRecord_AssociationRelation).class_eval do
125
+ prepend OrderCopMixin
126
+ end
127
+ end
128
+ end
129
+
130
+ def self.rails_logger?
131
+ config.rails_logger
132
+ end
133
+
134
+ def self.raise?
135
+ config.raise
136
+ end
137
+
138
+ def self.config(**options)
139
+ @config ||= OpenStruct.new(rails_logger: false, raise: true, enabled: true)
140
+ options.each do |k, v|
141
+ @config[k] = v
142
+ end
143
+ @enabled = @config.enabled
144
+ @config
145
+ end
146
+
147
+ def self.setup(**options)
148
+ config(**options)
149
+ end
150
+
151
+ def self.apply(app = Rails.application)
152
+ patch_active_record(app) if enabled?
153
+ end
154
+
155
+ if defined?(Rails::Railtie)
156
+ class OrderCopRailtie < Rails::Railtie
157
+ initializer "ordercop.patch" do |app|
158
+ return if OrderCop.disabled?
159
+
160
+ app.config.to_prepare do
161
+ app&.eager_load!
162
+
163
+ OrderCop.apply(app)
164
+ end
165
+ end
166
+ end
167
+ end
168
+ end
169
+
170
+ ActiveSupport::Reloader.to_prepare do
171
+ OrderCop.apply(Rails.application)
172
+ end
173
+ # OrderCop.setup(rails_logger: true) unless Rails.env.production?
data/sig/order_cop.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module OrderCop
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: order_cop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Gauthier Monserand
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '7.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '7.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: binding_of_caller
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.0'
41
+ description: Bullet-like gem to detect missing order clauses
42
+ email:
43
+ - gauthier@squadracer.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".rspec"
49
+ - ".standard.yml"
50
+ - Gemfile
51
+ - Gemfile.lock
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/order_cop.rb
56
+ - lib/order_cop/version.rb
57
+ - sig/order_cop.rbs
58
+ homepage: https://github.com/squadracer/order_cop
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ homepage_uri: https://github.com/squadracer/order_cop
63
+ source_code_uri: https://github.com/squadracer/order_cop
64
+ changelog_uri: https://github.com/squadracer/order_cop
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: 2.6.0
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.4.10
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Bullet-like gem to detect missing order clauses
84
+ test_files: []