From 6b16af6325d46a37b68b46cb669bca9e3ca0cef2 Mon Sep 17 00:00:00 2001 From: yazeed Date: Sat, 1 Jun 2024 12:46:28 +0300 Subject: [PATCH 1/2] feat: Add getOrder method with dynamic column support and tests - Implemented `getOrder()` method to fetch order value from a dynamically determined column, enhancing flexibility in model configuration. - Added tests to validate the `getOrder()` functionality. - Updated PHPDoc for `getOrder()` to clearly explain the method's behavior and its dynamic column determination. This update ensures that the model can handle varying order columns, catering to different sorting configurations seamlessly. --- src/SortableTrait.php | 15 +++++++++++++++ tests/SortableTest.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/SortableTrait.php b/src/SortableTrait.php index b3a6cd3..e7ada34 100644 --- a/src/SortableTrait.php +++ b/src/SortableTrait.php @@ -35,6 +35,21 @@ public function getLowestOrderNumber(): int return (int) $this->buildSortQuery()->min($this->determineOrderColumnName()); } + /** + * Get the order value from a dynamically determined column. + * + * This method returns the model's order value from the column specified by `determineOrderColumnName`. + * It allows for flexibility in specifying the order column in different model configurations. + * + * @return int The value of the order column. + */ + public function getOrder() : int + { + $orderColumnName = $this->determineOrderColumnName(); + + return $this->$orderColumnName; + } + public function scopeOrdered(Builder $query, string $direction = 'asc') { return $query->orderBy($this->determineOrderColumnName(), $direction); diff --git a/tests/SortableTest.php b/tests/SortableTest.php index 4b7248a..9f30181 100644 --- a/tests/SortableTest.php +++ b/tests/SortableTest.php @@ -3,6 +3,8 @@ namespace Spatie\EloquentSortable\Test; use Illuminate\Support\Collection; +use Spatie\EloquentSortable\Sortable; +use Spatie\EloquentSortable\SortableTrait; class SortableTest extends TestCase { @@ -423,4 +425,34 @@ public function it_can_tell_if_element_is_last_in_order() $this->assertTrue($model[$model->count() - 1]->isLastInOrder()); $this->assertFalse($model[$model->count() - 2]->isLastInOrder()); } + + /** @test */ + public function it_can_determine_custom_column_and_get_order_number() + { + $model = Dummy::first(); + + $this->assertEquals($model->getOrder(), 1); + + $model = new class () extends Dummy { + public $sortable = [ + 'order_column_name' => 'my_custom_order_column', + ]; + }; + + $this->assertEquals($model->determineOrderColumnName(), 'my_custom_order_column'); + + $model->my_custom_order_column = 2; + + $this->assertEquals($model->getOrder(), 2); + + $model = new class () extends Dummy { + public $sortable = [ + 'order_column_name' => 'my_other_custom_order_column', + ]; + }; + + $model->my_other_custom_order_column = 3; + + $this->assertEquals($model->getOrder(), 3); + } } From 813eddc08ec0e5207410ac4974151b7476b02c9d Mon Sep 17 00:00:00 2001 From: yazeed Date: Sat, 1 Jun 2024 12:54:47 +0300 Subject: [PATCH 2/2] feat: Add getOrder method with dynamic column support. - Implemented `getOrder()` method to fetch order value from a dynamically determined column, enhancing flexibility in model configuration. - Added tests to validate the `getOrder()` functionality. - Updated PHPDoc for `getOrder()` to clearly explain the method's behavior and its dynamic column determination. This update ensures that the model can handle varying order columns, catering to different sorting configurations seamlessly. --- tests/SortableTest.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/SortableTest.php b/tests/SortableTest.php index 9f30181..40cf7a8 100644 --- a/tests/SortableTest.php +++ b/tests/SortableTest.php @@ -430,29 +430,26 @@ public function it_can_tell_if_element_is_last_in_order() public function it_can_determine_custom_column_and_get_order_number() { $model = Dummy::first(); - $this->assertEquals($model->getOrder(), 1); + + $model = new class () extends Dummy { public $sortable = [ 'order_column_name' => 'my_custom_order_column', ]; }; - - $this->assertEquals($model->determineOrderColumnName(), 'my_custom_order_column'); - $model->my_custom_order_column = 2; - $this->assertEquals($model->getOrder(), 2); + + $model = new class () extends Dummy { public $sortable = [ 'order_column_name' => 'my_other_custom_order_column', ]; }; - $model->my_other_custom_order_column = 3; - $this->assertEquals($model->getOrder(), 3); } }