rubocop-rails 2.11.2 → 2.11.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: 70f7044795da60c3d19962181ad6e9bc1914c1ae9c46f5072f4c2bc9c6115add
4
- data.tar.gz: d1130053b86d468d25b114694dca3d56eea533aafdbff9ff8dc33f3fdc306fbb
3
+ metadata.gz: afc20a9d5f0799fab1f02a5a05c3b297a47b1713f6b948a25511da46b2e5a332
4
+ data.tar.gz: d71033e04f2cd0b1f7801d1ab80d99859af6e3d1bfba9551dec1d18a0e7b40dc
5
5
  SHA512:
6
- metadata.gz: 49d18f985b54c2a8beefe8f00ad758b2cabed80d463315e10c994eeb348af4f1e6db1f53ac7a73434105d210db831b98ddb8f5ae44e849346888ff427c4eabcc
7
- data.tar.gz: 291ad9f2cafe1c8ce146bf9e311e5bf84c667e087596050076d9fa560ef0612c41a425beb8d314386f98e4f2eeb4f31bdb50f134eebad52b44efca82ea4299ba
6
+ metadata.gz: eab328cf38c0f4957b79c3722175edd1a259af69f6c7c4a4501bc72a245b70ef3d4290b4daf4ef6925bf643dcffb1370a50ccfc5e7e870e138c3df675598df54
7
+ data.tar.gz: 7fb02325ab085ed0d716718dc02da92b39da5710b117b49a722f8f0d4de4d2e47648278a39aff455b81d2186b4914eed7f797fbdfd86d2ff4ee570dc20543309
@@ -57,12 +57,13 @@ module RuboCop
57
57
  # Resolve relation into column name.
58
58
  # It just returns column_name if the column exists.
59
59
  # Or it tries to resolve column_name as a relation.
60
+ # Returns an array of column names if the relation is polymorphic.
60
61
  # It returns `nil` if it can't resolve.
61
62
  #
62
63
  # @param name [String]
63
64
  # @param class_node [RuboCop::AST::Node]
64
65
  # @param table [RuboCop::Rails::SchemaLoader::Table]
65
- # @return [String, nil]
66
+ # @return [Array, String, nil]
66
67
  def resolve_relation_into_column(name:, class_node:, table:)
67
68
  return unless table
68
69
  return name if table.with_column?(name: name)
@@ -71,7 +72,9 @@ module RuboCop
71
72
  next unless belongs_to.first_argument.value.to_s == name
72
73
 
73
74
  fk = foreign_key_of(belongs_to) || "#{name}_id"
74
- return fk if table.with_column?(name: fk)
75
+ next unless table.with_column?(name: fk)
76
+
77
+ return polymorphic?(belongs_to) ? [fk, "#{name}_type"] : fk
75
78
  end
76
79
  nil
77
80
  end
@@ -88,6 +91,15 @@ module RuboCop
88
91
  end
89
92
  end
90
93
 
94
+ def polymorphic?(belongs_to)
95
+ options = belongs_to.last_argument
96
+ return false unless options.hash_type?
97
+
98
+ options.each_pair.any? do |pair|
99
+ pair.key.sym_type? && pair.key.value == :polymorphic && pair.value.true_type?
100
+ end
101
+ end
102
+
91
103
  def in_where?(node)
92
104
  send_node = node.each_ancestor(:send).first
93
105
  send_node && WHERE_METHODS.include?(send_node.method_name)
@@ -71,7 +71,7 @@ module RuboCop
71
71
 
72
72
  # Autocorrect by swapping between two nodes autocorrecting them
73
73
  def autocorrect(corrector, node)
74
- previous = left_siblings_of(node).reverse_each.find do |sibling|
74
+ previous = node.left_siblings.reverse_each.find do |sibling|
75
75
  callback?(sibling)
76
76
  end
77
77
 
@@ -96,14 +96,6 @@ module RuboCop
96
96
  node.send_type? && CALLBACKS_ORDER_MAP.key?(node.method_name)
97
97
  end
98
98
 
99
- def left_siblings_of(node)
100
- siblings_of(node)[0, node.sibling_index]
101
- end
102
-
103
- def siblings_of(node)
104
- node.parent.children
105
- end
106
-
107
99
  def source_range_with_comment(node)
108
100
  begin_pos = begin_pos_with_comment(node)
109
101
  end_pos = end_position_for(node)
@@ -56,8 +56,8 @@ module RuboCop
56
56
 
57
57
  def register_offense(allow_nil, message)
58
58
  add_offense(allow_nil, message: message) do |corrector|
59
- prv_sib = previous_sibling(allow_nil)
60
- nxt_sib = next_sibling(allow_nil)
59
+ prv_sib = allow_nil.left_sibling
60
+ nxt_sib = allow_nil.right_sibling
61
61
 
62
62
  if nxt_sib
63
63
  corrector.remove(range_between(node_beg(allow_nil), node_beg(nxt_sib)))
@@ -88,14 +88,6 @@ module RuboCop
88
88
  nil
89
89
  end
90
90
 
91
- def previous_sibling(node)
92
- node.parent.children[node.sibling_index - 1]
93
- end
94
-
95
- def next_sibling(node)
96
- node.parent.children[node.sibling_index + 1]
97
- end
98
-
99
91
  def node_beg(node)
100
92
  node.loc.expression.begin_pos
101
93
  end
@@ -81,7 +81,7 @@ module RuboCop
81
81
  names_from_scope = column_names_from_scope(node)
82
82
  ret.concat(names_from_scope) if names_from_scope
83
83
 
84
- ret.map! do |name|
84
+ ret = ret.flat_map do |name|
85
85
  klass = class_node(node)
86
86
  resolve_relation_into_column(
87
87
  name: name.to_s,
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Rails
5
5
  # This module holds the RuboCop Rails version information.
6
6
  module Version
7
- STRING = '2.11.2'
7
+ STRING = '2.11.3'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.11.2
4
+ version: 2.11.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-07-01 00:00:00.000000000 Z
13
+ date: 2021-07-11 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport