Module: Mongoid::MultiParameterAttributes
- Defined in:
- lib/mongoid_multiparams.rb
Overview
Adds Rails’ multi-parameter attribute support to Mongoid.
@todo: Durran: This module needs an overhaul.
Defined Under Namespace
Modules: Errors
Instance Method Summary collapse
-
#process_attributes(attrs = nil) ⇒ Object
Process the provided attributes casting them to their proper values if a field exists for them on the document.
Instance Method Details
#process_attributes(attrs = nil) ⇒ Object
Process the provided attributes casting them to their proper values if a field exists for them on the document. This will be limited to only the attributes provided in the suppied Hash so that no extra nil values get put into the document’s attributes.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/mongoid_multiparams.rb', line 79 def process_attributes(attrs = nil) if attrs errors = [] attributes = attrs.class.new attributes.permit! if attrs.respond_to?(:permitted?) && attrs.permitted? multi_parameter_attributes = {} attrs.each_pair do |key, value| if key =~ /\A([^\(]+)\((\d+)([if])\)$/ key, index = $1, $2.to_i (multi_parameter_attributes[key] ||= {})[index] = value.empty? ? nil : value.send("to_#{$3}") else attributes[key] = value end end multi_parameter_attributes.each_pair do |key, values| begin values = (values.keys.min..values.keys.max).map { |i| values[i] } field = self.class.fields[database_field_name(key)] attributes[key] = instantiate_object(field, values) rescue => e errors << Errors::AttributeAssignmentError.new( "error on assignment #{values.inspect} to #{key}", e, key ) end end unless errors.empty? raise Errors::MultiparameterAssignmentErrors.new(errors), "#{errors.size} error(s) on assignment of multiparameter attributes" end super(attributes) else super end end |