Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/perron/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ def root?
slug == "/"
end

def destroy
File.delete(@file_path) and self
end

private

ID_LENGTH = 8
Expand Down
4 changes: 4 additions & 0 deletions lib/perron/resource/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/models/content/zombie.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Content::Zombie < Perron::Resource
end
9 changes: 9 additions & 0 deletions test/perron/resource/class_methods_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 12 additions & 0 deletions test/perron/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading