order_cop 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1d24dd6bfe948fbc5927f0e49ca32795df0e68d41d2103eddf0779870118df7f
4
- data.tar.gz: f3991ed96ead7fd3286bd92224532947a504a716712bb04c43c5cfd534679eed
3
+ metadata.gz: b8edfc0b999f13867cc3c91455d6054bd4ae551bad96b46035bac8942229efc7
4
+ data.tar.gz: cfbe8f395ff23161ae2dee5836ab8f608829c735cc47d8aacf30360b2336b546
5
5
  SHA512:
6
- metadata.gz: e04ad1f84754c6df5ca457af7c40cff47a3243deecc9dd4975813e3de66b7fd5f6f532d7779a889bbb6488f0814b63e4d21826cfd68a79b082f25846272944ba
7
- data.tar.gz: 7ad6da6dfe4278af541afbe369b102d0ca72f8f06d9af0fe466a5bed4c7c00d6d9084ca79db7d2ccd7183e07db4848a4033b77e3e04cbfa0dc600a6f5e0e369e
6
+ metadata.gz: d862643e34b5e836066c651ef155da4dd0dc91c21d0586346733697da9a09c47422ef20ede4b90740cce125cccdd599a2c76c0daa217c9507fbff57efd8a22fb
7
+ data.tar.gz: 231ce43da723ff0eb183be69e4c229bef0215c5bf9dd4292f5f44446145fa5b106dc8297c5ae0abaee1c16e3e3056c3576fa0bace94ff3722579185e526f51e9
data/Gemfile CHANGED
@@ -14,3 +14,5 @@ gem "standard", "~> 1.3"
14
14
  gem "rails", "~> 7.0.0"
15
15
 
16
16
  gem "sqlite3"
17
+
18
+ gem "simplecov"
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- order_cop (0.2.2)
4
+ order_cop (0.2.3)
5
5
  binding_of_caller (~> 1.0)
6
6
  rails (~> 7.0)
7
7
 
@@ -82,6 +82,7 @@ GEM
82
82
  date (3.3.3)
83
83
  debug_inspector (1.1.0)
84
84
  diff-lcs (1.5.0)
85
+ docile (1.4.0)
85
86
  erubi (1.12.0)
86
87
  globalid (1.1.0)
87
88
  activesupport (>= 5.0)
@@ -183,6 +184,12 @@ GEM
183
184
  rubocop (>= 1.7.0, < 2.0)
184
185
  rubocop-ast (>= 0.4.0)
185
186
  ruby-progressbar (1.13.0)
187
+ simplecov (0.22.0)
188
+ docile (~> 1.1)
189
+ simplecov-html (~> 0.11)
190
+ simplecov_json_formatter (~> 0.1)
191
+ simplecov-html (0.12.3)
192
+ simplecov_json_formatter (0.1.4)
186
193
  sqlite3 (1.6.3-x86_64-linux)
187
194
  standard (1.30.1)
188
195
  language_server-protocol (~> 3.17.0.2)
@@ -214,6 +221,7 @@ DEPENDENCIES
214
221
  rails (~> 7.0.0)
215
222
  rake (~> 13.0)
216
223
  rspec (~> 3.0)
224
+ simplecov
217
225
  sqlite3
218
226
  standard (~> 1.3)
219
227
 
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/generators"
4
+
5
+ module OrderCop
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path("../../templates", __FILE__)
8
+ desc "Install OrderCop in your Ruby on Rails application"
9
+
10
+ def add_initializer
11
+ template "order_cop.rb.erb", "config/initializers/order_cop.rb"
12
+ end
13
+ end
14
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OrderCop
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.3"
5
5
  end
data/lib/order_cop.rb CHANGED
@@ -10,6 +10,7 @@ require "binding_of_caller"
10
10
  require "zeitwerk"
11
11
  loader = Zeitwerk::Loader.for_gem
12
12
  loader.setup
13
+
13
14
  module OrderCop
14
15
  class OrderCopConfig
15
16
  def initialize
@@ -18,8 +19,17 @@ module OrderCop
18
19
  @debug = false
19
20
  @rails_logger = false
20
21
  @whitelist_methods = %i[sum any? none? inspect method_missing not load_target reindex attachment attachments attributes_table with_current_arbre_element]
22
+ @only_view = false
23
+ @view_paths = [
24
+ /app\/views/,
25
+ /app\/helpers/,
26
+ /\.erb/,
27
+ /\.haml/,
28
+ /\.slim/,
29
+ /\(erb\)/
30
+ ].freeze
21
31
  end
22
- attr_accessor :enabled, :raise, :debug, :rails_logger, :whitelist_methods
32
+ attr_accessor :enabled, :raise, :debug, :rails_logger, :whitelist_methods, :only_view, :view_paths
23
33
  end
24
34
 
25
35
  def self.config(**options, &block)
@@ -104,12 +114,26 @@ module OrderCop
104
114
  false
105
115
  end
106
116
 
117
+ def stack_in_view?
118
+ 1.upto(binding.frame_count).each do |i|
119
+ lbinding = binding.of_caller(i)
120
+ location = lbinding.source_location[0]
121
+ puts "location: #{location}" if OrderCop.config.debug
122
+ if OrderCop.config.view_paths.any? { location.match(_1) }
123
+ puts "#{location} is in view" if OrderCop.config.debug
124
+ return true
125
+ end
126
+ end
127
+ false
128
+ end
129
+
107
130
  def detect_missing_order(method)
108
131
  return if OrderCop.disabled?
109
132
  puts "missing order, detect if allowed" if OrderCop.config.debug
110
133
  puts "stack size: #{binding.frame_count}" if OrderCop.config.debug
111
134
 
112
135
  return if stack_is_whitelisted?
136
+ return if OrderCop.config.only_view && !stack_in_view?
113
137
 
114
138
  level = 1.upto(binding.frame_count).find do |i|
115
139
  lbinding = binding.of_caller(i)
@@ -161,6 +185,8 @@ module OrderCop
161
185
  end
162
186
 
163
187
  if defined?(Rails::Railtie)
188
+ require_relative "order_cop/install_generator"
189
+
164
190
  class OrderCopRailtie < Rails::Railtie
165
191
  initializer "ordercop.patch" do |app|
166
192
  return if OrderCop.disabled?
@@ -0,0 +1,21 @@
1
+ if defined?(OrderCop)
2
+ OrderCop.config do |config|
3
+ # Enable or disable OrderCop (defaults to true)
4
+ # config.enabled = true
5
+
6
+ # Raise an error when a query is not ordered (defaults to true)
7
+ # config.raise = true
8
+
9
+ # Log missing order queries to Rails.logger (defaults to false)
10
+ # config.rails_logger = false
11
+
12
+ # Only raise if the query is in a view (defaults to false)
13
+ # config.only_view = false
14
+
15
+ # Whitelist methods that don't need to be ordered (defaults to <%= OrderCop.config.whitelist_methods %>)
16
+ # config.whitelist_methods = []
17
+
18
+ # Whitelist views that don't need to be ordered (defaults to <%= OrderCop.config.view_paths %>)
19
+ # config.view_paths = []
20
+ end
21
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: order_cop
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gauthier Monserand
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-20 00:00:00.000000000 Z
11
+ date: 2023-08-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -53,7 +53,9 @@ files:
53
53
  - README.md
54
54
  - Rakefile
55
55
  - lib/order_cop.rb
56
+ - lib/order_cop/install_generator.rb
56
57
  - lib/order_cop/version.rb
58
+ - lib/templates/order_cop.rb.erb
57
59
  - sig/order_cop.rbs
58
60
  homepage: https://github.com/squadracer/order_cop
59
61
  licenses: