aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAkira Matsuda <ronnie@dio.jp>2019-08-01 16:41:26 +0900
committerAkira Matsuda <ronnie@dio.jp>2019-08-01 17:58:00 +0900
commitaf2129b4c74732c88ffce76e5c55c805cb9431f6 (patch)
treec74d8c85f81ea6b3a5e4209be247c9d83666bd4d
parentdbf3e4882f9da95e34ed9086f182cf424aaac224 (diff)
downloadrails-af2129b4c74732c88ffce76e5c55c805cb9431f6.tar.gz
rails-af2129b4c74732c88ffce76e5c55c805cb9431f6.tar.bz2
rails-af2129b4c74732c88ffce76e5c55c805cb9431f6.zip
Use `try` only when we're unsure if the receiver would respond_to the method
-rw-r--r--actionmailbox/lib/action_mailbox/router.rb2
-rw-r--r--actionpack/lib/action_controller/metal/conditional_get.rb2
-rw-r--r--actionpack/lib/action_controller/metal/content_security_policy.rb2
-rw-r--r--actionpack/lib/action_dispatch/http/feature_policy.rb2
-rw-r--r--actionpack/lib/action_dispatch/system_testing/browser.rb4
-rw-r--r--actionview/lib/action_view/helpers/sanitize_helper.rb3
-rw-r--r--actionview/lib/action_view/helpers/tags/date_field.rb2
-rw-r--r--actionview/lib/action_view/helpers/tags/datetime_local_field.rb2
-rw-r--r--actionview/lib/action_view/helpers/tags/month_field.rb2
-rw-r--r--actionview/lib/action_view/helpers/tags/time_field.rb2
-rw-r--r--actionview/lib/action_view/helpers/tags/week_field.rb2
-rw-r--r--actionview/lib/action_view/renderer/streaming_template_renderer.rb2
-rw-r--r--actionview/lib/action_view/renderer/template_renderer.rb4
-rw-r--r--activejob/lib/active_job/core.rb4
-rw-r--r--activejob/test/support/integration/dummy_app_template.rb2
-rw-r--r--activemodel/lib/active_model/secure_password.rb20
-rw-r--r--activerecord/lib/active_record/associations/belongs_to_association.rb2
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb2
-rw-r--r--activerecord/lib/active_record/database_configurations.rb2
-rw-r--r--activerecord/lib/active_record/errors.rb2
-rw-r--r--activerecord/lib/active_record/serialization.rb2
-rw-r--r--activerecord/test/cases/relations_test.rb2
-rw-r--r--activesupport/test/current_attributes_test.rb10
-rw-r--r--railties/test/application/current_attributes_integration_test.rb4
24 files changed, 40 insertions, 43 deletions
diff --git a/actionmailbox/lib/action_mailbox/router.rb b/actionmailbox/lib/action_mailbox/router.rb
index 54982eae21..9ac58e5b3f 100644
--- a/actionmailbox/lib/action_mailbox/router.rb
+++ b/actionmailbox/lib/action_mailbox/router.rb
@@ -31,7 +31,7 @@ module ActionMailbox
end
def mailbox_for(inbound_email)
- routes.detect { |route| route.match?(inbound_email) }.try(:mailbox_class)
+ routes.detect { |route| route.match?(inbound_email) }&.mailbox_class
end
private
diff --git a/actionpack/lib/action_controller/metal/conditional_get.rb b/actionpack/lib/action_controller/metal/conditional_get.rb
index 29d1919ec5..f21e51a68a 100644
--- a/actionpack/lib/action_controller/metal/conditional_get.rb
+++ b/actionpack/lib/action_controller/metal/conditional_get.rb
@@ -17,7 +17,7 @@ module ActionController
# of cached pages.
#
# class InvoicesController < ApplicationController
- # etag { current_user.try :id }
+ # etag { current_user&.id }
#
# def show
# # Etag will differ even for the same invoice when it's viewed by a different current_user
diff --git a/actionpack/lib/action_controller/metal/content_security_policy.rb b/actionpack/lib/action_controller/metal/content_security_policy.rb
index ebd90f07c8..25fc110bfe 100644
--- a/actionpack/lib/action_controller/metal/content_security_policy.rb
+++ b/actionpack/lib/action_controller/metal/content_security_policy.rb
@@ -45,7 +45,7 @@ module ActionController #:nodoc:
end
def current_content_security_policy
- request.content_security_policy.try(:clone) || ActionDispatch::ContentSecurityPolicy.new
+ request.content_security_policy&.clone || ActionDispatch::ContentSecurityPolicy.new
end
end
end
diff --git a/actionpack/lib/action_dispatch/http/feature_policy.rb b/actionpack/lib/action_dispatch/http/feature_policy.rb
index 78e982918d..c09690f9fc 100644
--- a/actionpack/lib/action_dispatch/http/feature_policy.rb
+++ b/actionpack/lib/action_dispatch/http/feature_policy.rb
@@ -42,7 +42,7 @@ module ActionDispatch #:nodoc:
end
def policy_empty?(policy)
- policy.try(:directives) && policy.directives.empty?
+ policy&.directives&.empty?
end
end
diff --git a/actionpack/lib/action_dispatch/system_testing/browser.rb b/actionpack/lib/action_dispatch/system_testing/browser.rb
index e861e52f09..91f332be13 100644
--- a/actionpack/lib/action_dispatch/system_testing/browser.rb
+++ b/actionpack/lib/action_dispatch/system_testing/browser.rb
@@ -47,14 +47,14 @@ module ActionDispatch
case type
when :chrome
if ::Selenium::WebDriver::Service.respond_to? :driver_path=
- ::Selenium::WebDriver::Chrome::Service.driver_path.try(:call)
+ ::Selenium::WebDriver::Chrome::Service.driver_path&.call
else
# Selenium <= v3.141.0
::Selenium::WebDriver::Chrome.driver_path
end
when :firefox
if ::Selenium::WebDriver::Service.respond_to? :driver_path=
- ::Selenium::WebDriver::Firefox::Service.driver_path.try(:call)
+ ::Selenium::WebDriver::Firefox::Service.driver_path&.call
else
# Selenium <= v3.141.0
::Selenium::WebDriver::Firefox.driver_path
diff --git a/actionview/lib/action_view/helpers/sanitize_helper.rb b/actionview/lib/action_view/helpers/sanitize_helper.rb
index f4fa133f55..fdce4fe688 100644
--- a/actionview/lib/action_view/helpers/sanitize_helper.rb
+++ b/actionview/lib/action_view/helpers/sanitize_helper.rb
@@ -1,6 +1,5 @@
# frozen_string_literal: true
-require "active_support/core_ext/object/try"
require "rails-html-sanitizer"
module ActionView
@@ -80,7 +79,7 @@ module ActionView
# config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a']
# config.action_view.sanitized_allowed_attributes = ['href', 'title']
def sanitize(html, options = {})
- self.class.white_list_sanitizer.sanitize(html, options).try(:html_safe)
+ self.class.white_list_sanitizer.sanitize(html, options)&.html_safe
end
# Sanitizes a block of CSS code. Used by +sanitize+ when it comes across a style attribute.
diff --git a/actionview/lib/action_view/helpers/tags/date_field.rb b/actionview/lib/action_view/helpers/tags/date_field.rb
index ceaabfa99c..9cdfc6991f 100644
--- a/actionview/lib/action_view/helpers/tags/date_field.rb
+++ b/actionview/lib/action_view/helpers/tags/date_field.rb
@@ -6,7 +6,7 @@ module ActionView
class DateField < DatetimeField # :nodoc:
private
def format_date(value)
- value.try(:strftime, "%Y-%m-%d")
+ value&.strftime("%Y-%m-%d")
end
end
end
diff --git a/actionview/lib/action_view/helpers/tags/datetime_local_field.rb b/actionview/lib/action_view/helpers/tags/datetime_local_field.rb
index 8908bf9948..f0834ac6ce 100644
--- a/actionview/lib/action_view/helpers/tags/datetime_local_field.rb
+++ b/actionview/lib/action_view/helpers/tags/datetime_local_field.rb
@@ -12,7 +12,7 @@ module ActionView
private
def format_date(value)
- value.try(:strftime, "%Y-%m-%dT%T")
+ value&.strftime("%Y-%m-%dT%T")
end
end
end
diff --git a/actionview/lib/action_view/helpers/tags/month_field.rb b/actionview/lib/action_view/helpers/tags/month_field.rb
index 463866a181..b582bb4f79 100644
--- a/actionview/lib/action_view/helpers/tags/month_field.rb
+++ b/actionview/lib/action_view/helpers/tags/month_field.rb
@@ -6,7 +6,7 @@ module ActionView
class MonthField < DatetimeField # :nodoc:
private
def format_date(value)
- value.try(:strftime, "%Y-%m")
+ value&.strftime("%Y-%m")
end
end
end
diff --git a/actionview/lib/action_view/helpers/tags/time_field.rb b/actionview/lib/action_view/helpers/tags/time_field.rb
index e74c578db9..e5e0b84891 100644
--- a/actionview/lib/action_view/helpers/tags/time_field.rb
+++ b/actionview/lib/action_view/helpers/tags/time_field.rb
@@ -6,7 +6,7 @@ module ActionView
class TimeField < DatetimeField # :nodoc:
private
def format_date(value)
- value.try(:strftime, "%T.%L")
+ value&.strftime("%T.%L")
end
end
end
diff --git a/actionview/lib/action_view/helpers/tags/week_field.rb b/actionview/lib/action_view/helpers/tags/week_field.rb
index 5a403ed91d..7828a3149f 100644
--- a/actionview/lib/action_view/helpers/tags/week_field.rb
+++ b/actionview/lib/action_view/helpers/tags/week_field.rb
@@ -6,7 +6,7 @@ module ActionView
class WeekField < DatetimeField # :nodoc:
private
def format_date(value)
- value.try(:strftime, "%Y-W%V")
+ value&.strftime("%Y-W%V")
end
end
end
diff --git a/actionview/lib/action_view/renderer/streaming_template_renderer.rb b/actionview/lib/action_view/renderer/streaming_template_renderer.rb
index 08a280c7ee..5942717b8d 100644
--- a/actionview/lib/action_view/renderer/streaming_template_renderer.rb
+++ b/actionview/lib/action_view/renderer/streaming_template_renderer.rb
@@ -62,7 +62,7 @@ module ActionView
output = ActionView::StreamingBuffer.new(buffer)
yielder = lambda { |*name| view._layout_for(*name) }
- instrument(:template, identifier: template.identifier, layout: layout.try(:virtual_path)) do
+ instrument(:template, identifier: template.identifier, layout: (layout && layout.virtual_path)) do
outer_config = I18n.config
fiber = Fiber.new do
I18n.config = outer_config
diff --git a/actionview/lib/action_view/renderer/template_renderer.rb b/actionview/lib/action_view/renderer/template_renderer.rb
index b2d7332572..da0b2dc0d2 100644
--- a/actionview/lib/action_view/renderer/template_renderer.rb
+++ b/actionview/lib/action_view/renderer/template_renderer.rb
@@ -1,7 +1,5 @@
# frozen_string_literal: true
-require "active_support/core_ext/object/try"
-
module ActionView
class TemplateRenderer < AbstractRenderer #:nodoc:
def render(context, options)
@@ -54,7 +52,7 @@ module ActionView
# supplied as well.
def render_template(view, template, layout_name, locals)
render_with_layout(view, template, layout_name, locals) do |layout|
- instrument(:template, identifier: template.identifier, layout: layout.try(:virtual_path)) do
+ instrument(:template, identifier: template.identifier, layout: (layout && layout.virtual_path)) do
template.render(view, locals) { |*name| view._layout_for(*name) }
end
end
diff --git a/activejob/lib/active_job/core.rb b/activejob/lib/active_job/core.rb
index 283125698d..5b10858d80 100644
--- a/activejob/lib/active_job/core.rb
+++ b/activejob/lib/active_job/core.rb
@@ -100,7 +100,7 @@ module ActiveJob
"executions" => executions,
"exception_executions" => exception_executions,
"locale" => I18n.locale.to_s,
- "timezone" => Time.zone.try(:name),
+ "timezone" => Time.zone&.name,
"enqueued_at" => Time.now.utc.iso8601
}
end
@@ -140,7 +140,7 @@ module ActiveJob
self.executions = job_data["executions"]
self.exception_executions = job_data["exception_executions"]
self.locale = job_data["locale"] || I18n.locale.to_s
- self.timezone = job_data["timezone"] || Time.zone.try(:name)
+ self.timezone = job_data["timezone"] || Time.zone&.name
self.enqueued_at = job_data["enqueued_at"]
end
diff --git a/activejob/test/support/integration/dummy_app_template.rb b/activejob/test/support/integration/dummy_app_template.rb
index b56dd3e591..65229ba397 100644
--- a/activejob/test/support/integration/dummy_app_template.rb
+++ b/activejob/test/support/integration/dummy_app_template.rb
@@ -21,7 +21,7 @@ class TestJob < ActiveJob::Base
File.open(Rails.root.join("tmp/\#{x}.new"), "wb+") do |f|
f.write Marshal.dump({
"locale" => I18n.locale.to_s || "en",
- "timezone" => Time.zone.try(:name) || "UTC",
+ "timezone" => Time.zone&.name || "UTC",
"executed_at" => Time.now.to_r
})
end
diff --git a/activemodel/lib/active_model/secure_password.rb b/activemodel/lib/active_model/secure_password.rb
index 5f409326bd..c177d771ad 100644
--- a/activemodel/lib/active_model/secure_password.rb
+++ b/activemodel/lib/active_model/secure_password.rb
@@ -45,19 +45,19 @@ module ActiveModel
# end
#
# user = User.new(name: 'david', password: '', password_confirmation: 'nomatch')
- # user.save # => false, password required
+ # user.save # => false, password required
# user.password = 'mUc3m00RsqyRe'
- # user.save # => false, confirmation doesn't match
+ # user.save # => false, confirmation doesn't match
# user.password_confirmation = 'mUc3m00RsqyRe'
- # user.save # => true
+ # user.save # => true
# user.recovery_password = "42password"
- # user.recovery_password_digest # => "$2a$04$iOfhwahFymCs5weB3BNH/uXkTG65HR.qpW.bNhEjFP3ftli3o5DQC"
- # user.save # => true
- # user.authenticate('notright') # => false
- # user.authenticate('mUc3m00RsqyRe') # => user
- # user.authenticate_recovery_password('42password') # => user
- # User.find_by(name: 'david').try(:authenticate, 'notright') # => false
- # User.find_by(name: 'david').try(:authenticate, 'mUc3m00RsqyRe') # => user
+ # user.recovery_password_digest # => "$2a$04$iOfhwahFymCs5weB3BNH/uXkTG65HR.qpW.bNhEjFP3ftli3o5DQC"
+ # user.save # => true
+ # user.authenticate('notright') # => false
+ # user.authenticate('mUc3m00RsqyRe') # => user
+ # user.authenticate_recovery_password('42password') # => user
+ # User.find_by(name: 'david')&.authenticate('notright') # => false
+ # User.find_by(name: 'david')&.authenticate('mUc3m00RsqyRe') # => user
def has_secure_password(attribute = :password, validations: true)
# Load bcrypt gem only when has_secure_password is used.
# This is to avoid ActiveModel (and by extension the entire framework)
diff --git a/activerecord/lib/active_record/associations/belongs_to_association.rb b/activerecord/lib/active_record/associations/belongs_to_association.rb
index 3346725f2d..e3886f394a 100644
--- a/activerecord/lib/active_record/associations/belongs_to_association.rb
+++ b/activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -44,7 +44,7 @@ module ActiveRecord
def decrement_counters_before_last_save
if reflection.polymorphic?
- model_was = owner.attribute_before_last_save(reflection.foreign_type).try(:constantize)
+ model_was = owner.attribute_before_last_save(reflection.foreign_type)&.constantize
else
model_was = klass
end
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb b/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb
index 8df91c988b..75213e964b 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/referential_integrity.rb
@@ -24,7 +24,7 @@ WARNING: Rails was not able to disable referential integrity.
This is most likely caused due to missing permissions.
Rails needs superuser privileges to disable referential integrity.
- cause: #{original_exception.try(:message)}
+ cause: #{original_exception&.message}
WARNING
raise e
diff --git a/activerecord/lib/active_record/database_configurations.rb b/activerecord/lib/active_record/database_configurations.rb
index fcf13a910c..c56a099769 100644
--- a/activerecord/lib/active_record/database_configurations.rb
+++ b/activerecord/lib/active_record/database_configurations.rb
@@ -152,7 +152,7 @@ module ActiveRecord
def build_db_config_from_string(env_name, spec_name, config)
url = config
uri = URI.parse(url)
- if uri.try(:scheme)
+ if uri&.scheme
ActiveRecord::DatabaseConfigurations::UrlConfig.new(env_name, spec_name, url)
end
rescue URI::InvalidURIError
diff --git a/activerecord/lib/active_record/errors.rb b/activerecord/lib/active_record/errors.rb
index 509f21c9a5..0f110b4536 100644
--- a/activerecord/lib/active_record/errors.rb
+++ b/activerecord/lib/active_record/errors.rb
@@ -106,7 +106,7 @@ module ActiveRecord
# Wraps the underlying database error as +cause+.
class StatementInvalid < ActiveRecordError
def initialize(message = nil, sql: nil, binds: nil)
- super(message || $!.try(:message))
+ super(message || $!&.message)
@sql = sql
@binds = binds
end
diff --git a/activerecord/lib/active_record/serialization.rb b/activerecord/lib/active_record/serialization.rb
index 741fea43ce..9fc62a99f8 100644
--- a/activerecord/lib/active_record/serialization.rb
+++ b/activerecord/lib/active_record/serialization.rb
@@ -11,7 +11,7 @@ module ActiveRecord #:nodoc:
end
def serializable_hash(options = nil)
- options = options.try(:dup) || {}
+ options = options ? options.dup : {}
options[:except] = Array(options[:except]).map(&:to_s)
options[:except] |= Array(self.class.inheritance_column)
diff --git a/activerecord/test/cases/relations_test.rb b/activerecord/test/cases/relations_test.rb
index 1a20fe5dc2..8b3ae02947 100644
--- a/activerecord/test/cases/relations_test.rb
+++ b/activerecord/test/cases/relations_test.rb
@@ -1932,7 +1932,7 @@ class RelationTest < ActiveRecord::TestCase
assert_no_queries do
result = authors_count.map do |post|
- [post.num_posts, post.author.try(:name)]
+ [post.num_posts, post.author&.name]
end
expected = [[1, nil], [5, "David"], [3, "Mary"], [2, "Bob"]]
diff --git a/activesupport/test/current_attributes_test.rb b/activesupport/test/current_attributes_test.rb
index adbdc646bc..4cbf462da0 100644
--- a/activesupport/test/current_attributes_test.rb
+++ b/activesupport/test/current_attributes_test.rb
@@ -9,7 +9,7 @@ class CurrentAttributesTest < ActiveSupport::TestCase
attribute :world, :account, :person, :request
delegate :time_zone, to: :person
- before_reset { Session.previous = person.try(:id) }
+ before_reset { Session.previous = person&.id }
resets do
Time.zone = "UTC"
@@ -18,13 +18,13 @@ class CurrentAttributesTest < ActiveSupport::TestCase
def account=(account)
super
- self.person = "#{account}'s person"
+ self.person = Person.new(1, "#{account}'s person")
end
def person=(person)
super
- Time.zone = person.try(:time_zone)
- Session.current = person.try(:id)
+ Time.zone = person&.time_zone
+ Session.current = person&.id
end
def request
@@ -63,7 +63,7 @@ class CurrentAttributesTest < ActiveSupport::TestCase
test "set attribute via overwritten method" do
Current.account = "account/1"
assert_equal "account/1", Current.account
- assert_equal "account/1's person", Current.person
+ assert_equal "account/1's person", Current.person.name
end
test "set auxiliary class via overwritten method" do
diff --git a/railties/test/application/current_attributes_integration_test.rb b/railties/test/application/current_attributes_integration_test.rb
index 146e96facc..f4ff5b8077 100644
--- a/railties/test/application/current_attributes_integration_test.rb
+++ b/railties/test/application/current_attributes_integration_test.rb
@@ -18,7 +18,7 @@ class CurrentAttributesIntegrationTest < ActiveSupport::TestCase
def customer=(customer)
super
- Time.zone = customer.try(:time_zone)
+ Time.zone = customer&.time_zone
end
end
RUBY
@@ -53,7 +53,7 @@ class CurrentAttributesIntegrationTest < ActiveSupport::TestCase
RUBY
app_file "app/views/customers/index.html.erb", <<-RUBY
- <%= Current.customer.try(:name) || 'noone' %>,<%= Time.zone.name %>
+ <%= Current.customer&.name || 'noone' %>,<%= Time.zone.name %>
RUBY
require "#{app_path}/config/environment"