Skip to content

Commit 2714024

Browse files
committed
Cleanup defined? usage
Now that we dropped support for Ruby 2.7, we no longer need to check if variables are defined before accessing them to avoid the undefined variable warning.
1 parent ccabc05 commit 2714024

File tree

21 files changed

+45
-57
lines changed

21 files changed

+45
-57
lines changed

actionpack/lib/action_dispatch/testing/assertions/routing.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def assert_routing(path, options, defaults = {}, extras = {}, message = nil)
248248

249249
# ROUTES TODO: These assertions should really work in an integration context
250250
def method_missing(selector, ...)
251-
if defined?(@controller) && @controller && defined?(@routes) && @routes && @routes.named_routes.route_defined?(selector)
251+
if @controller && @routes&.named_routes.route_defined?(selector)
252252
@controller.public_send(selector, ...)
253253
else
254254
super
@@ -258,7 +258,7 @@ def method_missing(selector, ...)
258258
private
259259
def create_routes
260260
@routes = ActionDispatch::Routing::RouteSet.new
261-
if defined?(@controller) && @controller
261+
if @controller
262262
@controller = @controller.clone
263263
_routes = @routes
264264

@@ -282,7 +282,7 @@ def create_routes
282282

283283
def reset_routes(old_routes, old_controller)
284284
@routes = old_routes
285-
if defined?(@controller) && @controller
285+
if @controller
286286
@controller = old_controller
287287
end
288288
end

actionview/lib/action_view/test_case.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def method_missing(selector, ...)
433433

434434
def respond_to_missing?(name, include_private = false)
435435
begin
436-
routes = defined?(@controller) && @controller.respond_to?(:_routes) && @controller._routes
436+
routes = @controller.respond_to?(:_routes) && @controller._routes
437437
rescue
438438
# Don't call routes, if there is an error on _routes call
439439
end

activejob/lib/active_job/core.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ def deserialize_arguments(serialized_args)
196196
end
197197

198198
def arguments_serialized?
199-
defined?(@serialized_arguments) && @serialized_arguments
199+
@serialized_arguments
200200
end
201201
end
202202
end

activemodel/lib/active_model/attribute.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def original_value_for_database
153153
alias :assigned? :original_attribute
154154

155155
def initialize_dup(other)
156-
if defined?(@value) && @value.duplicable?
156+
if @value&.duplicable?
157157
@value = @value.dup
158158
end
159159
end

activerecord/lib/active_record/associations/preloader/through_association.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ def preloaded_records
99
end
1010

1111
def records_by_owner
12-
return @records_by_owner if defined?(@records_by_owner)
13-
14-
@records_by_owner = owners.each_with_object({}) do |owner, result|
12+
@records_by_owner ||= owners.each_with_object({}) do |owner, result|
1513
if loaded?(owner)
1614
result[owner] = target_for(owner)
1715
next

activerecord/lib/active_record/attribute_methods.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def define_attribute_methods # :nodoc:
137137

138138
def undefine_attribute_methods # :nodoc:
139139
GeneratedAttributeMethods::LOCK.synchronize do
140-
super if defined?(@attribute_methods_generated) && @attribute_methods_generated
140+
super if @attribute_methods_generated
141141
@attribute_methods_generated = false
142142
@alias_attributes_mass_generated = false
143143
end
@@ -288,9 +288,7 @@ def respond_to?(name, include_private = false)
288288

289289
# If the result is true then check for the select case.
290290
# For queries selecting a subset of columns, return false for unselected columns.
291-
# We check defined?(@attributes) not to issue warnings if called on objects that
292-
# have been allocated but not yet initialized.
293-
if defined?(@attributes)
291+
if @attributes
294292
if name = self.class.symbol_column_to_string(name.to_sym)
295293
return _has_attribute?(name)
296294
end
@@ -460,8 +458,7 @@ def accessed_fields
460458

461459
private
462460
def attribute_method?(attr_name)
463-
# We check defined? because Syck calls respond_to? before actually calling initialize.
464-
defined?(@attributes) && @attributes.key?(attr_name)
461+
@attributes&.key?(attr_name)
465462
end
466463

467464
def attributes_with_values(attribute_names)

activerecord/lib/active_record/connection_handling.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def connection
258258

259259
# Return the connection specification name from the current class or its parent.
260260
def connection_specification_name
261-
if !defined?(@connection_specification_name) || @connection_specification_name.nil?
261+
if @connection_specification_name.nil?
262262
return self == Base ? Base.name : superclass.connection_specification_name
263263
end
264264
@connection_specification_name

activerecord/lib/active_record/core.rb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,7 @@ def full_inspect
720720
def pretty_print(pp)
721721
return super if custom_inspect_method_defined?
722722
pp.object_address_group(self) do
723-
if defined?(@attributes) && @attributes
723+
if @attributes
724724
attr_names = attributes_for_inspect.select { |name| _has_attribute?(name.to_s) }
725725
pp.seplist(attr_names, proc { pp.text "," }) do |attr_name|
726726
attr_name = attr_name.to_s
@@ -791,9 +791,7 @@ def inspection_filter
791791
end
792792

793793
def inspect_with_attributes(attributes_to_list)
794-
# We check defined?(@attributes) not to issue warnings if the object is
795-
# allocated but not initialized.
796-
inspection = if defined?(@attributes) && @attributes
794+
inspection = if @attributes
797795
attributes_to_list.filter_map do |name|
798796
name = name.to_s
799797
if _has_attribute?(name)

activerecord/lib/active_record/inheritance.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ def base_class?
165165

166166
# Returns whether this class is an abstract class or not.
167167
def abstract_class?
168-
defined?(@abstract_class) && @abstract_class == true
168+
@abstract_class == true
169169
end
170170

171171
# Sets the application record class for Active Record

activerecord/lib/active_record/model_schema.rb

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ def table_name=(value)
273273
@table_name = value
274274
@quoted_table_name = nil
275275
@arel_table = nil
276-
@sequence_name = nil unless defined?(@explicit_sequence_name) && @explicit_sequence_name
276+
@sequence_name = nil unless @explicit_sequence_name
277277
@predicate_builder = nil
278278
end
279279

@@ -414,11 +414,10 @@ def table_exists?
414414
end
415415

416416
def attributes_builder # :nodoc:
417-
unless defined?(@attributes_builder) && @attributes_builder
417+
@attributes_builder ||= begin
418418
defaults = _default_attributes.except(*(column_names - [primary_key]))
419-
@attributes_builder = ActiveModel::AttributeSet::Builder.new(attribute_types, defaults)
419+
ActiveModel::AttributeSet::Builder.new(attribute_types, defaults)
420420
end
421-
@attributes_builder
422421
end
423422

424423
def columns_hash # :nodoc:
@@ -573,7 +572,7 @@ def inherited(child_class)
573572
end
574573

575574
def schema_loaded?
576-
defined?(@schema_loaded) && @schema_loaded
575+
@schema_loaded
577576
end
578577

579578
def load_schema!

0 commit comments

Comments
 (0)