Skip to content

Commit

Permalink
Merge pull request #4 from AuroraWebSoftware/all-shapes
Browse files Browse the repository at this point in the history
All shapes
  • Loading branch information
emreakay authored Oct 2, 2024
2 parents be51ea4 + 999c904 commit 46dd3e0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
26 changes: 18 additions & 8 deletions src/Traits/Flexy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -162,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] =
Expand Down
29 changes: 25 additions & 4 deletions tests/PackageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,37 @@
$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();
//
// 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);
});

0 comments on commit 46dd3e0

Please sign in to comment.