From 7fbe9907e721911f18091f682383f6ff136959b2 Mon Sep 17 00:00:00 2001 From: Mehmet Faruk Demirkoparan Date: Wed, 2 Oct 2024 11:50:31 +0300 Subject: [PATCH 1/2] - Sort bug fix - Value save fix --- src/Traits/Flexy.php | 37 +++++++++++++++++++++++-------------- tests/PackageTest.php | 29 +++++++++++++++++++++++++---- 2 files changed, 48 insertions(+), 18 deletions(-) diff --git a/src/Traits/Flexy.php b/src/Traits/Flexy.php index 16b56bc..dfe5673 100644 --- a/src/Traits/Flexy.php +++ b/src/Traits/Flexy.php @@ -68,7 +68,14 @@ public static function bootFlexy(): void Validator::make($data, $rules, $messages)->validate(); } - $addition = []; + $addition = [ + 'value_string' => null, + 'value_int' => null, + 'value_decimal' => null, + 'value_datetime' => null, + 'value_boolean' => null, + 'value_json' => null, + ]; if (is_string($value)) { $addition['value_string'] = $value; @@ -111,13 +118,12 @@ public static function bootFlexy(): void } public static function setFlexyShape( - string $fieldName, FlexyFieldType $fieldType, - int $sort, + string $fieldName, FlexyFieldType $fieldType, + int $sort, ?string $validationRules = null, - ?array $validationMessages = null, - ?array $fieldMetadata = [] - ): Shape - { + ?array $validationMessages = null, + ?array $fieldMetadata = [] + ): Shape { $modelType = static::getModelType(); return Shape::updateOrCreate( @@ -163,13 +169,16 @@ public function flexy(): Attribute $this->fields->_model_type = static::getModelType(); $this->fields->_model_id = $this->id; -// leftJoin('ff_shapes', 'ff_values.model_type', '=', 'ff_shapes.model_type') - $values = Value::where( - [ - 'ff_values.model_type' => static::getModelType(), - 'ff_values.model_id' => $this->id, - ] - )->get(); + $values = Value::where([ + 'ff_values.model_type' => static::getModelType(), + 'ff_values.model_id' => $this->id, + ]) + ->leftJoin('ff_shapes', function ($join) { + $join->on('ff_values.field_name', '=', 'ff_shapes.field_name') + ->on('ff_values.model_type', '=', 'ff_shapes.model_type'); + }) + ->orderBy('ff_shapes.sort') + ->get(); $values->each(function ($value) { $this->fields[$value->field_name] = diff --git a/tests/PackageTest.php b/tests/PackageTest.php index fac03d1..df57237 100644 --- a/tests/PackageTest.php +++ b/tests/PackageTest.php @@ -219,8 +219,8 @@ $flexyModel1 = ExampleShapelyFlexyModel::create(['name' => 'ExampleFlexyModel 1']); ExampleShapelyFlexyModel::$hasShape = true; - ExampleShapelyFlexyModel::setFlexyShape('a', FlexyFieldType::INTEGER, 2, 'numeric|max:7'); - ExampleShapelyFlexyModel::setFlexyShape('b', FlexyFieldType::INTEGER, 1, 'numeric|max:7'); + ExampleShapelyFlexyModel::setFlexyShape('a', FlexyFieldType::INTEGER, 3, 'numeric|max:7'); + ExampleShapelyFlexyModel::setFlexyShape('b', FlexyFieldType::INTEGER, 2, 'numeric|max:7'); // $flexyModel1->flexy->a = 5; // $flexyModel1->save(); @@ -228,7 +228,28 @@ // dd(ExampleShapelyFlexyModel::getAllFlexyShapes()); expect(ExampleShapelyFlexyModel::getAllFlexyShapes())->toHaveCount(2); - ExampleShapelyFlexyModel::setFlexyShape('c', FlexyFieldType::STRING, 5, 'string|max:7'); - + ExampleShapelyFlexyModel::setFlexyShape('c', FlexyFieldType::STRING, 1, 'string|max:7'); + // dd(ExampleShapelyFlexyModel::getAllFlexyShapes()); expect(ExampleShapelyFlexyModel::getAllFlexyShapes())->toHaveCount(3); }); + +it('can get all shapes models field_name', function () { + $flexyModel1 = ExampleShapelyFlexyModel::create(['name' => 'ExampleFlexyModel 1']); + $flexyModel2 = ExampleShapelyFlexyModel::create(['name' => 'ExampleFlexyModel 2']); + ExampleShapelyFlexyModel::$hasShape = true; + + $flexyModel1::setFlexyShape('a', FlexyFieldType::INTEGER, 3, 'numeric|max:7'); + $flexyModel2::setFlexyShape('a', FlexyFieldType::INTEGER, 2, 'numeric|max:7'); + + $flexyModel1->flexy->a = 5; + $flexyModel1->save(); + $flexyModel2->flexy->a = 1; + $flexyModel2->save(); + + // dd(ExampleShapelyFlexyModel::getAllFlexyShapes()); + expect(ExampleShapelyFlexyModel::getAllFlexyShapes())->toHaveCount(1); + + ExampleShapelyFlexyModel::setFlexyShape('c', FlexyFieldType::STRING, 1, 'string|max:7'); + // dd(ExampleShapelyFlexyModel::getAllFlexyShapes()); + expect(ExampleShapelyFlexyModel::getAllFlexyShapes())->toHaveCount(2); +}); From 999c904280c2b334bb02e850035f7ef495a4ce06 Mon Sep 17 00:00:00 2001 From: MFarukDemirkoparan Date: Wed, 2 Oct 2024 08:51:56 +0000 Subject: [PATCH 2/2] Fix styling --- src/Traits/Flexy.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/Traits/Flexy.php b/src/Traits/Flexy.php index 975c2dd..dfe5673 100644 --- a/src/Traits/Flexy.php +++ b/src/Traits/Flexy.php @@ -118,13 +118,12 @@ public static function bootFlexy(): void } public static function setFlexyShape( - string $fieldName, FlexyFieldType $fieldType, - int $sort, + string $fieldName, FlexyFieldType $fieldType, + int $sort, ?string $validationRules = null, - ?array $validationMessages = null, - ?array $fieldMetadata = [] - ): Shape - { + ?array $validationMessages = null, + ?array $fieldMetadata = [] + ): Shape { $modelType = static::getModelType(); return Shape::updateOrCreate(