From af2129b4c74732c88ffce76e5c55c805cb9431f6 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 1 Aug 2019 16:41:26 +0900 Subject: Use `try` only when we're unsure if the receiver would respond_to the method --- activerecord/lib/active_record/associations/belongs_to_association.rb | 2 +- .../connection_adapters/postgresql/referential_integrity.rb | 2 +- activerecord/lib/active_record/database_configurations.rb | 2 +- activerecord/lib/active_record/errors.rb | 2 +- activerecord/lib/active_record/serialization.rb | 2 +- activerecord/test/cases/relations_test.rb | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) (limited to 'activerecord') 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"]] -- cgit v1.2.3