From 9250bb77902e4a1b6772fa5c2653af6b2b0b8e9a Mon Sep 17 00:00:00 2001 From: Garret Alfert Date: Wed, 26 Sep 2012 19:58:43 +0200 Subject: [PATCH] Create directory objects for parent directories that don't contain any files themselves --- lib/remote_storage/riak.rb | 13 +++++++++++++ spec/directories_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/lib/remote_storage/riak.rb b/lib/remote_storage/riak.rb index 68c7a83..fe8f6f7 100644 --- a/lib/remote_storage/riak.rb +++ b/lib/remote_storage/riak.rb @@ -72,11 +72,24 @@ module RemoteStorage :directory_bin => [category]}) object.store + create_parent_directory_objects(user, category) update_directory_object(user, category) rescue ::Riak::HTTPFailedRequest halt 422 end + def create_parent_directory_objects(user, category) + parent_directories = category.split("/") + parent_directories.pop + while parent_directories.any? + directory = parent_directories.join("/") + unless directory_bucket.exist?("#{user}:#{directory}") + update_directory_object(user, directory) + end + parent_directories.pop + end + end + def update_directory_object(user, category) if category.match /\// parent_directory = category[0..category.rindex("/")-1] diff --git a/spec/directories_spec.rb b/spec/directories_spec.rb index 7f6a15a..c6f9c0d 100644 --- a/spec/directories_spec.rb +++ b/spec/directories_spec.rb @@ -65,6 +65,29 @@ describe "Directories" do content["home/"].to_s.must_match /\d+/ content["home/"].to_s.length.must_be :>=, 10 end + + context "sub-directories without objects" do + it "lists the direct sub-directories" do + put "/jimmy/tasks/private/projects/world-domination/start", "write a manifesto" + get "/jimmy/tasks/private/" + + last_response.status.must_equal 200 + + content = JSON.parse(last_response.body) + content.must_include "projects/" + content["projects/"].to_s.must_match /\d+/ + content["projects/"].to_s.length.must_be :>=, 10 + end + + it "does not update existing directory objects" do + tasks_timestamp = directory_bucket.get("jimmy:tasks").last_modified + wait_a_second + put "/jimmy/tasks/private/projects/world-domination/start", "write a manifesto" + + tasks_object = directory_bucket.get("jimmy:tasks") + tasks_object.last_modified.must_equal tasks_timestamp + end + end end context "for a sub-directory" do