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
|
end
|
||||||
|
|
||||||
def mailbox_for(inbound_email)
|
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -17,7 +17,7 @@ module ActionController
|
||||||
# of cached pages.
|
# of cached pages.
|
||||||
#
|
#
|
||||||
# class InvoicesController < ApplicationController
|
# class InvoicesController < ApplicationController
|
||||||
# etag { current_user.try :id }
|
# etag { current_user&.id }
|
||||||
#
|
#
|
||||||
# def show
|
# def show
|
||||||
# # Etag will differ even for the same invoice when it's viewed by a different current_user
|
# # 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
|
end
|
||||||
|
|
||||||
def current_content_security_policy
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -42,7 +42,7 @@ module ActionDispatch #:nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def policy_empty?(policy)
|
def policy_empty?(policy)
|
||||||
policy.try(:directives) && policy.directives.empty?
|
policy&.directives&.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -47,14 +47,14 @@ module ActionDispatch
|
||||||
case type
|
case type
|
||||||
when :chrome
|
when :chrome
|
||||||
if ::Selenium::WebDriver::Service.respond_to? :driver_path=
|
if ::Selenium::WebDriver::Service.respond_to? :driver_path=
|
||||||
::Selenium::WebDriver::Chrome::Service.driver_path.try(:call)
|
::Selenium::WebDriver::Chrome::Service.driver_path&.call
|
||||||
else
|
else
|
||||||
# Selenium <= v3.141.0
|
# Selenium <= v3.141.0
|
||||||
::Selenium::WebDriver::Chrome.driver_path
|
::Selenium::WebDriver::Chrome.driver_path
|
||||||
end
|
end
|
||||||
when :firefox
|
when :firefox
|
||||||
if ::Selenium::WebDriver::Service.respond_to? :driver_path=
|
if ::Selenium::WebDriver::Service.respond_to? :driver_path=
|
||||||
::Selenium::WebDriver::Firefox::Service.driver_path.try(:call)
|
::Selenium::WebDriver::Firefox::Service.driver_path&.call
|
||||||
else
|
else
|
||||||
# Selenium <= v3.141.0
|
# Selenium <= v3.141.0
|
||||||
::Selenium::WebDriver::Firefox.driver_path
|
::Selenium::WebDriver::Firefox.driver_path
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "active_support/core_ext/object/try"
|
|
||||||
require "rails-html-sanitizer"
|
require "rails-html-sanitizer"
|
||||||
|
|
||||||
module ActionView
|
module ActionView
|
||||||
|
@ -80,7 +79,7 @@ module ActionView
|
||||||
# config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a']
|
# config.action_view.sanitized_allowed_tags = ['strong', 'em', 'a']
|
||||||
# config.action_view.sanitized_allowed_attributes = ['href', 'title']
|
# config.action_view.sanitized_allowed_attributes = ['href', 'title']
|
||||||
def sanitize(html, options = {})
|
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
|
end
|
||||||
|
|
||||||
# Sanitizes a block of CSS code. Used by +sanitize+ when it comes across a style attribute.
|
# 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:
|
class DateField < DatetimeField # :nodoc:
|
||||||
private
|
private
|
||||||
def format_date(value)
|
def format_date(value)
|
||||||
value.try(:strftime, "%Y-%m-%d")
|
value&.strftime("%Y-%m-%d")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,7 +12,7 @@ module ActionView
|
||||||
|
|
||||||
private
|
private
|
||||||
def format_date(value)
|
def format_date(value)
|
||||||
value.try(:strftime, "%Y-%m-%dT%T")
|
value&.strftime("%Y-%m-%dT%T")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,7 +6,7 @@ module ActionView
|
||||||
class MonthField < DatetimeField # :nodoc:
|
class MonthField < DatetimeField # :nodoc:
|
||||||
private
|
private
|
||||||
def format_date(value)
|
def format_date(value)
|
||||||
value.try(:strftime, "%Y-%m")
|
value&.strftime("%Y-%m")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,7 +6,7 @@ module ActionView
|
||||||
class TimeField < DatetimeField # :nodoc:
|
class TimeField < DatetimeField # :nodoc:
|
||||||
private
|
private
|
||||||
def format_date(value)
|
def format_date(value)
|
||||||
value.try(:strftime, "%T.%L")
|
value&.strftime("%T.%L")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,7 +6,7 @@ module ActionView
|
||||||
class WeekField < DatetimeField # :nodoc:
|
class WeekField < DatetimeField # :nodoc:
|
||||||
private
|
private
|
||||||
def format_date(value)
|
def format_date(value)
|
||||||
value.try(:strftime, "%Y-W%V")
|
value&.strftime("%Y-W%V")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -62,7 +62,7 @@ module ActionView
|
||||||
output = ActionView::StreamingBuffer.new(buffer)
|
output = ActionView::StreamingBuffer.new(buffer)
|
||||||
yielder = lambda { |*name| view._layout_for(*name) }
|
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
|
outer_config = I18n.config
|
||||||
fiber = Fiber.new do
|
fiber = Fiber.new do
|
||||||
I18n.config = outer_config
|
I18n.config = outer_config
|
||||||
|
|
|
@ -1,7 +1,5 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "active_support/core_ext/object/try"
|
|
||||||
|
|
||||||
module ActionView
|
module ActionView
|
||||||
class TemplateRenderer < AbstractRenderer #:nodoc:
|
class TemplateRenderer < AbstractRenderer #:nodoc:
|
||||||
def render(context, options)
|
def render(context, options)
|
||||||
|
@ -54,7 +52,7 @@ module ActionView
|
||||||
# supplied as well.
|
# supplied as well.
|
||||||
def render_template(view, template, layout_name, locals)
|
def render_template(view, template, layout_name, locals)
|
||||||
render_with_layout(view, template, layout_name, locals) do |layout|
|
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) }
|
template.render(view, locals) { |*name| view._layout_for(*name) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -100,7 +100,7 @@ module ActiveJob
|
||||||
"executions" => executions,
|
"executions" => executions,
|
||||||
"exception_executions" => exception_executions,
|
"exception_executions" => exception_executions,
|
||||||
"locale" => I18n.locale.to_s,
|
"locale" => I18n.locale.to_s,
|
||||||
"timezone" => Time.zone.try(:name),
|
"timezone" => Time.zone&.name,
|
||||||
"enqueued_at" => Time.now.utc.iso8601
|
"enqueued_at" => Time.now.utc.iso8601
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
@ -140,7 +140,7 @@ module ActiveJob
|
||||||
self.executions = job_data["executions"]
|
self.executions = job_data["executions"]
|
||||||
self.exception_executions = job_data["exception_executions"]
|
self.exception_executions = job_data["exception_executions"]
|
||||||
self.locale = job_data["locale"] || I18n.locale.to_s
|
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"]
|
self.enqueued_at = job_data["enqueued_at"]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ class TestJob < ActiveJob::Base
|
||||||
File.open(Rails.root.join("tmp/\#{x}.new"), "wb+") do |f|
|
File.open(Rails.root.join("tmp/\#{x}.new"), "wb+") do |f|
|
||||||
f.write Marshal.dump({
|
f.write Marshal.dump({
|
||||||
"locale" => I18n.locale.to_s || "en",
|
"locale" => I18n.locale.to_s || "en",
|
||||||
"timezone" => Time.zone.try(:name) || "UTC",
|
"timezone" => Time.zone&.name || "UTC",
|
||||||
"executed_at" => Time.now.to_r
|
"executed_at" => Time.now.to_r
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
|
|
@ -56,8 +56,8 @@ module ActiveModel
|
||||||
# user.authenticate('notright') # => false
|
# user.authenticate('notright') # => false
|
||||||
# user.authenticate('mUc3m00RsqyRe') # => user
|
# user.authenticate('mUc3m00RsqyRe') # => user
|
||||||
# user.authenticate_recovery_password('42password') # => user
|
# user.authenticate_recovery_password('42password') # => user
|
||||||
# User.find_by(name: 'david').try(:authenticate, 'notright') # => false
|
# User.find_by(name: 'david')&.authenticate('notright') # => false
|
||||||
# User.find_by(name: 'david').try(:authenticate, 'mUc3m00RsqyRe') # => user
|
# User.find_by(name: 'david')&.authenticate('mUc3m00RsqyRe') # => user
|
||||||
def has_secure_password(attribute = :password, validations: true)
|
def has_secure_password(attribute = :password, validations: true)
|
||||||
# Load bcrypt gem only when has_secure_password is used.
|
# Load bcrypt gem only when has_secure_password is used.
|
||||||
# This is to avoid ActiveModel (and by extension the entire framework)
|
# This is to avoid ActiveModel (and by extension the entire framework)
|
||||||
|
|
|
@ -44,7 +44,7 @@ module ActiveRecord
|
||||||
|
|
||||||
def decrement_counters_before_last_save
|
def decrement_counters_before_last_save
|
||||||
if reflection.polymorphic?
|
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
|
else
|
||||||
model_was = klass
|
model_was = klass
|
||||||
end
|
end
|
||||||
|
|
|
@ -24,7 +24,7 @@ WARNING: Rails was not able to disable referential integrity.
|
||||||
This is most likely caused due to missing permissions.
|
This is most likely caused due to missing permissions.
|
||||||
Rails needs superuser privileges to disable referential integrity.
|
Rails needs superuser privileges to disable referential integrity.
|
||||||
|
|
||||||
cause: #{original_exception.try(:message)}
|
cause: #{original_exception&.message}
|
||||||
|
|
||||||
WARNING
|
WARNING
|
||||||
raise e
|
raise e
|
||||||
|
|
|
@ -152,7 +152,7 @@ module ActiveRecord
|
||||||
def build_db_config_from_string(env_name, spec_name, config)
|
def build_db_config_from_string(env_name, spec_name, config)
|
||||||
url = config
|
url = config
|
||||||
uri = URI.parse(url)
|
uri = URI.parse(url)
|
||||||
if uri.try(:scheme)
|
if uri&.scheme
|
||||||
ActiveRecord::DatabaseConfigurations::UrlConfig.new(env_name, spec_name, url)
|
ActiveRecord::DatabaseConfigurations::UrlConfig.new(env_name, spec_name, url)
|
||||||
end
|
end
|
||||||
rescue URI::InvalidURIError
|
rescue URI::InvalidURIError
|
||||||
|
|
|
@ -106,7 +106,7 @@ module ActiveRecord
|
||||||
# Wraps the underlying database error as +cause+.
|
# Wraps the underlying database error as +cause+.
|
||||||
class StatementInvalid < ActiveRecordError
|
class StatementInvalid < ActiveRecordError
|
||||||
def initialize(message = nil, sql: nil, binds: nil)
|
def initialize(message = nil, sql: nil, binds: nil)
|
||||||
super(message || $!.try(:message))
|
super(message || $!&.message)
|
||||||
@sql = sql
|
@sql = sql
|
||||||
@binds = binds
|
@binds = binds
|
||||||
end
|
end
|
||||||
|
|
|
@ -11,7 +11,7 @@ module ActiveRecord #:nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def serializable_hash(options = nil)
|
def serializable_hash(options = nil)
|
||||||
options = options.try(:dup) || {}
|
options = options ? options.dup : {}
|
||||||
|
|
||||||
options[:except] = Array(options[:except]).map(&:to_s)
|
options[:except] = Array(options[:except]).map(&:to_s)
|
||||||
options[:except] |= Array(self.class.inheritance_column)
|
options[:except] |= Array(self.class.inheritance_column)
|
||||||
|
|
|
@ -1932,7 +1932,7 @@ class RelationTest < ActiveRecord::TestCase
|
||||||
|
|
||||||
assert_no_queries do
|
assert_no_queries do
|
||||||
result = authors_count.map do |post|
|
result = authors_count.map do |post|
|
||||||
[post.num_posts, post.author.try(:name)]
|
[post.num_posts, post.author&.name]
|
||||||
end
|
end
|
||||||
|
|
||||||
expected = [[1, nil], [5, "David"], [3, "Mary"], [2, "Bob"]]
|
expected = [[1, nil], [5, "David"], [3, "Mary"], [2, "Bob"]]
|
||||||
|
|
|
@ -9,7 +9,7 @@ class CurrentAttributesTest < ActiveSupport::TestCase
|
||||||
attribute :world, :account, :person, :request
|
attribute :world, :account, :person, :request
|
||||||
delegate :time_zone, to: :person
|
delegate :time_zone, to: :person
|
||||||
|
|
||||||
before_reset { Session.previous = person.try(:id) }
|
before_reset { Session.previous = person&.id }
|
||||||
|
|
||||||
resets do
|
resets do
|
||||||
Time.zone = "UTC"
|
Time.zone = "UTC"
|
||||||
|
@ -18,13 +18,13 @@ class CurrentAttributesTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
def account=(account)
|
def account=(account)
|
||||||
super
|
super
|
||||||
self.person = "#{account}'s person"
|
self.person = Person.new(1, "#{account}'s person")
|
||||||
end
|
end
|
||||||
|
|
||||||
def person=(person)
|
def person=(person)
|
||||||
super
|
super
|
||||||
Time.zone = person.try(:time_zone)
|
Time.zone = person&.time_zone
|
||||||
Session.current = person.try(:id)
|
Session.current = person&.id
|
||||||
end
|
end
|
||||||
|
|
||||||
def request
|
def request
|
||||||
|
@ -63,7 +63,7 @@ class CurrentAttributesTest < ActiveSupport::TestCase
|
||||||
test "set attribute via overwritten method" do
|
test "set attribute via overwritten method" do
|
||||||
Current.account = "account/1"
|
Current.account = "account/1"
|
||||||
assert_equal "account/1", Current.account
|
assert_equal "account/1", Current.account
|
||||||
assert_equal "account/1's person", Current.person
|
assert_equal "account/1's person", Current.person.name
|
||||||
end
|
end
|
||||||
|
|
||||||
test "set auxiliary class via overwritten method" do
|
test "set auxiliary class via overwritten method" do
|
||||||
|
|
|
@ -18,7 +18,7 @@ class CurrentAttributesIntegrationTest < ActiveSupport::TestCase
|
||||||
|
|
||||||
def customer=(customer)
|
def customer=(customer)
|
||||||
super
|
super
|
||||||
Time.zone = customer.try(:time_zone)
|
Time.zone = customer&.time_zone
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
RUBY
|
RUBY
|
||||||
|
@ -53,7 +53,7 @@ class CurrentAttributesIntegrationTest < ActiveSupport::TestCase
|
||||||
RUBY
|
RUBY
|
||||||
|
|
||||||
app_file "app/views/customers/index.html.erb", <<-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
|
RUBY
|
||||||
|
|
||||||
require "#{app_path}/config/environment"
|
require "#{app_path}/config/environment"
|
||||||
|
|
Loading…
Reference in a new issue