Skip to content

Commit 2efd780

Browse files
committed
send() will raise an ArgumentError, so we should leverage ruby
1 parent 6e63e7a commit 2efd780

File tree

2 files changed

+7
-17
lines changed

2 files changed

+7
-17
lines changed

activerecord/lib/active_record/aggregations.rb

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,9 @@ def reader_method(name, class_name, mapping, allow_nil, constructor)
224224
define_method(name) do
225225
if @aggregation_cache[name].nil? && (!allow_nil || mapping.any? {|pair| !read_attribute(pair.first).nil? })
226226
attrs = mapping.collect {|pair| read_attribute(pair.first)}
227-
object = case constructor
228-
when Symbol
229-
class_name.constantize.send(constructor, *attrs)
230-
when Proc, Method
231-
constructor.call(*attrs)
232-
else
233-
raise ArgumentError, 'Constructor must be a symbol denoting the constructor method to call or a Proc to be invoked.'
234-
end
227+
object = constructor.respond_to?(:call) ?
228+
constructor.call(*attrs) :
229+
class_name.constantize.send(constructor, *attrs)
235230
@aggregation_cache[name] = object
236231
end
237232
@aggregation_cache[name]
@@ -248,14 +243,9 @@ def writer_method(name, class_name, mapping, allow_nil, converter)
248243
@aggregation_cache[name] = nil
249244
else
250245
unless part.is_a?(class_name.constantize) || converter.nil?
251-
part = case converter
252-
when Symbol
253-
class_name.constantize.send(converter, part)
254-
when Proc, Method
255-
converter.call(part)
256-
else
257-
raise ArgumentError, 'Converter must be a symbol denoting the converter method to call or a Proc to be invoked.'
258-
end
246+
part = converter.respond_to?(:call) ?
247+
converter.call(part) :
248+
class_name.constantize.send(converter, part)
259249
end
260250

261251
mapping.each { |pair| self[pair.first] = part.send(pair.last) }

activerecord/test/models/customer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,4 @@ def initialize(first, last = nil)
7070
def to_s
7171
"#{first} #{last.upcase}"
7272
end
73-
end
73+
end

0 commit comments

Comments
 (0)