diff --git a/lib/rubocop/cop/rails/time_zone.rb b/lib/rubocop/cop/rails/time_zone.rb index 31aff61542..80d7d97790 100644 --- a/lib/rubocop/cop/rails/time_zone.rb +++ b/lib/rubocop/cop/rails/time_zone.rb @@ -97,13 +97,9 @@ def autocorrect(corrector, node) end def autocorrect_time_new(node, corrector) - if node.first_argument&.str_type? - corrector.replace(node.loc.selector, 'parse') - elsif node.arguments? - corrector.replace(node.loc.selector, 'local') - else - corrector.replace(node.loc.selector, 'now') - end + replacement = replacement(node) + + corrector.replace(node.loc.selector, replacement) end # remove redundant `.in_time_zone` from `Time.zone.now.in_time_zone` @@ -185,9 +181,7 @@ def method_send?(node) def safe_method(method_name, node) if %w[new current].include?(method_name) - return 'parse' if node.first_argument&.str_type? - - node.arguments? ? 'local' : 'now' + replacement(node) else method_name end @@ -263,6 +257,12 @@ def offset_option_provided?(node) pair.key.sym_type? && pair.key.value == :in && !pair.value.nil_type? end end + + def replacement(node) + return 'now' unless node.arguments? + + node.first_argument.str_type? ? 'parse' : 'local' + end end end end