instance method
writer_method
Ruby on Rails 7.2.3
Since v2.2.3 PrivateSignature
writer_method(name, class_name, mapping, allow_nil, converter)
No documentation comment.
Parameters
-
namereq -
class_namereq -
mappingreq -
allow_nilreq -
converterreq
Source
# File activerecord/lib/active_record/aggregations.rb, line 261
def writer_method(name, class_name, mapping, allow_nil, converter)
define_method("#{name}=") do |part|
klass = class_name.constantize
unless part.is_a?(klass) || converter.nil? || part.nil?
part = converter.respond_to?(:call) ? converter.call(part) : klass.send(converter, part)
end
hash_from_multiparameter_assignment = part.is_a?(Hash) &&
part.keys.all?(Integer)
if hash_from_multiparameter_assignment
raise ArgumentError unless part.size == part.each_key.max
part = klass.new(*part.sort.map(&:last))
end
if part.nil? && allow_nil
mapping.each { |key, _| write_attribute(key, nil) }
@aggregation_cache[name] = nil
else
mapping.each { |key, value| write_attribute(key, part.send(value)) }
@aggregation_cache[name] = part.dup.freeze
end
end
end
Defined in activerecord/lib/active_record/aggregations.rb line 261
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Aggregations::ClassMethods