Skip to content

Commit

Permalink
Avoid calling defined?(ActiveRecord) multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Jan 29, 2023
1 parent fe75b7b commit 3f79a68
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/active_decorator/decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,23 @@ def decorate(obj)
when nil, true, false
# Do nothing
else
if defined?(ActiveRecord) && ((ActiveRecord::Relation === obj) || obj.is_a?(ActiveRecord::Relation))
# don't call each nor to_a immediately
if obj.respond_to?(:records)
# Rails 5.0
obj.extend ActiveDecorator::RelationDecorator unless (ActiveDecorator::RelationDecorator === obj)
else
# Rails 3.x and 4.x
obj.extend ActiveDecorator::RelationDecoratorLegacy unless (ActiveDecorator::RelationDecoratorLegacy === obj)
if defined? ActiveRecord
if obj.is_a? ActiveRecord::Relation
# don't call each nor to_a immediately
if obj.respond_to?(:records)
# Rails 5.0
return obj.extend ActiveDecorator::RelationDecorator unless ActiveDecorator::RelationDecorator === obj
else
# Rails 3.x and 4.x
return obj.extend ActiveDecorator::RelationDecoratorLegacy unless ActiveDecorator::RelationDecoratorLegacy === obj
end
elsif ActiveRecord::Base === obj
obj.extend ActiveDecorator::Decorated unless ActiveDecorator::Decorated === obj
end
else
obj.extend ActiveDecorator::Decorated if defined?(ActiveRecord) && (ActiveRecord::Base === obj) && !(ActiveDecorator::Decorated === obj)

d = decorator_for obj.class
obj.extend d if d && !(d === obj)
end

d = decorator_for obj.class
obj.extend d if d && !(d === obj)
end

obj
Expand Down

0 comments on commit 3f79a68

Please sign in to comment.