rubocop-rails 2.19.1 → 2.30.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +70 -16
- data/config/default.yml +173 -28
- data/lib/rubocop/cop/mixin/active_record_helper.rb +16 -4
- data/lib/rubocop/cop/mixin/active_record_migrations_helper.rb +2 -2
- data/lib/rubocop/cop/mixin/database_type_resolvable.rb +66 -0
- data/lib/rubocop/cop/mixin/index_method.rb +68 -61
- data/lib/rubocop/cop/mixin/routes_helper.rb +20 -0
- data/lib/rubocop/cop/mixin/target_rails_version.rb +27 -2
- data/lib/rubocop/cop/rails/action_controller_flash_before_render.rb +3 -1
- data/lib/rubocop/cop/rails/action_controller_test_case.rb +2 -2
- data/lib/rubocop/cop/rails/action_filter.rb +3 -0
- data/lib/rubocop/cop/rails/action_order.rb +1 -5
- data/lib/rubocop/cop/rails/active_record_aliases.rb +2 -2
- data/lib/rubocop/cop/rails/active_record_callbacks_order.rb +1 -5
- data/lib/rubocop/cop/rails/active_support_aliases.rb +6 -5
- data/lib/rubocop/cop/rails/active_support_on_load.rb +21 -1
- data/lib/rubocop/cop/rails/add_column_index.rb +1 -0
- data/lib/rubocop/cop/rails/after_commit_override.rb +1 -1
- data/lib/rubocop/cop/rails/application_record.rb +4 -0
- data/lib/rubocop/cop/rails/assert_not.rb +0 -1
- data/lib/rubocop/cop/rails/belongs_to.rb +1 -1
- data/lib/rubocop/cop/rails/blank.rb +1 -1
- data/lib/rubocop/cop/rails/bulk_change_table.rb +19 -45
- data/lib/rubocop/cop/rails/compact_blank.rb +29 -8
- data/lib/rubocop/cop/rails/content_tag.rb +2 -2
- data/lib/rubocop/cop/rails/dangerous_column_names.rb +448 -0
- data/lib/rubocop/cop/rails/date.rb +14 -5
- data/lib/rubocop/cop/rails/delegate.rb +53 -7
- data/lib/rubocop/cop/rails/duplicate_association.rb +71 -10
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +3 -3
- data/lib/rubocop/cop/rails/eager_evaluation_log_message.rb +2 -2
- data/lib/rubocop/cop/rails/enum_hash.rb +31 -8
- data/lib/rubocop/cop/rails/enum_syntax.rb +130 -0
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +29 -7
- data/lib/rubocop/cop/rails/env_local.rb +69 -0
- data/lib/rubocop/cop/rails/expanded_date_range.rb +1 -1
- data/lib/rubocop/cop/rails/file_path.rb +186 -18
- data/lib/rubocop/cop/rails/find_by.rb +3 -3
- data/lib/rubocop/cop/rails/find_by_id.rb +9 -23
- data/lib/rubocop/cop/rails/find_each.rb +1 -1
- data/lib/rubocop/cop/rails/freeze_time.rb +1 -1
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +1 -1
- data/lib/rubocop/cop/rails/helper_instance_variable.rb +1 -1
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +7 -0
- data/lib/rubocop/cop/rails/http_status.rb +16 -5
- data/lib/rubocop/cop/rails/i18n_lazy_lookup.rb +63 -13
- data/lib/rubocop/cop/rails/i18n_locale_texts.rb +5 -1
- data/lib/rubocop/cop/rails/ignored_skip_action_filter_option.rb +23 -3
- data/lib/rubocop/cop/rails/index_by.rb +28 -12
- data/lib/rubocop/cop/rails/index_with.rb +28 -12
- data/lib/rubocop/cop/rails/inquiry.rb +2 -1
- data/lib/rubocop/cop/rails/inverse_of.rb +1 -1
- data/lib/rubocop/cop/rails/lexically_scoped_action_filter.rb +19 -10
- data/lib/rubocop/cop/rails/link_to_blank.rb +2 -2
- data/lib/rubocop/cop/rails/match_route.rb +1 -9
- data/lib/rubocop/cop/rails/multiple_route_paths.rb +50 -0
- data/lib/rubocop/cop/rails/not_null_column.rb +100 -6
- data/lib/rubocop/cop/rails/output.rb +3 -2
- data/lib/rubocop/cop/rails/pick.rb +10 -5
- data/lib/rubocop/cop/rails/pluck.rb +21 -1
- data/lib/rubocop/cop/rails/pluck_id.rb +2 -1
- data/lib/rubocop/cop/rails/pluck_in_where.rb +35 -13
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +30 -16
- data/lib/rubocop/cop/rails/presence.rb +1 -1
- data/lib/rubocop/cop/rails/present.rb +1 -3
- data/lib/rubocop/cop/rails/rake_environment.rb +22 -6
- data/lib/rubocop/cop/rails/redundant_active_record_all_method.rb +190 -0
- data/lib/rubocop/cop/rails/redundant_foreign_key.rb +1 -1
- data/lib/rubocop/cop/rails/redundant_presence_validation_on_belongs_to.rb +16 -0
- data/lib/rubocop/cop/rails/redundant_receiver_in_with_options.rb +2 -2
- data/lib/rubocop/cop/rails/reflection_class_name.rb +2 -2
- data/lib/rubocop/cop/rails/refute_methods.rb +0 -1
- data/lib/rubocop/cop/rails/relative_date_constant.rb +1 -1
- data/lib/rubocop/cop/rails/render_plain_text.rb +6 -3
- data/lib/rubocop/cop/rails/request_referer.rb +1 -1
- data/lib/rubocop/cop/rails/response_parsed_body.rb +52 -10
- data/lib/rubocop/cop/rails/reversible_migration.rb +7 -5
- data/lib/rubocop/cop/rails/root_pathname_methods.rb +58 -15
- data/lib/rubocop/cop/rails/save_bang.rb +22 -14
- data/lib/rubocop/cop/rails/schema_comment.rb +17 -10
- data/lib/rubocop/cop/rails/select_map.rb +79 -0
- data/lib/rubocop/cop/rails/skips_model_validations.rb +9 -4
- data/lib/rubocop/cop/rails/squished_sql_heredocs.rb +1 -2
- data/lib/rubocop/cop/rails/strip_heredoc.rb +1 -1
- data/lib/rubocop/cop/rails/strong_parameters_expect.rb +104 -0
- data/lib/rubocop/cop/rails/three_state_boolean_column.rb +4 -5
- data/lib/rubocop/cop/rails/time_zone.rb +26 -11
- data/lib/rubocop/cop/rails/transaction_exit_statement.rb +40 -9
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +11 -26
- data/lib/rubocop/cop/rails/unique_validation_without_index.rb +17 -21
- data/lib/rubocop/cop/rails/unknown_env.rb +5 -1
- data/lib/rubocop/cop/rails/unused_ignored_columns.rb +6 -0
- data/lib/rubocop/cop/rails/unused_render_content.rb +67 -0
- data/lib/rubocop/cop/rails/validation.rb +9 -4
- data/lib/rubocop/cop/rails/where_equals.rb +29 -12
- data/lib/rubocop/cop/rails/where_exists.rb +9 -9
- data/lib/rubocop/cop/rails/where_missing.rb +6 -2
- data/lib/rubocop/cop/rails/where_not.rb +18 -11
- data/lib/rubocop/cop/rails/where_range.rb +203 -0
- data/lib/rubocop/cop/rails_cops.rb +11 -0
- data/lib/rubocop/rails/migration_file_skippable.rb +54 -0
- data/lib/rubocop/rails/plugin.rb +48 -0
- data/lib/rubocop/rails/schema_loader/schema.rb +8 -7
- data/lib/rubocop/rails/schema_loader.rb +5 -15
- data/lib/rubocop/rails/version.rb +1 -1
- data/lib/rubocop/rails.rb +1 -8
- data/lib/rubocop-rails.rb +12 -4
- metadata +55 -11
- data/lib/rubocop/rails/inject.rb +0 -18
@@ -16,7 +16,6 @@ module RuboCop
|
|
16
16
|
# And `compact_blank!` has different implementations for `Array`, `Hash`, and
|
17
17
|
# `ActionController::Parameters`.
|
18
18
|
# `Array#compact_blank!`, `Hash#compact_blank!` are equivalent to `delete_if(&:blank?)`.
|
19
|
-
# `ActionController::Parameters#compact_blank!` is equivalent to `reject!(&:blank?)`.
|
20
19
|
# If the cop makes a mistake, autocorrected code may get unexpected behavior.
|
21
20
|
#
|
22
21
|
# @example
|
@@ -24,6 +23,10 @@ module RuboCop
|
|
24
23
|
# # bad
|
25
24
|
# collection.reject(&:blank?)
|
26
25
|
# collection.reject { |_k, v| v.blank? }
|
26
|
+
# collection.select(&:present?)
|
27
|
+
# collection.select { |_k, v| v.present? }
|
28
|
+
# collection.filter(&:present?)
|
29
|
+
# collection.filter { |_k, v| v.present? }
|
27
30
|
#
|
28
31
|
# # good
|
29
32
|
# collection.compact_blank
|
@@ -31,8 +34,8 @@ module RuboCop
|
|
31
34
|
# # bad
|
32
35
|
# collection.delete_if(&:blank?) # Same behavior as `Array#compact_blank!` and `Hash#compact_blank!`
|
33
36
|
# collection.delete_if { |_k, v| v.blank? } # Same behavior as `Array#compact_blank!` and `Hash#compact_blank!`
|
34
|
-
# collection.
|
35
|
-
# collection.
|
37
|
+
# collection.keep_if(&:present?) # Same behavior as `Array#compact_blank!` and `Hash#compact_blank!`
|
38
|
+
# collection.keep_if { |_k, v| v.present? } # Same behavior as `Array#compact_blank!` and `Hash#compact_blank!`
|
36
39
|
#
|
37
40
|
# # good
|
38
41
|
# collection.compact_blank!
|
@@ -43,25 +46,41 @@ module RuboCop
|
|
43
46
|
extend TargetRailsVersion
|
44
47
|
|
45
48
|
MSG = 'Use `%<preferred_method>s` instead.'
|
46
|
-
RESTRICT_ON_SEND = %i[reject delete_if
|
49
|
+
RESTRICT_ON_SEND = %i[reject delete_if select filter keep_if].freeze
|
50
|
+
DESTRUCTIVE_METHODS = %i[delete_if keep_if].freeze
|
47
51
|
|
48
52
|
minimum_target_rails_version 6.1
|
49
53
|
|
50
54
|
def_node_matcher :reject_with_block?, <<~PATTERN
|
51
55
|
(block
|
52
|
-
(send _ {:reject :delete_if
|
56
|
+
(send _ {:reject :delete_if})
|
53
57
|
$(args ...)
|
54
58
|
(send
|
55
59
|
$(lvar _) :blank?))
|
56
60
|
PATTERN
|
57
61
|
|
58
62
|
def_node_matcher :reject_with_block_pass?, <<~PATTERN
|
59
|
-
(send _ {:reject :delete_if
|
63
|
+
(send _ {:reject :delete_if}
|
60
64
|
(block_pass
|
61
65
|
(sym :blank?)))
|
62
66
|
PATTERN
|
63
67
|
|
68
|
+
def_node_matcher :select_with_block?, <<~PATTERN
|
69
|
+
(block
|
70
|
+
(send _ {:select :filter :keep_if})
|
71
|
+
$(args ...)
|
72
|
+
(send
|
73
|
+
$(lvar _) :present?))
|
74
|
+
PATTERN
|
75
|
+
|
76
|
+
def_node_matcher :select_with_block_pass?, <<~PATTERN
|
77
|
+
(send _ {:select :filter :keep_if}
|
78
|
+
(block-pass
|
79
|
+
(sym :present?)))
|
80
|
+
PATTERN
|
81
|
+
|
64
82
|
def on_send(node)
|
83
|
+
return if target_ruby_version < 2.6 && node.method?(:filter)
|
65
84
|
return unless bad_method?(node)
|
66
85
|
|
67
86
|
range = offense_range(node)
|
@@ -75,8 +94,10 @@ module RuboCop
|
|
75
94
|
|
76
95
|
def bad_method?(node)
|
77
96
|
return true if reject_with_block_pass?(node)
|
97
|
+
return true if select_with_block_pass?(node)
|
78
98
|
|
79
|
-
|
99
|
+
arguments, receiver_in_block = reject_with_block?(node.parent) || select_with_block?(node.parent)
|
100
|
+
if arguments
|
80
101
|
return use_single_value_block_argument?(arguments, receiver_in_block) ||
|
81
102
|
use_hash_value_block_argument?(arguments, receiver_in_block)
|
82
103
|
end
|
@@ -103,7 +124,7 @@ module RuboCop
|
|
103
124
|
end
|
104
125
|
|
105
126
|
def preferred_method(node)
|
106
|
-
|
127
|
+
DESTRUCTIVE_METHODS.include?(node.method_name) ? 'compact_blank!' : 'compact_blank'
|
107
128
|
end
|
108
129
|
end
|
109
130
|
end
|
@@ -7,7 +7,7 @@ module RuboCop
|
|
7
7
|
#
|
8
8
|
# NOTE: Allow `tag` when the first argument is a variable because
|
9
9
|
# `tag(name)` is simpler rather than `tag.public_send(name)`.
|
10
|
-
# And this cop will be renamed to something like `LegacyTag` in the future. (e.g. RuboCop Rails
|
10
|
+
# And this cop will be renamed to something like `LegacyTag` in the future. (e.g. RuboCop Rails 3.0)
|
11
11
|
#
|
12
12
|
# @example
|
13
13
|
# # bad
|
@@ -79,7 +79,7 @@ module RuboCop
|
|
79
79
|
end
|
80
80
|
|
81
81
|
def allowed_name?(argument)
|
82
|
-
return false unless argument.
|
82
|
+
return false unless argument.type?(:str, :sym)
|
83
83
|
|
84
84
|
!/^[a-zA-Z-][a-zA-Z\-0-9]*$/.match?(argument.value)
|
85
85
|
end
|
@@ -0,0 +1,448 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Rails
|
6
|
+
# Avoid dangerous column names.
|
7
|
+
#
|
8
|
+
# Some column names are considered dangerous because they would overwrite methods already defined.
|
9
|
+
#
|
10
|
+
# @example
|
11
|
+
# # bad
|
12
|
+
# add_column :users, :save
|
13
|
+
#
|
14
|
+
# # good
|
15
|
+
# add_column :users, :saved
|
16
|
+
class DangerousColumnNames < Base # rubocop:disable Metrics/ClassLength
|
17
|
+
include MigrationsHelper
|
18
|
+
|
19
|
+
COLUMN_TYPE_METHOD_NAMES = %i[
|
20
|
+
bigint
|
21
|
+
binary
|
22
|
+
blob
|
23
|
+
boolean
|
24
|
+
date
|
25
|
+
datetime
|
26
|
+
decimal
|
27
|
+
float
|
28
|
+
integer
|
29
|
+
numeric
|
30
|
+
primary_key
|
31
|
+
string
|
32
|
+
text
|
33
|
+
time
|
34
|
+
].to_set.freeze
|
35
|
+
|
36
|
+
# Generated from `ActiveRecord::AttributeMethods.dangerous_attribute_methods` on activerecord 7.1.3.
|
37
|
+
# rubocop:disable Metrics/CollectionLiteralLength
|
38
|
+
DANGEROUS_COLUMN_NAMES = %w[
|
39
|
+
__callbacks
|
40
|
+
__id__
|
41
|
+
_assign_attribute
|
42
|
+
_assign_attributes
|
43
|
+
_before_commit_callbacks
|
44
|
+
_commit_callbacks
|
45
|
+
_committed_already_called
|
46
|
+
_create_callbacks
|
47
|
+
_create_record
|
48
|
+
_delete_row
|
49
|
+
_destroy
|
50
|
+
_destroy_callbacks
|
51
|
+
_ensure_no_duplicate_errors
|
52
|
+
_find_callbacks
|
53
|
+
_find_record
|
54
|
+
_has_attribute
|
55
|
+
_initialize_callbacks
|
56
|
+
_lock_value_for_database
|
57
|
+
_merge_attributes
|
58
|
+
_primary_key_constraints_hash
|
59
|
+
_raise_readonly_record_error
|
60
|
+
_raise_record_not_destroyed
|
61
|
+
_raise_record_not_touched_error
|
62
|
+
_read_attribute
|
63
|
+
_record_changed
|
64
|
+
_reflections
|
65
|
+
_rollback_callbacks
|
66
|
+
_run_before_commit_callbacks
|
67
|
+
_run_commit_callbacks
|
68
|
+
_run_create_callbacks
|
69
|
+
_run_destroy_callbacks
|
70
|
+
_run_find_callbacks
|
71
|
+
_run_initialize_callbacks
|
72
|
+
_run_rollback_callbacks
|
73
|
+
_run_save_callbacks
|
74
|
+
_run_touch_callbacks
|
75
|
+
_run_update_callbacks
|
76
|
+
_run_validate_callbacks
|
77
|
+
_run_validation_callbacks
|
78
|
+
_save_callbacks
|
79
|
+
_touch_callbacks
|
80
|
+
_touch_row
|
81
|
+
_trigger_destroy_callback
|
82
|
+
_trigger_update_callback
|
83
|
+
_update_callbacks
|
84
|
+
_update_record
|
85
|
+
_update_row
|
86
|
+
_validate_callbacks
|
87
|
+
_validation_callbacks
|
88
|
+
_validators
|
89
|
+
_write_attribute
|
90
|
+
[]
|
91
|
+
[]=
|
92
|
+
accessed_fields
|
93
|
+
add_to_transaction
|
94
|
+
aggregate_reflections
|
95
|
+
all_timestamp_attributes_in_model
|
96
|
+
allow_destroy
|
97
|
+
apply_scoping
|
98
|
+
around_save_collection_association
|
99
|
+
assign_attributes
|
100
|
+
assign_multiparameter_attributes
|
101
|
+
assign_nested_attributes_for_collection_association
|
102
|
+
assign_nested_attributes_for_one_to_one_association
|
103
|
+
assign_nested_parameter_attributes
|
104
|
+
assign_to_or_mark_for_destruction
|
105
|
+
associated_records_to_validate_or_save
|
106
|
+
association
|
107
|
+
association_cached
|
108
|
+
association_foreign_key_changed
|
109
|
+
association_instance_get
|
110
|
+
association_instance_set
|
111
|
+
association_valid
|
112
|
+
attachment_changes
|
113
|
+
attachment_reflections
|
114
|
+
attribute
|
115
|
+
attribute_aliases
|
116
|
+
attribute_before_last_save
|
117
|
+
attribute_before_type_cast
|
118
|
+
attribute_came_from_user
|
119
|
+
attribute_change
|
120
|
+
attribute_change_to_be_saved
|
121
|
+
attribute_changed
|
122
|
+
attribute_changed_in_place
|
123
|
+
attribute_for_database
|
124
|
+
attribute_for_inspect
|
125
|
+
attribute_in_database
|
126
|
+
attribute_method
|
127
|
+
attribute_method_matchers
|
128
|
+
attribute_missing
|
129
|
+
attribute_names
|
130
|
+
attribute_names_for_partial_inserts
|
131
|
+
attribute_names_for_partial_updates
|
132
|
+
attribute_names_for_serialization
|
133
|
+
attribute_present
|
134
|
+
attribute_previous_change
|
135
|
+
attribute_previously_changed
|
136
|
+
attribute_previously_was
|
137
|
+
attribute_was
|
138
|
+
attribute_will_change
|
139
|
+
attribute=
|
140
|
+
attributes
|
141
|
+
attributes_before_type_cast
|
142
|
+
attributes_for_create
|
143
|
+
attributes_for_database
|
144
|
+
attributes_for_update
|
145
|
+
attributes_in_database
|
146
|
+
attributes_with_values
|
147
|
+
attributes=
|
148
|
+
automatic_scope_inversing
|
149
|
+
becomes
|
150
|
+
before_committed
|
151
|
+
belongs_to_touch_method
|
152
|
+
broadcast_action
|
153
|
+
broadcast_action_later
|
154
|
+
broadcast_action_later_to
|
155
|
+
broadcast_action_to
|
156
|
+
broadcast_after_to
|
157
|
+
broadcast_append
|
158
|
+
broadcast_append_later
|
159
|
+
broadcast_append_later_to
|
160
|
+
broadcast_append_to
|
161
|
+
broadcast_before_to
|
162
|
+
broadcast_prepend
|
163
|
+
broadcast_prepend_later
|
164
|
+
broadcast_prepend_later_to
|
165
|
+
broadcast_prepend_to
|
166
|
+
broadcast_remove
|
167
|
+
broadcast_remove_to
|
168
|
+
broadcast_render
|
169
|
+
broadcast_render_later
|
170
|
+
broadcast_render_later_to
|
171
|
+
broadcast_render_to
|
172
|
+
broadcast_rendering_with_defaults
|
173
|
+
broadcast_replace
|
174
|
+
broadcast_replace_later
|
175
|
+
broadcast_replace_later_to
|
176
|
+
broadcast_replace_to
|
177
|
+
broadcast_target_default
|
178
|
+
broadcast_update
|
179
|
+
broadcast_update_later
|
180
|
+
broadcast_update_later_to
|
181
|
+
broadcast_update_to
|
182
|
+
build_decrypt_attribute_assignments
|
183
|
+
build_encrypt_attribute_assignments
|
184
|
+
cache_key
|
185
|
+
cache_key_with_version
|
186
|
+
cache_timestamp_format
|
187
|
+
cache_version
|
188
|
+
cache_versioning
|
189
|
+
call_reject_if
|
190
|
+
can_use_fast_cache_version
|
191
|
+
cant_modify_encrypted_attributes_when_frozen
|
192
|
+
changed
|
193
|
+
changed_attribute_names_to_save
|
194
|
+
changed_attributes
|
195
|
+
changed_for_autosave
|
196
|
+
changes
|
197
|
+
changes_applied
|
198
|
+
changes_to_save
|
199
|
+
check_record_limit
|
200
|
+
ciphertext_for
|
201
|
+
class
|
202
|
+
clear_attribute_change
|
203
|
+
clear_attribute_changes
|
204
|
+
clear_changes_information
|
205
|
+
clear_timestamp_attributes
|
206
|
+
clear_transaction_record_state
|
207
|
+
clone
|
208
|
+
collection_cache_versioning
|
209
|
+
column_for_attribute
|
210
|
+
committed
|
211
|
+
connection_handler
|
212
|
+
create_or_update
|
213
|
+
current_time_from_proper_timezone
|
214
|
+
custom_inspect_method_defined
|
215
|
+
custom_validation_context
|
216
|
+
decrement
|
217
|
+
decrypt
|
218
|
+
decrypt_attributes
|
219
|
+
decrypt_rich_texts
|
220
|
+
default_connection_handler
|
221
|
+
default_role
|
222
|
+
default_scope_override
|
223
|
+
default_scopes
|
224
|
+
default_shard
|
225
|
+
default_validation_context
|
226
|
+
defined_enums
|
227
|
+
delete
|
228
|
+
destroy
|
229
|
+
destroy_association_async_job
|
230
|
+
destroy_associations
|
231
|
+
destroy_row
|
232
|
+
destroyed
|
233
|
+
destroyed_by_association
|
234
|
+
destroyed_by_association=
|
235
|
+
dup
|
236
|
+
each_counter_cached_associations
|
237
|
+
encode_with
|
238
|
+
encrypt
|
239
|
+
encrypt_attributes
|
240
|
+
encrypt_rich_texts
|
241
|
+
encryptable_rich_texts
|
242
|
+
encrypted_attribute
|
243
|
+
encrypted_attributes
|
244
|
+
encrypted_attributes=
|
245
|
+
ensure_proper_type
|
246
|
+
errors
|
247
|
+
execute_callstack_for_multiparameter_attributes
|
248
|
+
extract_callstack_for_multiparameter_attributes
|
249
|
+
find_parameter_position
|
250
|
+
forget_attribute_assignments
|
251
|
+
format_for_inspect
|
252
|
+
freeze
|
253
|
+
from_json
|
254
|
+
frozen?
|
255
|
+
halted_callback_hook
|
256
|
+
has_attribute
|
257
|
+
has_changes_to_save
|
258
|
+
has_defer_touch_attrs
|
259
|
+
has_destroy_flag
|
260
|
+
has_encrypted_attributes
|
261
|
+
has_encrypted_rich_texts
|
262
|
+
has_transactional_callbacks
|
263
|
+
hash
|
264
|
+
id
|
265
|
+
id_before_type_cast
|
266
|
+
id_for_database
|
267
|
+
id_in_database
|
268
|
+
id_was
|
269
|
+
id=
|
270
|
+
include_root_in_json
|
271
|
+
increment
|
272
|
+
init_internals
|
273
|
+
init_with
|
274
|
+
init_with_attributes
|
275
|
+
initialize_internals_callback
|
276
|
+
inspection_filter
|
277
|
+
invalid
|
278
|
+
lock
|
279
|
+
lock_optimistically
|
280
|
+
locking_enabled
|
281
|
+
logger
|
282
|
+
mark_for_destruction
|
283
|
+
marked_for_destruction
|
284
|
+
matched_attribute_method
|
285
|
+
max_updated_column_timestamp
|
286
|
+
missing_attribute
|
287
|
+
model_name
|
288
|
+
mutations_before_last_save
|
289
|
+
mutations_from_database
|
290
|
+
nested_attributes_options
|
291
|
+
nested_records_changed_for_autosave
|
292
|
+
new_record
|
293
|
+
no_touching
|
294
|
+
normalize_reflection_attribute
|
295
|
+
partial_inserts
|
296
|
+
partial_updates
|
297
|
+
perform_validations
|
298
|
+
persisted
|
299
|
+
pk_attribute
|
300
|
+
pluralize_table_names
|
301
|
+
populate_with_current_scope_attributes
|
302
|
+
previous_changes
|
303
|
+
previously_new_record
|
304
|
+
previously_persisted
|
305
|
+
primary_key_prefix_type
|
306
|
+
query_attribute
|
307
|
+
raise_nested_attributes_record_not_found
|
308
|
+
raise_validation_error
|
309
|
+
raw_timestamp_to_cache_version
|
310
|
+
read_attribute
|
311
|
+
read_attribute_before_type_cast
|
312
|
+
read_attribute_for_serialization
|
313
|
+
read_attribute_for_validation
|
314
|
+
read_store_attribute
|
315
|
+
readonly
|
316
|
+
record_timestamps
|
317
|
+
record_timestamps=
|
318
|
+
reject_new_record
|
319
|
+
reload
|
320
|
+
remember_transaction_record_state
|
321
|
+
respond_to_without_attributes
|
322
|
+
restore_attribute
|
323
|
+
restore_attributes
|
324
|
+
restore_transaction_record_state
|
325
|
+
rolledback
|
326
|
+
run_callbacks
|
327
|
+
run_validations
|
328
|
+
sanitize_for_mass_assignment
|
329
|
+
sanitize_forbidden_attributes
|
330
|
+
save
|
331
|
+
save_belongs_to_association
|
332
|
+
save_collection_association
|
333
|
+
save_has_one_association
|
334
|
+
saved_change_to_attribute
|
335
|
+
saved_changes
|
336
|
+
serializable_add_includes
|
337
|
+
serializable_attributes
|
338
|
+
serializable_hash
|
339
|
+
should_record_timestamps
|
340
|
+
signed_id
|
341
|
+
signed_id_verifier_secret
|
342
|
+
skip_time_zone_conversion_for_attributes
|
343
|
+
slice
|
344
|
+
store_accessor_for
|
345
|
+
store_full_class_name
|
346
|
+
store_full_sti_class
|
347
|
+
strict_loaded_associations
|
348
|
+
strict_loading
|
349
|
+
strict_loading_mode
|
350
|
+
strict_loading_n_plus_one_only
|
351
|
+
surreptitiously_touch
|
352
|
+
table_name_prefix
|
353
|
+
table_name_suffix
|
354
|
+
time_zone_aware_attributes
|
355
|
+
time_zone_aware_types
|
356
|
+
timestamp_attributes_for_create_in_model
|
357
|
+
timestamp_attributes_for_update_in_model
|
358
|
+
to_ary
|
359
|
+
to_gid
|
360
|
+
to_gid_param
|
361
|
+
to_global_id
|
362
|
+
to_key
|
363
|
+
to_model
|
364
|
+
to_partial_path
|
365
|
+
to_sgid
|
366
|
+
to_sgid_param
|
367
|
+
to_signed_global_id
|
368
|
+
toggle
|
369
|
+
touch
|
370
|
+
touch_deferred_attributes
|
371
|
+
touch_later
|
372
|
+
transaction
|
373
|
+
transaction_include_any_action
|
374
|
+
trigger_transactional_callbacks
|
375
|
+
type_cast_attribute_value
|
376
|
+
type_for_attribute
|
377
|
+
update
|
378
|
+
update_attribute
|
379
|
+
update_column
|
380
|
+
update_columns
|
381
|
+
valid
|
382
|
+
validate
|
383
|
+
validate_collection_association
|
384
|
+
validate_encryption_allowed
|
385
|
+
validate_single_association
|
386
|
+
validates_absence_of
|
387
|
+
validates_acceptance_of
|
388
|
+
validates_comparison_of
|
389
|
+
validates_confirmation_of
|
390
|
+
validates_exclusion_of
|
391
|
+
validates_format_of
|
392
|
+
validates_inclusion_of
|
393
|
+
validates_length_of
|
394
|
+
validates_numericality_of
|
395
|
+
validates_presence_of
|
396
|
+
validates_size_of
|
397
|
+
validates_with
|
398
|
+
validation_context
|
399
|
+
validation_context=
|
400
|
+
values_at
|
401
|
+
verify_readonly_attribute
|
402
|
+
will_be_destroyed
|
403
|
+
will_save_change_to_attribute
|
404
|
+
with_lock
|
405
|
+
with_transaction_returning_status
|
406
|
+
write_attribute
|
407
|
+
write_store_attribute
|
408
|
+
].freeze
|
409
|
+
# rubocop:enable Metrics/CollectionLiteralLength
|
410
|
+
|
411
|
+
MSG = 'Avoid dangerous column names.'
|
412
|
+
|
413
|
+
RESTRICT_ON_SEND = [:add_column, :rename, :rename_column, *COLUMN_TYPE_METHOD_NAMES].freeze
|
414
|
+
|
415
|
+
def on_send(node)
|
416
|
+
column_name_node = column_name_node_from(node)
|
417
|
+
return false unless column_name_node
|
418
|
+
return false unless dangerous_column_name_node?(column_name_node)
|
419
|
+
|
420
|
+
add_offense(column_name_node)
|
421
|
+
end
|
422
|
+
|
423
|
+
private
|
424
|
+
|
425
|
+
def column_name_node_from(node)
|
426
|
+
case node.method_name
|
427
|
+
when :add_column, :rename
|
428
|
+
node.arguments[1]
|
429
|
+
when :rename_column
|
430
|
+
node.arguments[2]
|
431
|
+
when *COLUMN_TYPE_METHOD_NAMES
|
432
|
+
node.first_argument
|
433
|
+
end
|
434
|
+
end
|
435
|
+
|
436
|
+
def dangerous_column_name_node?(node)
|
437
|
+
return false unless node.respond_to?(:value)
|
438
|
+
|
439
|
+
dangerous_column_name?(node.value.to_s)
|
440
|
+
end
|
441
|
+
|
442
|
+
def dangerous_column_name?(column_name)
|
443
|
+
DANGEROUS_COLUMN_NAMES.include?(column_name)
|
444
|
+
end
|
445
|
+
end
|
446
|
+
end
|
447
|
+
end
|
448
|
+
end
|
@@ -12,16 +12,19 @@ module RuboCop
|
|
12
12
|
# The cop also reports warnings when you are using `to_time` method,
|
13
13
|
# because it doesn't know about Rails time zone either.
|
14
14
|
#
|
15
|
-
# Two styles are supported for this cop. When `EnforcedStyle` is
|
15
|
+
# Two styles are supported for this cop. When `EnforcedStyle` is `strict`
|
16
16
|
# then the Date methods `today`, `current`, `yesterday`, and `tomorrow`
|
17
17
|
# are prohibited and the usage of both `to_time`
|
18
|
-
# and
|
18
|
+
# and `to_time_in_current_zone` are reported as warning.
|
19
19
|
#
|
20
20
|
# When `EnforcedStyle` is `flexible` then only `Date.today` is prohibited.
|
21
21
|
#
|
22
22
|
# And you can set a warning for `to_time` with `AllowToTime: false`.
|
23
23
|
# `AllowToTime` is `true` by default to prevent false positive on `DateTime` object.
|
24
24
|
#
|
25
|
+
# @safety
|
26
|
+
# This cop's autocorrection is unsafe because it may change handling time.
|
27
|
+
#
|
25
28
|
# @example EnforcedStyle: flexible (default)
|
26
29
|
# # bad
|
27
30
|
# Date.today
|
@@ -51,6 +54,8 @@ module RuboCop
|
|
51
54
|
# # bad
|
52
55
|
# date.to_time
|
53
56
|
class Date < Base
|
57
|
+
extend AutoCorrector
|
58
|
+
|
54
59
|
include ConfigurableEnforcedStyle
|
55
60
|
|
56
61
|
MSG = 'Do not use `Date.%<method_called>s` without zone. Use `Time.zone.%<day>s` instead.'
|
@@ -92,7 +97,9 @@ module RuboCop
|
|
92
97
|
|
93
98
|
message = format(DEPRECATED_MSG, deprecated: method[:deprecated], relevant: method[:relevant])
|
94
99
|
|
95
|
-
add_offense(node.loc.selector, message: message)
|
100
|
+
add_offense(node.loc.selector, message: message) do |corrector|
|
101
|
+
corrector.replace(node.loc.selector, method[:relevant].to_s)
|
102
|
+
end
|
96
103
|
end
|
97
104
|
end
|
98
105
|
|
@@ -108,7 +115,9 @@ module RuboCop
|
|
108
115
|
|
109
116
|
message = format(MSG, method_called: method_name, day: day)
|
110
117
|
|
111
|
-
add_offense(node.loc.selector, message: message)
|
118
|
+
add_offense(node.loc.selector, message: message) do |corrector|
|
119
|
+
corrector.replace(node.receiver.loc.name, 'Time.zone')
|
120
|
+
end
|
112
121
|
end
|
113
122
|
|
114
123
|
def extract_method_chain(node)
|
@@ -130,7 +139,7 @@ module RuboCop
|
|
130
139
|
end
|
131
140
|
|
132
141
|
def safe_to_time?(node)
|
133
|
-
return unless node.method?(:to_time)
|
142
|
+
return false unless node.method?(:to_time)
|
134
143
|
|
135
144
|
if node.receiver.str_type?
|
136
145
|
zone_regexp = /([+-][\d:]+|\dZ)\z/
|