Skip to content

Commit d1c53a9

Browse files
committed
reducing function calls and using faster methods for testing
1 parent 87a28e3 commit d1c53a9

File tree

2 files changed

+10
-16
lines changed

2 files changed

+10
-16
lines changed

activerecord/lib/active_record/relation.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def many?
9999
if block_given?
100100
to_a.many? { |*block_args| yield(*block_args) }
101101
else
102-
@limit_value.present? ? to_a.many? : size > 1
102+
@limit_value ? to_a.many? : size > 1
103103
end
104104
end
105105

@@ -316,12 +316,12 @@ def to_sql
316316

317317
def scope_for_create
318318
@scope_for_create ||= begin
319-
@create_with_value || @where_values.inject({}) do |hash, where|
320-
if where.is_a?(Arel::Predicates::Equality)
321-
hash[where.operand1.name] = where.operand2.respond_to?(:value) ? where.operand2.value : where.operand2
322-
end
323-
hash
324-
end
319+
@create_with_value || Hash[
320+
@where_values.grep(Arel::Predicates::Equality).map { |where|
321+
[where.operand1.name,
322+
where.operand2.respond_to?(:value) ?
323+
where.operand2.value : where.operand2]
324+
}]
325325
end
326326
end
327327

activerecord/lib/active_record/relation/query_methods.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ def custom_join_sql(*joins)
129129
def build_arel
130130
arel = table
131131

132-
arel = build_joins(arel, @joins_values) if @joins_values.present?
132+
arel = build_joins(arel, @joins_values) unless @joins_values.empty?
133133

134134
@where_values.uniq.each do |where|
135135
next if where.blank?
@@ -145,7 +145,7 @@ def build_arel
145145

146146
arel = arel.having(*@having_values.uniq.select{|h| h.present?}) if @having_values.present?
147147

148-
arel = arel.take(@limit_value) if @limit_value.present?
148+
arel = arel.take(@limit_value) if @limit_value
149149
arel = arel.skip(@offset_value) if @offset_value.present?
150150

151151
arel = arel.group(*@group_values.uniq.select{|g| g.present?}) if @group_values.present?
@@ -155,13 +155,7 @@ def build_arel
155155
arel = build_select(arel, @select_values.uniq)
156156

157157
arel = arel.from(@from_value) if @from_value.present?
158-
159-
case @lock_value
160-
when TrueClass
161-
arel = arel.lock
162-
when String
163-
arel = arel.lock(@lock_value)
164-
end if @lock_value.present?
158+
arel = arel.lock(@lock_value) if @lock_value
165159

166160
arel
167161
end

0 commit comments

Comments
 (0)