@@ -48,7 +48,7 @@ def create
4848 @resource = resource = new_resource ( resource_params )
4949 authorize_resource ( resource )
5050
51- if resource . save
51+ if resource . save ( context : validation_contexts_on_create ( resource ) )
5252 yield ( resource ) if block_given?
5353 redirect_to (
5454 after_resource_created_path ( resource ) ,
@@ -63,7 +63,9 @@ def create
6363
6464 def update
6565 @resource = resource = requested_resource
66- if resource . update ( resource_params )
66+ resource . assign_attributes ( resource_params )
67+
68+ if resource . save ( context : validation_contexts_on_update ( resource ) )
6769 redirect_to (
6870 after_resource_updated_path ( resource ) ,
6971 notice : translate_with_resource ( "update.success" ) ,
@@ -277,6 +279,33 @@ def authorize_resource(resource)
277279 end
278280 end
279281
282+ # Override this if you want to contextualize the resource differently.
283+ #
284+ # @param resource A resource to be contextualized.
285+ # @return nothing
286+ def contextualize_resource ( resource )
287+ end
288+
289+ # Override this if you want to provide additional validation contexts.
290+ #
291+ # @param resource [ActiveRecord::Base] The resource to be validated.
292+ # @return [Array<Symbol>] The validation contexts to be used.
293+ def validation_contexts_on_create ( resource )
294+ default_validation_contexts ( resource )
295+ end
296+
297+ # Override this if you want to provide additional validation contexts.
298+ #
299+ # @param resource [ActiveRecord::Base] The resource to be validated.
300+ # @return [Array<Symbol>] The validation contexts to be used.
301+ def validation_contexts_on_update ( resource )
302+ default_validation_contexts ( resource )
303+ end
304+
305+ def default_validation_contexts ( resource )
306+ resource . new_record? ? [ :create ] : [ :update ]
307+ end
308+
280309 def paginate_resources ( resources )
281310 resources . page ( params [ :_page ] ) . per ( records_per_page )
282311 end
0 commit comments