lib/azeroth/request_handler/update.rb in azeroth-0.8.2 vs lib/azeroth/request_handler/update.rb in azeroth-0.9.0
- old
+ new
@@ -6,30 +6,45 @@
#
# hadler for requests to update resource
class Update < RequestHandler
private
+ delegate :update_with, to: :options
+
# @private
#
# Updates and return an instance of the model
#
# update uses the method +"#{model.name}_params"+ to
# fetch all allowed attributes for update
#
# @return [Object]
def resource
- @resource ||= update_resource
+ @resource ||= perform_update
end
- # build resource for update
+ # Update a resource saving it to the database
#
- # @return [Object]
- def update_resource
- controller.send(model.name).tap do |entry|
+ # @return [Object] updated resource
+ def perform_update
+ @resource = controller.send(model.name)
+ resource.tap do
trigger_event(:save) do
- entry.update(attributes)
+ update_and_save_resource
end
end
+ end
+
+ # @private
+ #
+ # Update the resource, either by running update_with
+ # or directly updating the attributes in the object
+ #
+ # @return [Object] updated resource
+ def update_and_save_resource
+ return resource.update(attributes) unless update_with
+
+ controller.run(update_with)
end
# @private
#
# Response status