Skip to content

Commit c9f9175

Browse files
committed
chore(concern): [Customizable] add load custom field values method
1 parent 3185013 commit c9f9175

File tree

2 files changed

+91
-5
lines changed

2 files changed

+91
-5
lines changed

src/Concerns/Customizable.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ trait Customizable
1818
{
1919
protected array $validatedCustomFieldValues = [];
2020

21+
/**
22+
* Boot Model Observer.
23+
*/
2124
public static function bootCustomizable(): void
2225
{
2326
static::observe(ModelObserver::class);
@@ -53,11 +56,10 @@ public function validateCustomFields(): void
5356
$this->validatedCustomFieldValues = $validator->validate();
5457
}
5558

56-
protected function getTableColumns(): Collection
57-
{
58-
return Collection::make(Schema::getColumnListing($this->getTable()));
59-
}
6059

60+
/**
61+
* Get the custom fields associated with the model.
62+
*/
6163
public function getCustomFields(): Collection
6264
{
6365
$context = $this->getCustomFieldContext();
@@ -76,11 +78,17 @@ public function getCustomFields(): Collection
7678
return $customFields;
7779
}
7880

81+
/**
82+
* Get the context model associated with the model.
83+
*/
7984
public function getCustomFieldContext(): ?Model
8085
{
8186
return null;
8287
}
8388

89+
/**
90+
* Save the custom field values.
91+
*/
8492
public function saveCustomFieldValues(): void
8593
{
8694
$validatedCustomFieldValues = $this->validatedCustomFieldValues;
@@ -101,4 +109,27 @@ public function saveCustomFieldValues(): void
101109
}
102110
});
103111
}
112+
113+
/**
114+
* Load custom field values and set them as model attributes.
115+
*/
116+
public function loadCustomFieldValues(): void
117+
{
118+
if (! $this->customFieldValues) {
119+
return;
120+
}
121+
122+
$this->customFieldValues->each(function (CustomFieldValue $customFieldValue) {
123+
$attribute = $customFieldValue->customField->key;
124+
$this->setAttribute($attribute, $customFieldValue->customField->parseValue($customFieldValue->value));
125+
});
126+
}
127+
128+
/**
129+
* Get the table columns of the model.
130+
*/
131+
protected function getTableColumns(): Collection
132+
{
133+
return Collection::make(Schema::getColumnListing($this->getTable()));
134+
}
104135
}

tests/Unit/Concerns/CustomizableTest.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace OnrampLab\CustomFields\Tests\Unit\Concerns;
44

5+
use Illuminate\Validation\ValidationException;
56
use OnrampLab\CustomFields\Models\CustomField;
67
use OnrampLab\CustomFields\Tests\Classes\Account;
78
use OnrampLab\CustomFields\Tests\Classes\User;
89
use OnrampLab\CustomFields\Tests\TestCase;
10+
use OnrampLab\CustomFields\ValueObjects\AvailableOption;
911

1012
class CustomizableTest extends TestCase
1113
{
@@ -18,7 +20,7 @@ protected function setUp(): void
1820
'type' => 'text',
1921
'model_class' => User::class,
2022
'contextable_id' => $this->account->id,
21-
'contextable_type' => get_class($this->account)
23+
'contextable_type' => $this->account->getMorphClass()
2224
];
2325
$this->customField = CustomField::factory()->create($attributes);
2426
$this->user = User::factory()->create(['account_id' => $this->account->id, 'zip_code' => '12345']);
@@ -34,4 +36,57 @@ public function custom_field_values_relationship_should_work(): void
3436
$this->assertEquals($this->user->id, $customFieldValues->first()->customizable_id);
3537
$this->assertEquals(get_class($this->user), $customFieldValues->first()->customizable_type);
3638
}
39+
40+
/**
41+
* @test
42+
*/
43+
public function validate_custom_fields_should_work()
44+
{
45+
$this->expectException(ValidationException::class);
46+
$this->user->zip_code = 123;
47+
$this->user->validateCustomFields();
48+
}
49+
50+
/**
51+
* @test
52+
* @dataProvider customFieldDataProvider
53+
*/
54+
public function custom_load_custom_field_values_should_work($type, $value, $expected): void
55+
{
56+
$attributes = [
57+
'key' => 'field',
58+
'type' => $type,
59+
'model_class' => User::class,
60+
'contextable_id' => $this->account->id,
61+
'contextable_type' => $this->account->getMorphClass()
62+
];
63+
if ($type == 'select') {
64+
$attributes['available_options'] = [
65+
new AvailableOption([
66+
'name' => 'Option 1',
67+
'value' => 'Option 1',
68+
]),
69+
new AvailableOption([
70+
'name' => 'Option 2',
71+
'value' => 'Option 2',
72+
])
73+
];
74+
}
75+
$customField = CustomField::factory()->create($attributes);
76+
$user = User::factory()->create(['account_id' => $this->account->id, 'field' => $value]);
77+
$user->loadCustomFieldValues();
78+
$this->assertEquals($expected, $user->field);
79+
}
80+
81+
public function customFieldDataProvider(): array
82+
{
83+
return [
84+
'Text field' => ['text', 'Value', 'Value'],
85+
'Integer field' => ['integer', '42', 42],
86+
'Float field' => ['float', '3.14', 3.14],
87+
'Datetime field' => ['datetime', '2023-05-16 12:34:56', '2023-05-16 12:34:56'],
88+
'Select field' => ['select', 'Option 1', 'Option 1'],
89+
'Boolean field' => ['boolean', '1', true],
90+
];
91+
}
3792
}

0 commit comments

Comments
 (0)