Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Schilling <rschilling@student.tugraz.at>2017-03-02 15:14:13 +0300
committerRobert Schilling <rschilling@student.tugraz.at>2017-08-28 17:40:25 +0300
commite80313f9ee5b3495a8713e6ddae111bc8106155b (patch)
treef1327448ef9e837aedb9fde9a50d6531e42a6112 /lib/api/helpers.rb
parent998afa5f74558be215a924d95aa131a69831ca43 (diff)
Conditionally destroy a ressource
Diffstat (limited to 'lib/api/helpers.rb')
-rw-r--r--lib/api/helpers.rb17
1 files changed, 14 insertions, 3 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index 1c74a14d91c..8d4f8c01903 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -11,14 +11,25 @@ module API
declared(params, options).to_h.symbolize_keys
end
- def check_unmodified_since(last_modified)
- if_unmodified_since = Time.parse(headers['If-Unmodified-Since']) if headers.key?('If-Unmodified-Since') rescue nil
+ def check_unmodified_since!(last_modified)
+ if_unmodified_since = Time.parse(headers['If-Unmodified-Since']) rescue nil
- if if_unmodified_since && if_unmodified_since < last_modified
+ if if_unmodified_since && last_modified > if_unmodified_since
render_api_error!('412 Precondition Failed', 412)
end
end
+ def destroy_conditionally!(resource, last_update_field: :updated_at)
+ check_unmodified_since!(resource.public_send(last_update_field))
+
+ status 204
+ if block_given?
+ yield resource
+ else
+ resource.destroy
+ end
+ end
+
def current_user
return @current_user if defined?(@current_user)