diff --git a/lib/perron/resource.rb b/lib/perron/resource.rb index 0f914b5..9373401 100644 --- a/lib/perron/resource.rb +++ b/lib/perron/resource.rb @@ -96,6 +96,10 @@ def root? slug == "/" end + def destroy + File.delete(@file_path) and self + end + private ID_LENGTH = 8 diff --git a/lib/perron/resource/class_methods.rb b/lib/perron/resource/class_methods.rb index 3abbce3..205d5bf 100644 --- a/lib/perron/resource/class_methods.rb +++ b/lib/perron/resource/class_methods.rb @@ -44,6 +44,10 @@ def collection = Collection.new(collection_name) def root = all.find(&:root?) + def destroy_all + all.each(&:destroy) + end + def model_name @model_name ||= ActiveModel::Name.new(self, nil, name.demodulize.to_s) end diff --git a/test/dummy/app/models/content/zombie.rb b/test/dummy/app/models/content/zombie.rb new file mode 100644 index 0000000..f1a7d55 --- /dev/null +++ b/test/dummy/app/models/content/zombie.rb @@ -0,0 +1,4 @@ +# frozen_string_literal: true + +class Content::Zombie < Perron::Resource +end \ No newline at end of file diff --git a/test/perron/resource/class_methods_test.rb b/test/perron/resource/class_methods_test.rb index fea674c..c5c10b6 100644 --- a/test/perron/resource/class_methods_test.rb +++ b/test/perron/resource/class_methods_test.rb @@ -151,4 +151,13 @@ class Perron::Resource::ClassMethodsTest < ActiveSupport::TestCase assert_instance_of Perron::Relation, posts assert_equal 2, posts.size end + + test ".destroy_all deletes all resource files and returns array of deleted resources" do + existing_count = Content::Zombie.count + + deleted = Content::Zombie.destroy_all + + assert_equal existing_count, deleted.size + assert_equal 0, Content::Zombie.count + end end diff --git a/test/perron/resource_test.rb b/test/perron/resource_test.rb index cfeac9a..98d96cc 100644 --- a/test/perron/resource_test.rb +++ b/test/perron/resource_test.rb @@ -145,4 +145,16 @@ class Perron::Site::ResourceTest < ActiveSupport::TestCase assert_equal :not_found, result[:status] end + + test "#destroy deletes the resource file and returns self" do + test_file = "test/dummy/app/content/zombies/destroy-test-zombie.md" + File.write(test_file, "---\ntitle: Test Zombie\n---\nTest content") + + zombie = Content::Zombie.new(test_file) + + result = zombie.destroy + + assert_equal zombie, result + assert_not File.exist?(test_file) + end end