@@ -155,6 +155,41 @@ def constraint_error(klass, column_name)
155155 "Malformed constraint `#{ klass } ##{ column_name } '. If it's a legitimate column, and you are using a nested scaffold, please specify or double-check the reverse association name."
156156 end
157157
158+ def apply_constraint_on_association ( record , association , value , allow_autosave : false )
159+ if association . through_singular? && association . source_reflection . reverse
160+ create_on_through_singular ( record , association , association . klass . find ( value ) )
161+ elsif association . collection?
162+ record . send ( k . to_s ) . send ( :<< , association . klass . find ( value ) ) unless association . nested?
163+ elsif association . polymorphic?
164+ apply_constraint_on_polymorphic_association ( record , association , value )
165+ elsif !association . source_reflection &.through? && # regular singular association, or one-level through association
166+ !value . is_a? ( Array )
167+ record . send ( :"#{ k } =" , association . klass . find ( value ) )
168+
169+ # setting the belongs_to side of a has_one isn't safe. if the has_one was already
170+ # specified, rails won't automatically clear out the previous associated record.
171+ #
172+ # note that we can't take the extra step to correct this unless we're permitted to
173+ # run operations where activerecord auto-saves the object.
174+ reverse = association . reverse_association
175+ if reverse &.singular? && !reverse . belongs_to? && allow_autosave
176+ record . send ( k ) . send ( :"#{ reverse . name } =" , record )
177+ end
178+ end
179+ end
180+
181+ def apply_constraint_on_polymorphic_association ( record , association , value )
182+ unless value . is_a? ( Array ) && value . size >= 2
183+ raise ActiveScaffold ::MalformedConstraint , polymorphic_constraint_error ( association ) , caller
184+ end
185+
186+ if value . size == 2
187+ record . send ( :"#{ association . name } =" , value [ 0 ] . constantize . find ( value [ 1 ] ) )
188+ else
189+ record . send ( :"#{ association . foreign_type } =" , value [ 0 ] )
190+ end
191+ end
192+
158193 # Applies constraints to the given record.
159194 #
160195 # Searches through the known columns for association columns. If the given constraint is an association,
@@ -163,42 +198,12 @@ def constraint_error(klass, column_name)
163198 #
164199 # For some operations ActiveRecord will automatically update the database. That's not always ok.
165200 # If it *is* ok (e.g. you're in a transaction), then set :allow_autosave to true.
166- def apply_constraints_to_record ( record , options = { } )
167- options [ :allow_autosave ] = false if options [ :allow_autosave ] . nil?
168- constraints = options [ :constraints ] || active_scaffold_constraints
169-
201+ def apply_constraints_to_record ( record , allow_autosave : false , constraints : active_scaffold_constraints )
170202 config = record . is_a? ( active_scaffold_config . model ) ? active_scaffold_config : active_scaffold_config_for ( record . class )
171203 constraints . each do |k , v |
172204 column = config . columns [ k ]
173205 if column &.association
174- if column . association . through_singular? && column . association . source_reflection . reverse
175- create_on_through_singular ( record , column . association , column . association . klass . find ( v ) )
176- elsif column . association . collection?
177- record . send ( k . to_s ) . send ( :<< , column . association . klass . find ( v ) ) unless column . association . nested?
178- elsif column . association . polymorphic?
179- unless v . is_a? ( Array ) && v . size >= 2
180- raise ActiveScaffold ::MalformedConstraint , polymorphic_constraint_error ( column . association ) , caller
181- end
182-
183- if v . size == 2
184- record . send ( :"#{ k } =" , v [ 0 ] . constantize . find ( v [ 1 ] ) )
185- else
186- record . send ( :"#{ column . association . foreign_type } =" , v [ 0 ] )
187- end
188- elsif !column . association . source_reflection &.through? && # regular singular association, or one-level through association
189- !v . is_a? ( Array )
190- record . send ( :"#{ k } =" , column . association . klass . find ( v ) )
191-
192- # setting the belongs_to side of a has_one isn't safe. if the has_one was already
193- # specified, rails won't automatically clear out the previous associated record.
194- #
195- # note that we can't take the extra step to correct this unless we're permitted to
196- # run operations where activerecord auto-saves the object.
197- reverse = column . association . reverse_association
198- if reverse &.singular? && !reverse . belongs_to? && options [ :allow_autosave ]
199- record . send ( k ) . send ( :"#{ reverse . name } =" , record )
200- end
201- end
206+ apply_constraint_on_association ( record , column . association , v , allow_autosave : allow_autosave )
202207 else
203208 record . send ( :"#{ k } =" , v )
204209 end
0 commit comments