Skip to content

Commit b9306a9

Browse files
committed
Sorting: Renamed sort set to sort rule
Renamed based on feedback from Tim and Script on Discord. Also fixed flaky test
1 parent a208c46 commit b9306a9

30 files changed

+232
-224
lines changed

app/Activity/ActivityType.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ class ActivityType
7171
const IMPORT_RUN = 'import_run';
7272
const IMPORT_DELETE = 'import_delete';
7373

74-
const SORT_SET_CREATE = 'sort_set_create';
75-
const SORT_SET_UPDATE = 'sort_set_update';
76-
const SORT_SET_DELETE = 'sort_set_delete';
74+
const SORT_RULE_CREATE = 'sort_rule_create';
75+
const SORT_RULE_UPDATE = 'sort_rule_update';
76+
const SORT_RULE_DELETE = 'sort_rule_delete';
7777

7878
/**
7979
* Get all the possible values.

app/Console/Commands/AssignSortSetCommand.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use BookStack\Entities\Models\Book;
66
use BookStack\Sorting\BookSorter;
7-
use BookStack\Sorting\SortSet;
7+
use BookStack\Sorting\SortRule;
88
use Illuminate\Console\Command;
99

1010
class AssignSortSetCommand extends Command
@@ -37,7 +37,7 @@ public function handle(BookSorter $sorter): int
3737
return $this->listSortSets();
3838
}
3939

40-
$set = SortSet::query()->find($sortSetId);
40+
$set = SortRule::query()->find($sortSetId);
4141
if ($this->option('all-books')) {
4242
$query = Book::query();
4343
} else if ($this->option('books-without-sort')) {
@@ -87,7 +87,7 @@ public function handle(BookSorter $sorter): int
8787
protected function listSortSets(): int
8888
{
8989

90-
$sets = SortSet::query()->orderBy('id', 'asc')->get();
90+
$sets = SortRule::query()->orderBy('id', 'asc')->get();
9191
$this->error("Sort set ID required!");
9292
$this->warn("\nAvailable sort sets:");
9393
foreach ($sets as $set) {

app/Entities/Models/Book.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace BookStack\Entities\Models;
44

5-
use BookStack\Sorting\SortSet;
5+
use BookStack\Sorting\SortRule;
66
use BookStack\Uploads\Image;
77
use Exception;
88
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -17,14 +17,14 @@
1717
* @property string $description
1818
* @property int $image_id
1919
* @property ?int $default_template_id
20-
* @property ?int $sort_set_id
20+
* @property ?int $sort_rule_id
2121
* @property Image|null $cover
2222
* @property \Illuminate\Database\Eloquent\Collection $chapters
2323
* @property \Illuminate\Database\Eloquent\Collection $pages
2424
* @property \Illuminate\Database\Eloquent\Collection $directPages
2525
* @property \Illuminate\Database\Eloquent\Collection $shelves
2626
* @property ?Page $defaultTemplate
27-
* @property ?SortSet $sortSet
27+
* @property ?SortRule $sortRule
2828
*/
2929
class Book extends Entity implements HasCoverImage
3030
{
@@ -88,9 +88,9 @@ public function defaultTemplate(): BelongsTo
8888
/**
8989
* Get the sort set assigned to this book, if existing.
9090
*/
91-
public function sortSet(): BelongsTo
91+
public function sortRule(): BelongsTo
9292
{
93-
return $this->belongsTo(SortSet::class);
93+
return $this->belongsTo(SortRule::class);
9494
}
9595

9696
/**

app/Entities/Repos/BookRepo.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
use BookStack\Entities\Tools\TrashCan;
99
use BookStack\Exceptions\ImageUploadException;
1010
use BookStack\Facades\Activity;
11-
use BookStack\Sorting\SortSet;
11+
use BookStack\Sorting\SortRule;
1212
use BookStack\Uploads\ImageRepo;
1313
use Exception;
1414
use Illuminate\Http\UploadedFile;
@@ -35,8 +35,8 @@ public function create(array $input): Book
3535
Activity::add(ActivityType::BOOK_CREATE, $book);
3636

3737
$defaultBookSortSetting = intval(setting('sorting-book-default', '0'));
38-
if ($defaultBookSortSetting && SortSet::query()->find($defaultBookSortSetting)) {
39-
$book->sort_set_id = $defaultBookSortSetting;
38+
if ($defaultBookSortSetting && SortRule::query()->find($defaultBookSortSetting)) {
39+
$book->sort_rule_id = $defaultBookSortSetting;
4040
$book->save();
4141
}
4242

app/Sorting/BookSortController.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ public function update(Request $request, BookSorter $sorter, string $bookSlug)
6969

7070
if ($request->filled('auto-sort')) {
7171
$sortSetId = intval($request->get('auto-sort')) ?: null;
72-
if ($sortSetId && SortSet::query()->find($sortSetId) === null) {
72+
if ($sortSetId && SortRule::query()->find($sortSetId) === null) {
7373
$sortSetId = null;
7474
}
75-
$book->sort_set_id = $sortSetId;
75+
$book->sort_rule_id = $sortSetId;
7676
$book->save();
7777
$sorter->runBookAutoSort($book);
7878
if (!$loggedActivityForBook) {

app/Sorting/BookSorter.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct(
1616
) {
1717
}
1818

19-
public function runBookAutoSortForAllWithSet(SortSet $set): void
19+
public function runBookAutoSortForAllWithSet(SortRule $set): void
2020
{
2121
$set->books()->chunk(50, function ($books) {
2222
foreach ($books as $book) {
@@ -32,12 +32,12 @@ public function runBookAutoSortForAllWithSet(SortSet $set): void
3232
*/
3333
public function runBookAutoSort(Book $book): void
3434
{
35-
$set = $book->sortSet;
35+
$set = $book->sortRule;
3636
if (!$set) {
3737
return;
3838
}
3939

40-
$sortFunctions = array_map(function (SortSetOperation $op) {
40+
$sortFunctions = array_map(function (SortRuleOperation $op) {
4141
return $op->getSortFunction();
4242
}, $set->getOperations());
4343

app/Sorting/SortSet.php renamed to app/Sorting/SortRule.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,24 @@
1717
* @property Carbon $created_at
1818
* @property Carbon $updated_at
1919
*/
20-
class SortSet extends Model implements Loggable
20+
class SortRule extends Model implements Loggable
2121
{
2222
use HasFactory;
2323

2424
/**
25-
* @return SortSetOperation[]
25+
* @return SortRuleOperation[]
2626
*/
2727
public function getOperations(): array
2828
{
29-
return SortSetOperation::fromSequence($this->sequence);
29+
return SortRuleOperation::fromSequence($this->sequence);
3030
}
3131

3232
/**
33-
* @param SortSetOperation[] $options
33+
* @param SortRuleOperation[] $options
3434
*/
3535
public function setOperations(array $options): void
3636
{
37-
$values = array_map(fn (SortSetOperation $opt) => $opt->value, $options);
37+
$values = array_map(fn (SortRuleOperation $opt) => $opt->value, $options);
3838
$this->sequence = implode(',', $values);
3939
}
4040

@@ -45,7 +45,7 @@ public function logDescriptor(): string
4545

4646
public function getUrl(): string
4747
{
48-
return url("/settings/sorting/sets/{$this->id}");
48+
return url("/settings/sorting/rules/{$this->id}");
4949
}
5050

5151
public function books(): HasMany

app/Sorting/SortSetController.php renamed to app/Sorting/SortRuleController.php

+29-29
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use BookStack\Http\Controller;
77
use Illuminate\Http\Request;
88

9-
class SortSetController extends Controller
9+
class SortRuleController extends Controller
1010
{
1111
public function __construct()
1212
{
@@ -15,9 +15,9 @@ public function __construct()
1515

1616
public function create()
1717
{
18-
$this->setPageTitle(trans('settings.sort_set_create'));
18+
$this->setPageTitle(trans('settings.sort_rule_create'));
1919

20-
return view('settings.sort-sets.create');
20+
return view('settings.sort-rules.create');
2121
}
2222

2323
public function store(Request $request)
@@ -27,28 +27,28 @@ public function store(Request $request)
2727
'sequence' => ['required', 'string', 'min:1'],
2828
]);
2929

30-
$operations = SortSetOperation::fromSequence($request->input('sequence'));
30+
$operations = SortRuleOperation::fromSequence($request->input('sequence'));
3131
if (count($operations) === 0) {
3232
return redirect()->withInput()->withErrors(['sequence' => 'No operations set.']);
3333
}
3434

35-
$set = new SortSet();
36-
$set->name = $request->input('name');
37-
$set->setOperations($operations);
38-
$set->save();
35+
$rule = new SortRule();
36+
$rule->name = $request->input('name');
37+
$rule->setOperations($operations);
38+
$rule->save();
3939

40-
$this->logActivity(ActivityType::SORT_SET_CREATE, $set);
40+
$this->logActivity(ActivityType::SORT_RULE_CREATE, $rule);
4141

4242
return redirect('/settings/sorting');
4343
}
4444

4545
public function edit(string $id)
4646
{
47-
$set = SortSet::query()->findOrFail($id);
47+
$rule = SortRule::query()->findOrFail($id);
4848

49-
$this->setPageTitle(trans('settings.sort_set_edit'));
49+
$this->setPageTitle(trans('settings.sort_rule_edit'));
5050

51-
return view('settings.sort-sets.edit', ['set' => $set]);
51+
return view('settings.sort-rules.edit', ['rule' => $rule]);
5252
}
5353

5454
public function update(string $id, Request $request, BookSorter $bookSorter)
@@ -58,38 +58,38 @@ public function update(string $id, Request $request, BookSorter $bookSorter)
5858
'sequence' => ['required', 'string', 'min:1'],
5959
]);
6060

61-
$set = SortSet::query()->findOrFail($id);
62-
$operations = SortSetOperation::fromSequence($request->input('sequence'));
61+
$rule = SortRule::query()->findOrFail($id);
62+
$operations = SortRuleOperation::fromSequence($request->input('sequence'));
6363
if (count($operations) === 0) {
64-
return redirect($set->getUrl())->withInput()->withErrors(['sequence' => 'No operations set.']);
64+
return redirect($rule->getUrl())->withInput()->withErrors(['sequence' => 'No operations set.']);
6565
}
6666

67-
$set->name = $request->input('name');
68-
$set->setOperations($operations);
69-
$changedSequence = $set->isDirty('sequence');
70-
$set->save();
67+
$rule->name = $request->input('name');
68+
$rule->setOperations($operations);
69+
$changedSequence = $rule->isDirty('sequence');
70+
$rule->save();
7171

72-
$this->logActivity(ActivityType::SORT_SET_UPDATE, $set);
72+
$this->logActivity(ActivityType::SORT_RULE_UPDATE, $rule);
7373

7474
if ($changedSequence) {
75-
$bookSorter->runBookAutoSortForAllWithSet($set);
75+
$bookSorter->runBookAutoSortForAllWithSet($rule);
7676
}
7777

7878
return redirect('/settings/sorting');
7979
}
8080

8181
public function destroy(string $id, Request $request)
8282
{
83-
$set = SortSet::query()->findOrFail($id);
83+
$rule = SortRule::query()->findOrFail($id);
8484
$confirmed = $request->input('confirm') === 'true';
85-
$booksAssigned = $set->books()->count();
85+
$booksAssigned = $rule->books()->count();
8686
$warnings = [];
8787

8888
if ($booksAssigned > 0) {
8989
if ($confirmed) {
90-
$set->books()->update(['sort_set_id' => null]);
90+
$rule->books()->update(['sort_rule_id' => null]);
9191
} else {
92-
$warnings[] = trans('settings.sort_set_delete_warn_books', ['count' => $booksAssigned]);
92+
$warnings[] = trans('settings.sort_rule_delete_warn_books', ['count' => $booksAssigned]);
9393
}
9494
}
9595

@@ -98,16 +98,16 @@ public function destroy(string $id, Request $request)
9898
if ($confirmed) {
9999
setting()->remove('sorting-book-default');
100100
} else {
101-
$warnings[] = trans('settings.sort_set_delete_warn_default');
101+
$warnings[] = trans('settings.sort_rule_delete_warn_default');
102102
}
103103
}
104104

105105
if (count($warnings) > 0) {
106-
return redirect($set->getUrl() . '#delete')->withErrors(['delete' => $warnings]);
106+
return redirect($rule->getUrl() . '#delete')->withErrors(['delete' => $warnings]);
107107
}
108108

109-
$set->delete();
110-
$this->logActivity(ActivityType::SORT_SET_DELETE, $set);
109+
$rule->delete();
110+
$this->logActivity(ActivityType::SORT_RULE_DELETE, $rule);
111111

112112
return redirect('/settings/sorting');
113113
}

app/Sorting/SortSetOperation.php renamed to app/Sorting/SortRuleOperation.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Closure;
66
use Illuminate\Support\Str;
77

8-
enum SortSetOperation: string
8+
enum SortRuleOperation: string
99
{
1010
case NameAsc = 'name_asc';
1111
case NameDesc = 'name_desc';
@@ -26,13 +26,13 @@ public function getLabel(): string
2626
$label = '';
2727
if (str_ends_with($key, '_asc')) {
2828
$key = substr($key, 0, -4);
29-
$label = trans('settings.sort_set_op_asc');
29+
$label = trans('settings.sort_rule_op_asc');
3030
} elseif (str_ends_with($key, '_desc')) {
3131
$key = substr($key, 0, -5);
32-
$label = trans('settings.sort_set_op_desc');
32+
$label = trans('settings.sort_rule_op_desc');
3333
}
3434

35-
$label = trans('settings.sort_set_op_' . $key) . ' ' . $label;
35+
$label = trans('settings.sort_rule_op_' . $key) . ' ' . $label;
3636
return trim($label);
3737
}
3838

@@ -43,12 +43,12 @@ public function getSortFunction(): callable
4343
}
4444

4545
/**
46-
* @return SortSetOperation[]
46+
* @return SortRuleOperation[]
4747
*/
4848
public static function allExcluding(array $operations): array
4949
{
50-
$all = SortSetOperation::cases();
51-
$filtered = array_filter($all, function (SortSetOperation $operation) use ($operations) {
50+
$all = SortRuleOperation::cases();
51+
$filtered = array_filter($all, function (SortRuleOperation $operation) use ($operations) {
5252
return !in_array($operation, $operations);
5353
});
5454
return array_values($filtered);
@@ -57,12 +57,12 @@ public static function allExcluding(array $operations): array
5757
/**
5858
* Create a set of operations from a string sequence representation.
5959
* (values seperated by commas).
60-
* @return SortSetOperation[]
60+
* @return SortRuleOperation[]
6161
*/
6262
public static function fromSequence(string $sequence): array
6363
{
6464
$strOptions = explode(',', $sequence);
65-
$options = array_map(fn ($val) => SortSetOperation::tryFrom($val), $strOptions);
65+
$options = array_map(fn ($val) => SortRuleOperation::tryFrom($val), $strOptions);
6666
return array_filter($options);
6767
}
6868
}

database/factories/Entities/Models/BookFactory.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function definition()
2727
'slug' => Str::random(10),
2828
'description' => $description,
2929
'description_html' => '<p>' . e($description) . '</p>',
30-
'sort_set_id' => null,
30+
'sort_rule_id' => null,
3131
'default_template_id' => null,
3232
];
3333
}

0 commit comments

Comments
 (0)