mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Use try
only when we're unsure if the receiver would respond_to the method
This commit is contained in:
parent
dbf3e4882f
commit
af2129b4c7
24 changed files with 40 additions and 43 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -42,7 +42,7 @@ module ActionDispatch #:nodoc:
|
|||
end
|
||||
|
||||
def policy_empty?(policy)
|
||||
policy.try(:directives) && policy.directives.empty?
|
||||
policy&.directives&.empty?
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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"]]
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue