Skip to content

Commit bf3d7b5

Browse files
[9.x] Add disable duplicate action option (#801)
Co-authored-by: Duncan McClean <duncan@doublethree.digital>
1 parent 4f764af commit bf3d7b5

5 files changed

Lines changed: 56 additions & 1 deletion

File tree

docs/resources.mdx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,19 @@ You may also specify if you want a resource to be 'read only' - eg. users will n
110110
],
111111
```
112112

113+
### Duplicatable
114+
115+
By default, Runway allows models to be duplicated via the Duplicate action in the Control Panel. You can disable this for a resource by setting `duplicatable` to `false`.
116+
117+
```php
118+
'resources' => [
119+
\App\Models\Order::class => [
120+
'name' => 'Orders',
121+
'duplicatable' => false,
122+
],
123+
],
124+
```
125+
113126
### Ordering
114127

115128
Sometimes you may want to change the order that your models are returned in the Control Panel listing table. You can use the `order_by` and `order_by_direction` configuration options to tell Runway the order you wish models to be returned.

src/Actions/DuplicateModel.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ public function visibleTo($item)
2727
return false;
2828
}
2929

30-
return $item instanceof Model && $resource->hasVisibleBlueprint() && $resource->readOnly() !== true;
30+
return $item instanceof Model
31+
&& $resource->duplicatable()
32+
&& $resource->hasVisibleBlueprint()
33+
&& ! $resource->readOnly();
3134
}
3235

3336
public function visibleToBulk($items)

src/Resource.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ public function readOnly(): bool
133133
return $this->config->get('read_only', false);
134134
}
135135

136+
public function duplicatable(): bool
137+
{
138+
return $this->config->get('duplicatable', true);
139+
}
140+
136141
public function orderBy(): string
137142
{
138143
return $this->config->get('order_by', $this->primaryKey());

tests/Actions/DuplicateModelTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public function is_not_visible_to_eloquent_model_when_resource_is_read_only()
4646
$this->assertFalse($visibleTo);
4747
}
4848

49+
#[Test]
50+
public function is_not_visible_to_eloquent_model_when_resource_is_not_duplicatable()
51+
{
52+
Config::set('runway.resources.StatamicRadPack\\Runway\\Tests\\Fixtures\\Models\\Post.duplicatable', false);
53+
54+
Runway::discoverResources();
55+
56+
$visibleTo = (new DuplicateModel)->visibleTo(Post::factory()->create());
57+
58+
$this->assertFalse($visibleTo);
59+
}
60+
4961
#[Test]
5062
public function is_not_visible_to_eloquent_model_when_blueprint_is_hidden()
5163
{

tests/ResourceTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,28 @@ public function can_get_configured_plural()
154154
$this->assertEquals($plural, 'Bibliotheken');
155155
}
156156

157+
#[Test]
158+
public function duplicatable_defaults_to_true()
159+
{
160+
Runway::discoverResources();
161+
162+
$resource = Runway::findResource('post');
163+
164+
$this->assertTrue($resource->duplicatable());
165+
}
166+
167+
#[Test]
168+
public function duplicatable_can_be_disabled()
169+
{
170+
Config::set('runway.resources.StatamicRadPack\Runway\Tests\Fixtures\Models\Post.duplicatable', false);
171+
172+
Runway::discoverResources();
173+
174+
$resource = Runway::findResource('post');
175+
176+
$this->assertFalse($resource->duplicatable());
177+
}
178+
157179
#[Test]
158180
public function can_get_blueprint()
159181
{

0 commit comments

Comments
 (0)