rails-flog 1.3.2 → 1.6.1

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
- SHA1:
3
- metadata.gz: 9fbf46665254484f9fb2fcf9d209b46a8303e028
4
- data.tar.gz: b2bdd9a21547bf84cbbbba6ebd7d54dc5baef4e1
2
+ SHA256:
3
+ metadata.gz: ce762dd557d5f1894942ea6e1c2ce4a8c368d270881fb6ef83a9565330613bde
4
+ data.tar.gz: 5df9080511dcc817399c3080fdf7278204986f280c54c3b27945cfa51a8be222
5
5
  SHA512:
6
- metadata.gz: d737e91d22762f40a7b0b08e844408f92e8c17879e985ec07555c5eda8608650042770e1f6c5ab1c54f023c258753a22efa95a7defca591a0e6e45b589155556
7
- data.tar.gz: 67d28e57171f04d269539e8405e874eff4c35e7ba80596a8e8484c03903a5ca987d3373f94a3b5ee8c36be7289dd40e78d0fee93c3ba8cfba546065e3c06d1ce
6
+ metadata.gz: b92c5ae0ae5aaaf554f5b94bfda53bf90cde1e9ff44f0a31510fdfa08fd289db89b3e531635d62247fe98f2cf6db9f1a0b3ebf2d8c43be5bd085fc1f4f95337b
7
+ data.tar.gz: 4d22a90f56f4ff337c08bb18bdfda1e5ab0a11f43f0dd82dc7d1a5ffcf973b3cdb82ace1db3d8fe8ca66a21f5dfa3d5f88b21209f8c2834e9a5261608ccc382d
data/.gitignore CHANGED
@@ -16,3 +16,5 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  vendor/bundle
19
+
20
+ .idea
@@ -0,0 +1,25 @@
1
+ # Current style is easy to confirm SQL.
2
+ Layout/SpaceBeforeComma:
3
+ Exclude:
4
+ - 'test/unit/sql_formattable_test.rb'
5
+
6
+ Metrics/AbcSize:
7
+ Exclude:
8
+ - 'test/**/*'
9
+
10
+ # 80 is too restrict.
11
+ Metrics/LineLength:
12
+ Max: 120
13
+
14
+ Metrics/MethodLength:
15
+ Exclude:
16
+ - 'test/**/*'
17
+
18
+ # This is test code not production.
19
+ Security/Eval:
20
+ Exclude:
21
+ - 'test/unit/params_formattable_test.rb'
22
+
23
+ # method having '?' suffix should return boolean, double negation is very useful.
24
+ Style/DoubleNegation:
25
+ Enabled: false
@@ -1,9 +1,28 @@
1
1
  language: ruby
2
+ sudo: false
3
+ branches:
4
+ only:
5
+ - master
2
6
  rvm:
3
- - 1.9.3
4
- - 2.0.0
5
- - 2.1.0
7
+ - 2.3
8
+ - 2.4
9
+ - 2.5
10
+ - 2.6
6
11
  gemfile:
7
- - gemfiles/rails_3_2_x.gemfile
8
- - gemfiles/rails_4_0_x.gemfile
9
- - gemfiles/rails_4_1_x.gemfile
12
+ - gemfiles/rails_4_2_x.gemfile
13
+ - gemfiles/rails_5_0_x.gemfile
14
+ - gemfiles/rails_5_1_x.gemfile
15
+ - gemfiles/rails_5_2_x.gemfile
16
+ - gemfiles/rails_6_0_x.gemfile
17
+ matrix:
18
+ exclude:
19
+ # FIXME: will remove on dropping Rails 4.2 support
20
+ - gemfile: gemfiles/rails_4_2_x.gemfile
21
+ rvm: 2.6
22
+ - gemfile: gemfiles/rails_6_0_x.gemfile
23
+ rvm: 2.3
24
+ - gemfile: gemfiles/rails_6_0_x.gemfile
25
+ rvm: 2.4
26
+ before_install:
27
+ - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
28
+ - gem install bundler -v '< 2'
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in rails-flog.gemspec
data/README.md CHANGED
@@ -101,6 +101,10 @@ Flog.configure do |config|
101
101
  config.params_key_count_threshold = 2
102
102
  # If this value is true, nested Hash parameter is formatted coercively in any situation
103
103
  config.force_on_nested_params = false
104
+ # If this value is true, not format query
105
+ config.ignore_query = true
106
+ # If this value is true, not format parameters
107
+ config.ignore_params = true
104
108
  end
105
109
  ```
106
110
 
@@ -112,6 +116,8 @@ end
112
116
  |query_duration_threshold |float |0.0 |
113
117
  |params_key_count_threshold |integer |1 |
114
118
  |force_on_nested_params |boolean |true |
119
+ |ignore_query |boolean |false |
120
+ |ignore_params |boolean |false |
115
121
 
116
122
  ## Disable temporary
117
123
 
@@ -126,8 +132,8 @@ Priority of this feature is higher than configurations.
126
132
 
127
133
  ## Supported versions
128
134
 
129
- - Ruby: 1.9.3, 2.0.0, 2.1.0
130
- - Rails: 3.2.x, 4.0.x, 4.1.x
135
+ - Ruby: 2.3.x, 2.4.x, 2.5.x, 2.6.x
136
+ - Rails: 4.2.x, 5.0.x, 5.1.x, 5.2.x, 6.0.x
131
137
 
132
138
  ## Contributing
133
139
 
@@ -146,3 +152,8 @@ Priority of this feature is higher than configurations.
146
152
  - v1.3.0 (2014-01-26 JST): Add configuration
147
153
  - v1.3.1 (2014-01-27 JST): Refactored
148
154
  - v1.3.2 (2014-04-28 JST): Confirm with Rails 4.1
155
+ - v1.3.3 (2015-11-16 JST): Confirm with Rails 4.2
156
+ - v1.4.0 (2017-10-01 JST): Compatible for Rails 5.0 and Rails 5.1, and dropped Ruby 1.9.3 support
157
+ - v1.5.0 (2018-12-19 JST): Add `sql_indent` and `sql_in_values_num` options, and dropped eol rails versions.
158
+ - v1.6.0 (2019-07-26 JST): Add `ignore_query` and `ignore_params` options (Thanks @mothule)
159
+ - v1.6.1 (2021-01-13 JST): Use amazing_print instead of awesome_print
data/Rakefile CHANGED
@@ -1,9 +1,11 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
1
+ # frozen_string_literal: true
3
2
 
4
- task :default => :test
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ task default: :test
5
7
 
6
8
  Rake::TestTask.new do |t|
7
- t.libs << "test"
8
- t.test_files = Dir.glob("test/unit/*_test.rb")
9
+ t.libs << 'test'
10
+ t.test_files = Dir.glob('test/unit/*_test.rb')
9
11
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'rails', '~> 4.2.0'
6
+ gem 'sqlite3', '~> 1.3.0'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'rails', '~>5.0.0'
6
+ gem 'sqlite3', '~> 1.3.0'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'rails', '~>5.1.0'
6
+ gem 'sqlite3', '~> 1.4.0'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'rails', '~>5.2.0'
6
+ gem 'sqlite3', '~> 1.4.0'
7
+
8
+ gemspec path: '../'
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gem 'rails', '~>6.0.0'
6
+ gem 'sqlite3', '~> 1.4.0'
7
+
8
+ gemspec path: '../'
@@ -1,7 +1,7 @@
1
- # coding: utf-8
2
- require "flog/version"
3
- require "flog/status"
4
- require "flog/configuration"
5
- require "flog/sql_formattable"
6
- require "flog/params_formattable"
1
+ # frozen_string_literal: true
7
2
 
3
+ require 'flog/version'
4
+ require 'flog/status'
5
+ require 'flog/configuration'
6
+ require 'flog/sql_formattable'
7
+ require 'flog/params_formattable'
@@ -1,30 +1,54 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
3
+ require 'anbt-sql-formatter/rule'
4
+
5
+ # Flog is root module of this gem
2
6
  module Flog
7
+ ONELINE_IN_VALUES_NUM = ::AnbtSql::Rule::ONELINE_IN_VALUES_NUM
8
+
9
+ # Configuration of this gem.
10
+ # Call `configure` to setup.
3
11
  class Configuration
4
- attr_writer :ignore_cached_query, :force_on_nested_params
5
- attr_accessor :query_duration_threshold, :params_key_count_threshold
12
+ attr_writer :ignore_cached_query, :force_on_nested_params, :ignore_query, :ignore_params
13
+ attr_accessor :query_duration_threshold, :params_key_count_threshold, :sql_indent, :sql_in_values_num
6
14
 
7
15
  def initialize
8
16
  @ignore_cached_query = true
9
17
  @query_duration_threshold = 0.0
10
18
  @params_key_count_threshold = 1
11
19
  @force_on_nested_params = true
20
+ @sql_indent = "\t"
21
+ @sql_in_values_num = 1
22
+ @ignore_query = false
23
+ @ignore_params = false
12
24
  end
13
25
 
14
26
  def ignore_cached_query?
15
- !!@ignore_cached_query
27
+ !!@ignore_cached_query || @ignore_query
16
28
  end
17
29
 
18
30
  def force_on_nested_params?
19
31
  !!@force_on_nested_params
20
32
  end
21
- end
22
33
 
23
- def self.config
24
- @@config ||= Flog::Configuration.new
34
+ def ignore_query?
35
+ !!@ignore_query
36
+ end
37
+
38
+ def ignore_params?
39
+ !!@ignore_params
40
+ end
25
41
  end
26
42
 
27
- def self.configure
28
- yield(config) if block_given?
43
+ class << self
44
+ def config
45
+ @config ||= Flog::Configuration.new
46
+ end
47
+
48
+ def configure
49
+ cfg = Flog::Configuration.new
50
+ yield(cfg) if block_given?
51
+ @config = cfg
52
+ end
29
53
  end
30
54
  end
@@ -1,60 +1,70 @@
1
- # coding: utf-8
2
- require "action_controller/log_subscriber"
3
- require "awesome_print"
4
- require "flog/payload_value_shuntable"
1
+ # frozen_string_literal: true
5
2
 
6
- class ActionController::LogSubscriber
7
- include Flog::PayloadValueShuntable
3
+ require 'action_controller/log_subscriber'
4
+ require 'amazing_print'
5
+ require 'flog/payload_value_shuntable'
8
6
 
9
- def start_processing_with_flog(event)
10
- return start_processing_without_flog(event) unless formattable?(event)
11
-
12
- replaced = replace_params(event.payload[:params])
7
+ module Flog
8
+ # Overrides `inspect` method for formatting itself
9
+ module ParamsInspectOverridable
10
+ def inspect
11
+ "\n#{ai(plain: !ActionController::LogSubscriber.colorize_logging)}"
12
+ end
13
+ end
13
14
 
14
- shunt_payload_value(event.payload, :params, replaced) do
15
- start_processing_without_flog(event)
15
+ # Overrides `except` method for formatting ecepted params.
16
+ module ParamsExceptOverridable
17
+ def except(*keys)
18
+ excepted = super(*keys)
19
+ excepted.singleton_class.prepend(ParamsInspectOverridable)
20
+ excepted
16
21
  end
17
22
  end
18
- alias_method_chain :start_processing, :flog
19
-
20
- private
21
- def replace_params(params)
22
- return params if params.empty? || !params.respond_to?(:ai)
23
-
24
- replaced = params.dup
25
- class << replaced
26
- alias :original_except :except
27
-
28
- def except(*keys)
29
- excepted = original_except(*keys)
30
- class << excepted
31
- def inspect
32
- "\n#{ai(plain: !ActionController::LogSubscriber.colorize_logging)}"
33
- end
34
- end
35
- excepted
23
+
24
+ # ParamsFormattable enables to format request parameters in log.
25
+ module ParamsFormattable
26
+ include Flog::PayloadValueShuntable
27
+ def start_processing(event)
28
+ return super(event) unless formattable?(event)
29
+
30
+ replaced = replace_params(event.payload[:params])
31
+
32
+ shunt_payload_value(event.payload, :params, replaced) do
33
+ super(event)
36
34
  end
37
35
  end
38
- replaced
39
- end
40
36
 
41
- def formattable?(event)
42
- return false unless Flog::Status.params_formattable?
37
+ private
43
38
 
44
- return true if force_format_by_nested_params?(event)
39
+ def replace_params(params)
40
+ return params if params.empty? || !params.respond_to?(:ai)
45
41
 
46
- key_count_over?(event)
47
- end
42
+ replaced = params.dup
43
+ replaced.singleton_class.prepend(ParamsExceptOverridable)
44
+ replaced
45
+ end
48
46
 
49
- def force_format_by_nested_params?(event)
50
- return false unless Flog.config.force_on_nested_params?
47
+ def formattable?(event)
48
+ return false if Flog.config.ignore_params?
49
+ return false unless Flog::Status.params_formattable?
51
50
 
52
- event.payload[:params].values.any? { |value| value.is_a?(Hash) }
53
- end
51
+ return true if force_format_by_nested_params?(event)
52
+
53
+ key_count_over?(event)
54
+ end
55
+
56
+ def force_format_by_nested_params?(event)
57
+ return false unless Flog.config.force_on_nested_params?
54
58
 
55
- def key_count_over?(event)
56
- threshold = Flog.config.params_key_count_threshold.to_i
57
- params = event.payload[:params].except(*INTERNAL_PARAMS)
58
- params.keys.size > threshold
59
+ event.payload[:params].values.any? { |value| value.is_a?(Hash) }
60
+ end
61
+
62
+ def key_count_over?(event)
63
+ threshold = Flog.config.params_key_count_threshold.to_i
64
+ params = event.payload[:params].except(*ActionController::LogSubscriber::INTERNAL_PARAMS)
65
+ params.keys.size > threshold
66
+ end
59
67
  end
60
68
  end
69
+
70
+ ActionController::LogSubscriber.prepend(Flog::ParamsFormattable)
@@ -1,21 +1,33 @@
1
- # coding: utf-8
1
+ # frozen_string_literal: true
2
+
2
3
  module Flog
4
+ # PayloadValueShuntable enables to shunt value in payload
3
5
  module PayloadValueShuntable
4
6
  def shunt_payload_value(payload, key, temp_value, &block)
5
7
  return unless block
6
8
 
7
- key_exists = payload.key?(key)
9
+ if payload.key?(key)
10
+ shunt_if_key_already_exists(payload, key, temp_value, block)
11
+ else
12
+ shunt_if_key_not_exist(payload, key, temp_value, block)
13
+ end
14
+ end
15
+
16
+ def shunt_if_key_already_exists(payload, key, temp_value, block)
8
17
  base_value = payload[key]
9
18
  begin
10
19
  payload[key] = temp_value
11
20
  block.call
12
21
  ensure
13
- if key_exists
14
- payload[key] = base_value
15
- else
16
- payload.delete(key)
17
- end
22
+ payload[key] = base_value
18
23
  end
19
24
  end
25
+
26
+ def shunt_if_key_not_exist(payload, key, temp_value, block)
27
+ payload[key] = temp_value
28
+ block.call
29
+ ensure
30
+ payload.delete(key)
31
+ end
20
32
  end
21
33
  end
@@ -1,48 +1,56 @@
1
- # coding: utf-8
2
- require "active_record/log_subscriber"
3
- require "flog/payload_value_shuntable"
1
+ # frozen_string_literal: true
4
2
 
5
- class ActiveRecord::LogSubscriber
6
- include Flog::PayloadValueShuntable
3
+ require 'active_record/log_subscriber'
4
+ require 'flog/payload_value_shuntable'
7
5
 
8
- def sql_with_flog(event)
9
- return sql_without_flog(event) unless formattable?(event)
6
+ module Flog
7
+ # SqlFormattable enables to format SQL log
8
+ module SqlFormattable
9
+ include Flog::PayloadValueShuntable
10
10
 
11
- formatted = format_sql(event.payload[:sql])
11
+ def sql(event)
12
+ return super(event) unless formattable?(event)
12
13
 
13
- shunt_payload_value(event.payload, :sql, "\n\t#{formatted}") do
14
- sql_without_flog(event)
14
+ formatted = format_sql(event.payload[:sql])
15
+
16
+ shunt_payload_value(event.payload, :sql, "\n#{Flog.config.sql_indent}#{formatted}") do
17
+ super(event)
18
+ end
15
19
  end
16
- end
17
- alias_method_chain :sql, :flog
18
20
 
19
- private
20
- def format_sql(sql)
21
- return sql if sql.blank?
21
+ private
22
+
23
+ def format_sql(sql)
24
+ return sql if sql.blank?
22
25
 
23
- require "anbt-sql-formatter/formatter"
24
- rule = AnbtSql::Rule.new
25
- rule.keyword = AnbtSql::Rule::KEYWORD_UPPER_CASE
26
- %w(count sum).each do |function_name|
27
- rule.function_names << function_name
26
+ require 'anbt-sql-formatter/formatter'
27
+ rule = AnbtSql::Rule.new
28
+ rule.keyword = AnbtSql::Rule::KEYWORD_UPPER_CASE
29
+ rule.indent_string = Flog.config.sql_indent
30
+ rule.in_values_num = Flog.config.sql_in_values_num
31
+ %w[count sum].each do |function_name|
32
+ rule.function_names << function_name
33
+ end
34
+ AnbtSql::Formatter.new(rule).format(sql.squeeze(' '))
28
35
  end
29
- rule.indent_string = "\t"
30
- AnbtSql::Formatter.new(rule).format(sql.squeeze(" "))
31
- end
32
36
 
33
- def formattable?(event)
34
- return false unless Flog::Status.sql_formattable?
37
+ def formattable?(event)
38
+ return false if Flog.config.ignore_query?
39
+ return false unless Flog::Status.sql_formattable?
35
40
 
36
- return false if ignore_by_cached_query?(event)
41
+ return false if ignore_by_cached_query?(event)
37
42
 
38
- duration_over?(event)
39
- end
43
+ duration_over?(event)
44
+ end
40
45
 
41
- def ignore_by_cached_query?(event)
42
- event.payload[:name] == "CACHE" && Flog.config.ignore_cached_query?
43
- end
46
+ def ignore_by_cached_query?(event)
47
+ (event.payload[:name] == 'CACHE' || event.payload[:cached]) && Flog.config.ignore_cached_query?
48
+ end
44
49
 
45
- def duration_over?(event)
46
- event.duration >= Flog.config.query_duration_threshold.to_f
50
+ def duration_over?(event)
51
+ event.duration >= Flog.config.query_duration_threshold.to_f
52
+ end
47
53
  end
48
54
  end
55
+
56
+ ActiveRecord::LogSubscriber.prepend(Flog::SqlFormattable)